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() {
|
|
|
|
appSettings := cfg.Parse()
|
|
|
|
handleGracefulShutdown()
|
|
|
|
if !downloader.FileExists(appSettings.DataFile) {
|
|
|
|
downloader.DownloadFile(appSettings.DataFile, appSettings.DataURL)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}()
|
|
|
|
}
|