better db file download + use air for development + added optional env file loading

main
TZGyn 1 year ago
parent 74c5a1b70f
commit 1233fe0e95
Signed by: TZGyn
GPG Key ID: 122EAF77AE81FD4A

@ -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

@ -0,0 +1,2 @@
ACCOUNT_ID=
LICENSE_KEY=

6
.gitignore vendored

@ -1,4 +1,8 @@
/data/*
!/data/README.txt
/dist/*
!/dist/README.txt
!/dist/README.txt
/tmp
.env

@ -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

@ -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

@ -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=

@ -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")

Loading…
Cancel
Save