diff --git a/.air.toml b/.air.toml new file mode 100644 index 0000000..58fff2a --- /dev/null +++ b/.air.toml @@ -0,0 +1,51 @@ +root = "." +testdata_dir = "testdata" +tmp_dir = "tmp" + +[build] + args_bin = [] + bin = "./tmp/main" + cmd = "go build -o ./tmp/main ." + delay = 1000 + exclude_dir = ["assets", "tmp", "vendor", "testdata"] + exclude_file = [] + exclude_regex = ["_test.go"] + exclude_unchanged = false + follow_symlink = false + full_bin = "" + include_dir = [] + include_ext = ["go", "tpl", "tmpl", "html"] + include_file = [] + kill_delay = "0s" + log = "build-errors.log" + poll = false + poll_interval = 0 + post_cmd = [] + pre_cmd = [] + rerun = false + rerun_delay = 500 + send_interrupt = false + stop_on_error = false + +[color] + app = "" + build = "yellow" + main = "magenta" + runner = "green" + watcher = "cyan" + +[log] + main_only = false + time = false + +[misc] + clean_on_exit = false + +[proxy] + app_port = 0 + enabled = false + proxy_port = 0 + +[screen] + clear_on_rebuild = false + keep_scroll = true diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..bef2904 --- /dev/null +++ b/.env.example @@ -0,0 +1,2 @@ +ACCOUNT_ID= +LICENSE_KEY= \ No newline at end of file diff --git a/.gitignore b/.gitignore index 8143896..9508e3e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,8 @@ /data/* !/data/README.txt /dist/* -!/dist/README.txt \ No newline at end of file +!/dist/README.txt + +/tmp + +.env \ No newline at end of file diff --git a/fly.toml b/fly.toml index 0657bd3..a6337ba 100644 --- a/fly.toml +++ b/fly.toml @@ -16,10 +16,13 @@ primary_region = 'iad' [http_service] internal_port = 3000 force_https = true - auto_stop_machines = 'stop' + auto_stop_machines = false auto_start_machines = true min_machines_running = 0 processes = ['app'] [[vm]] size = 'shared-cpu-1x' + +[[restart]] + retries = 2 \ No newline at end of file diff --git a/go.mod b/go.mod index ac4b523..e8b6c3a 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( require ( github.com/andybalholm/brotli v1.0.5 // indirect github.com/google/uuid v1.5.0 // indirect + github.com/joho/godotenv v1.5.1 // indirect github.com/klauspost/compress v1.17.0 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect diff --git a/go.sum b/go.sum index c47327e..d24ff27 100644 --- a/go.sum +++ b/go.sum @@ -6,6 +6,8 @@ github.com/gofiber/fiber/v2 v2.52.5 h1:tWoP1MJQjGEe4GB5TUGOi7P2E0ZMMRx5ZTG4rT+yG github.com/gofiber/fiber/v2 v2.52.5/go.mod h1:KEOE+cXMhXG0zHc9d8+E38hoX+ZN7bhOtgeF2oT6jrQ= github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU= github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/klauspost/compress v1.17.0 h1:Rnbp4K9EjcDuVuHtd0dgA4qNuv9yKDYKK1ulpJwgrqM= github.com/klauspost/compress v1.17.0/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= diff --git a/main.go b/main.go index 7c17033..6cb0531 100644 --- a/main.go +++ b/main.go @@ -17,15 +17,22 @@ import ( "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/logger" + "github.com/joho/godotenv" "github.com/oschwald/geoip2-golang" "github.com/robfig/cron/v3" ) func main() { fmt.Println("Initializing GeoLite2 DB...") - err := downloadAndExtractDB() + + _, err := os.Stat("./data/GeoLite2-City.mmdb") + if err != nil { - log.Fatal(err) + fmt.Println("GeoLite2 DB Not Found...") + err := downloadAndExtractDB() + if err != nil { + log.Fatal(err) + } } db, err := geoip2.Open("./data/GeoLite2-City.mmdb") @@ -44,11 +51,18 @@ func main() { }) app.Get("/me", func(c *fiber.Ctx) error { + if len(c.IPs()) == 0 { + return c.JSON(fiber.Map{ + "success": false, + "message": "No IP", + }) + } + ip := c.IPs()[0] record, err := getCity(db, ip) if err != nil { - c.JSON(fiber.Map{ + return c.JSON(fiber.Map{ "success": false, "message": err.Error(), }) @@ -164,10 +178,20 @@ func downloadAndExtractDB() error { return err } + err = os.Remove("./db.tar.gz") + if err != nil { + return err + } + return nil } func downloadDB() error { + err := godotenv.Load() + if err != nil { + fmt.Println("No .env found") + } + accountID := os.Getenv("ACCOUNT_ID") if len(accountID) == 0 { return errors.New("please provide ACCOUNT_ID as env")