2024-01-15 14:30:13 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-01-16 20:44:20 +02:00
|
|
|
"log/slog"
|
|
|
|
"os"
|
2024-01-23 12:22:16 +02:00
|
|
|
"os/signal"
|
2024-01-25 14:35:04 +02:00
|
|
|
"recipes/internal/app"
|
2024-01-15 14:30:13 +02:00
|
|
|
"recipes/internal/config"
|
2024-01-23 12:22:16 +02:00
|
|
|
"syscall"
|
2024-01-16 20:44:20 +02:00
|
|
|
|
|
|
|
prettyLogger "github.com/charmbracelet/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
envLocal = "local"
|
|
|
|
envDev = "dev"
|
|
|
|
envProd = "prod"
|
2024-01-15 14:30:13 +02:00
|
|
|
)
|
|
|
|
|
2024-01-23 12:22:16 +02:00
|
|
|
//TODO
|
|
|
|
// cache
|
|
|
|
// app
|
|
|
|
// graceful sd
|
|
|
|
// tests
|
|
|
|
|
2024-01-15 14:30:13 +02:00
|
|
|
func main() {
|
|
|
|
// load config
|
|
|
|
cfg := config.MustLoad()
|
2024-01-16 20:44:20 +02:00
|
|
|
// init logger
|
|
|
|
log := setupLogger(cfg.Env)
|
|
|
|
log.Info("starting application", slog.String("env", cfg.Env))
|
|
|
|
log.Debug("debug messages are enabled")
|
|
|
|
log.Debug("Application config", slog.Any("config", fmt.Sprintf("%+v", *cfg)))
|
|
|
|
|
2024-01-23 12:22:16 +02:00
|
|
|
// init app (storage, cache, media storage)
|
2024-01-25 14:35:04 +02:00
|
|
|
application := app.New(log, cfg)
|
|
|
|
// start app
|
|
|
|
go application.HTTPSrv.MustRun()
|
2024-01-18 18:48:27 +02:00
|
|
|
|
2024-01-23 12:22:16 +02:00
|
|
|
// graceful shutdown
|
|
|
|
stop := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(stop, syscall.SIGTERM, syscall.SIGINT)
|
2024-01-18 18:48:27 +02:00
|
|
|
|
2024-01-23 12:22:16 +02:00
|
|
|
<-stop
|
2024-01-16 20:44:20 +02:00
|
|
|
|
2024-01-23 12:22:16 +02:00
|
|
|
log.Info("stopping application...")
|
|
|
|
|
2024-01-25 14:35:04 +02:00
|
|
|
application.HTTPSrv.Stop()
|
2024-01-23 12:22:16 +02:00
|
|
|
|
|
|
|
log.Info("application stopped")
|
2024-01-16 20:44:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func setupLogger(env string) *slog.Logger {
|
|
|
|
var log *slog.Logger
|
|
|
|
|
|
|
|
switch env {
|
|
|
|
case envLocal:
|
|
|
|
// log = slog.New(
|
|
|
|
// slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}),
|
|
|
|
// )
|
|
|
|
handler := prettyLogger.NewWithOptions(os.Stdout, prettyLogger.Options{Level: prettyLogger.DebugLevel})
|
|
|
|
log = slog.New(handler)
|
|
|
|
case envDev:
|
|
|
|
log = slog.New(
|
|
|
|
slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}),
|
|
|
|
)
|
|
|
|
case envProd:
|
|
|
|
log = slog.New(
|
|
|
|
slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo}),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return log
|
2024-01-15 14:30:13 +02:00
|
|
|
}
|