| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- package main
- import (
- "context"
- "log/slog"
- "net/http"
- "os"
- "os/signal"
- "syscall"
- "time"
- "cmr-backend/internal/app"
- )
- func main() {
- ctx := context.Background()
- cfg, err := app.LoadConfigFromEnv()
- if err != nil {
- slog.Error("load config failed", "error", err)
- os.Exit(1)
- }
- application, err := app.New(ctx, cfg)
- if err != nil {
- slog.Error("create app failed", "error", err)
- os.Exit(1)
- }
- defer application.Close()
- server := &http.Server{
- Addr: cfg.HTTPAddr,
- Handler: application.Router(),
- ReadHeaderTimeout: 5 * time.Second,
- }
- go func() {
- slog.Info("api server started", "addr", cfg.HTTPAddr)
- if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
- slog.Error("server stopped unexpectedly", "error", err)
- os.Exit(1)
- }
- }()
- stop := make(chan os.Signal, 1)
- signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM)
- <-stop
- shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
- defer cancel()
- if err := server.Shutdown(shutdownCtx); err != nil {
- slog.Error("graceful shutdown failed", "error", err)
- os.Exit(1)
- }
- }
|