Aaron Riedel
9e5c58207c
All checks were successful
continuous-integration/drone/push Build is passing
58 lines
1.4 KiB
Go
58 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
|
|
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"
|
|
"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)
|
|
}()
|
|
}
|