country-geo-locations/main.go

68 lines
1.5 KiB
Go
Raw Normal View History

2023-12-19 13:56:03 +01:00
package main
import (
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
2023-12-23 18:22:37 +01:00
api_v1 "git.ar21.de/yolokube/country-geo-locations/api/v1"
"git.ar21.de/yolokube/country-geo-locations/cfg"
"git.ar21.de/yolokube/country-geo-locations/pkg/downloader"
"git.ar21.de/yolokube/country-geo-locations/pkg/geoloc"
2023-12-19 13:56:03 +01:00
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/render"
)
func main() {
2024-03-02 10:29:59 +01:00
appSettings := cfg.NewAppSettings()
if err := appSettings.Parse(); err != nil {
panic(err)
}
2023-12-19 13:56:03 +01:00
handleGracefulShutdown()
2024-03-02 10:55:27 +01:00
ctx := downloader.NewContext(appSettings.DataFile, appSettings.DataURL)
if !ctx.FileExists() {
if err := ctx.Download(); err != nil {
panic(err)
}
fmt.Printf("Saved file to %s\n", ctx.Filename)
2023-12-19 13:56:03 +01:00
}
fmt.Printf("Import data from file...\r")
err := geoloc.ImportCSV(appSettings.DataFile)
if err != nil {
fmt.Println("Import data from file failed")
panic(err)
}
fmt.Println("Import data from file successful")
r := chi.NewRouter()
r.Use(middleware.RequestID)
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
r.Use(render.SetContentType(render.ContentTypeJSON))
r.Mount("/api/v1", api_v1.ApiRouter())
log.Printf("starting server at %s\n", appSettings.ServerAddress)
http.ListenAndServe(appSettings.ServerAddress, r)
}
func handleGracefulShutdown() {
var signals = make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGTERM)
signal.Notify(signals, syscall.SIGINT)
go func() {
sig := <-signals
log.Printf("caught signal: %+v", sig)
os.Exit(0)
}()
}