// Package classification JNX Log Manager API Documentation.
//
// Schemes: https
// Host: jannex
// BasePath: /v1
// Version: 1.0.0
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// swagger:meta
package main

import (
	"jannex/log-manager/modules/config"
	"jannex/log-manager/modules/loghandler"
	"jannex/log-manager/modules/utils"
	"jannex/log-manager/routers/router"

	"git.ex.umbach.dev/Alex/roese-utils/rsconfig"
	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/fiber/v2/middleware/cors"
	"github.com/gofiber/fiber/v2/middleware/logger"
)

func init() {
	rsconfig.CreateEnvConfigFileIfNotExists(`DEBUG=false
HOST=127.0.0.1
PORT=50110

SSE_SERVER_ENABLED=true

LOG_FOLDER=./logs/

# this determines how long the logs should be keept on the server after they will be deleted
DAYS_TO_KEEP_LOGS=30`)

	config.LoadConfig()
	utils.ValidatorInit()

	utils.CreateDirectoryIfNotExists(config.Cfg.LogFolder)

	// checking for deletable logs on startup
	loghandler.CheckForDeletableLogs()

	// starting the background log deleter
	go loghandler.StartBackgroundLogDeleter()
}

func main() {
	app := fiber.New(fiber.Config{
		BodyLimit: 100 * 1024 * 1024,
	})

	app.Use(cors.New(cors.Config{}))

	if config.Cfg.Debug {
		app.Use(logger.New(logger.Config{
			Format: "${pid} ${locals:requestid} ${status} - ${latency} ${method} ${path}​\n",
		}))
	}

	router.SetupRoutes(app)

	app.Listen(config.Cfg.Host + ":" + config.Cfg.Port)
}
