You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.4 KiB

package main
import (
"context"
"errors"
"log/slog"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"juegoland/games2/internal/api/httpapi"
"juegoland/games2/internal/domain/catalog"
"juegoland/games2/internal/server"
)
func main() {
logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
address := os.Getenv("GAMES2_API_ADDR")
if address == "" {
address = "127.0.0.1:8082"
}
// The first release includes no games. A game appears only when its rules,
// assets and publication record are installed and validated together.
registry := catalog.NewRegistry(nil)
service := server.NewService(registry, time.Now)
httpServer := &http.Server{
Addr: address,
Handler: httpapi.NewHandler(service),
ReadHeaderTimeout: 5 * time.Second,
IdleTimeout: 60 * time.Second,
MaxHeaderBytes: 1 << 20,
}
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
go func() {
<-ctx.Done()
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := httpServer.Shutdown(shutdownCtx); err != nil {
logger.Error("graceful shutdown failed", "error", err)
}
}()
logger.Info("games2 API listening", "address", address)
if err := httpServer.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
logger.Error("API stopped", "error", err)
os.Exit(1)
}
}

Powered by TurnKey Linux.