package main import ( "context" "errors" "log/slog" "net/http" "os" "os/signal" "subalineado/server/internal/httpapi" "syscall" "time" ) func main() { if err := run(); err != nil { slog.Error("server stopped", "error", err) os.Exit(1) } } func run() error { ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) defer stop() addr := os.Getenv("HTTP_ADDR") if addr == "" { addr = ":8080" } server := &http.Server{ Addr: addr, Handler: httpapi.NewRouter(), ReadHeaderTimeout: 5 * time.Second, ReadTimeout: 10 * time.Second, WriteTimeout: 15 * time.Second, IdleTimeout: 60 * time.Second, } failures := make(chan error, 1) go func() { slog.Info("API listening", "address", addr) failures <- server.ListenAndServe() }() select { case err := <-failures: if errors.Is(err, http.ErrServerClosed) { return nil } return err case <-ctx.Done(): shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() return server.Shutdown(shutdownCtx) } }