package main

import (
	"github.com/joho/godotenv"
	"github.com/rs/zerolog"
	"github.com/rs/zerolog/log"
	"os"
	"os/signal"
	"thuanle.me/ip-info/internal/data"
	"thuanle.me/ip-info/internal/services/api"
	"thuanle.me/ip-info/internal/services/db_updater"
	"time"
)

func main() {
	zerolog.TimeFieldFormat = "06/01/02 15:04:05"
	zerolog.DurationFieldInteger = true
	zerolog.DurationFieldUnit = time.Second

	err := godotenv.Load()
	if err != nil {
		log.Err(err).Msg("Error loading .env file")
	}

	err = data.CleanupDataDir()
	if err != nil {
		log.Err(err).Msg("Failed to clean temp files")
	}

	stop := make(chan os.Signal, 1)
	signal.Notify(stop, os.Interrupt)

	go db_updater.StartUpdateDbService()
	go api.StartApiService()

	<-stop

	log.Info().Msg("Shutting down...")
	api.Shutdown()
}
