Files
mtusi2026lab2/service/cmd/app/main.go
T

70 lines
1.6 KiB
Go

package main
import (
"context"
"fmt"
"log/slog"
"os"
"os/signal"
"syscall"
"gitea.privcloud.tech/mtusi2026/internal/adapter/controller/http"
inmemory2 "gitea.privcloud.tech/mtusi2026/internal/adapter/repository/cache/inmemory"
"gitea.privcloud.tech/mtusi2026/internal/config"
)
func main() {
if err := run(); err != nil {
slog.Error("application stopped", "error", err)
os.Exit(1)
}
}
func run() error {
cfg, err := config.LoadServer()
if err != nil {
return fmt.Errorf("load server config: %w", err)
}
router := http.NewRouter()
userCache := inmemory2.NewUserCache()
chatCache := inmemory2.NewChatCache(userCache)
messageCache := inmemory2.NewMessageCache(chatCache)
server := http.New(cfg, router)
baseHandler := http.NewBaseHandler(router)
healthHandler := http.NewHealthHandler(baseHandler, server)
chatHandler := http.NewChatHandler(baseHandler, userCache, chatCache, messageCache)
userHandler := http.NewUserHandler(baseHandler, userCache)
registerRoutes(
healthHandler,
chatHandler,
userHandler,
)
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
if err := server.Start(ctx); err != nil {
return fmt.Errorf("start HTTP server: %w", err)
}
slog.Info("HTTP server started", "address", cfg.BindAddress())
<-ctx.Done()
shutdownCtx, cancel := context.WithTimeout(context.Background(), cfg.ShutdownTimeout)
defer cancel()
if err := server.Stop(shutdownCtx); err != nil {
return fmt.Errorf("stop HTTP server: %w", err)
}
return nil
}
func registerRoutes(handlers ...http.Handler) {
for _, handler := range handlers {
handler.RegisterRoutes()
}
}