added new /me path to geolite2 server + update flyio machine size

main
TZGyn 1 year ago
parent 7d6685a44d
commit 65d6b224ef
Signed by: TZGyn
GPG Key ID: 122EAF77AE81FD4A

@ -16,7 +16,7 @@ primary_region = 'iad'
[http_service] [http_service]
internal_port = 3000 internal_port = 3000
force_https = true force_https = true
auto_stop_machines = 'stop' auto_stop_machines = 'off'
auto_start_machines = true auto_start_machines = true
min_machines_running = 0 min_machines_running = 0
processes = ['app'] processes = ['app']

@ -37,25 +37,29 @@ func main() {
return c.SendString("Hello, World!") return c.SendString("Hello, World!")
}) })
app.Get("/:ip", func(c *fiber.Ctx) error { app.Get("/me", func(c *fiber.Ctx) error {
queryIp := c.Params("ip") ip := c.IPs()[0]
db, err := geoip2.Open("./data/GeoLite2-City.mmdb") record, err := getCity(ip)
if err != nil { if err != nil {
log.Fatal(err) c.JSON(fiber.Map{
"success": false,
"message": err.Error(),
})
} }
defer db.Close()
// If you are using strings that may be invalid, check that ip is not nil return c.JSON(record)
ip := net.ParseIP(queryIp)
if ip == nil {
return c.JSON(fiber.Map{
"success": false, "message": "Please provide a valid ip",
}) })
}
record, err := db.City(ip) app.Get("/:ip", func(c *fiber.Ctx) error {
queryIp := c.Params("ip")
record, err := getCity(queryIp)
if err != nil { if err != nil {
log.Fatal(err) c.JSON(fiber.Map{
"success": false,
"message": err.Error(),
})
} }
return c.JSON(record) return c.JSON(record)
@ -91,6 +95,26 @@ func main() {
c.Stop() c.Stop()
} }
func getCity(queryIp string) (*geoip2.City, error) {
db, err := geoip2.Open("./data/GeoLite2-City.mmdb")
if err != nil {
return nil, err
}
defer db.Close()
// If you are using strings that may be invalid, check that ip is not nil
ip := net.ParseIP(queryIp)
if ip == nil {
return nil, errors.New("invalid ip")
}
record, err := db.City(ip)
if err != nil {
return nil, errors.New("no data")
}
return record, nil
}
func downloadAndExtractDB() error { func downloadAndExtractDB() error {
_, err := os.ReadDir("./data") _, err := os.ReadDir("./data")
if err != nil { if err != nil {

@ -17,6 +17,6 @@ primary_region = 'dfw'
processes = ['app'] processes = ['app']
[[vm]] [[vm]]
memory = '512' memory = '256'
cpu_kind = 'shared' cpu_kind = 'shared'
cpus = 1 cpus = 1

Loading…
Cancel
Save