Add backend foundation and config-driven workbench

This commit is contained in:
2026-04-01 15:01:44 +08:00
parent 88b8f05f03
commit 94a1f0ba78
68 changed files with 10833 additions and 0 deletions

55
backend/cmd/api/main.go Normal file
View File

@@ -0,0 +1,55 @@
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)
}
}