Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> # Conflicts: # api/cmd/admin/main.go # api/cmd/server/main.go
94 lines
2.7 KiB
Go
94 lines
2.7 KiB
Go
// Command server starts the OpenGoods public read-only API.
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
"github.com/baicai2026-baicai/goods/api/internal/cache"
|
|
"github.com/baicai2026-baicai/goods/api/internal/clientip"
|
|
"github.com/baicai2026-baicai/goods/api/internal/config"
|
|
"github.com/baicai2026-baicai/goods/api/internal/handler"
|
|
"github.com/baicai2026-baicai/goods/api/internal/publicweb"
|
|
"github.com/baicai2026-baicai/goods/api/internal/ratelimit"
|
|
"github.com/baicai2026-baicai/goods/api/internal/store"
|
|
)
|
|
|
|
func main() {
|
|
cfg := config.Load()
|
|
clientIPs, err := clientip.Parse(cfg.TrustedProxies)
|
|
if err != nil {
|
|
log.Fatalf("invalid OPENGOODS_TRUSTED_PROXIES: %v", err)
|
|
}
|
|
runCtx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
|
defer stop()
|
|
|
|
pool, err := pgxpool.New(context.Background(), cfg.DatabaseURL)
|
|
if err != nil {
|
|
log.Fatalf("failed to create db pool: %v", err)
|
|
}
|
|
|
|
pingCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
if err := pool.Ping(pingCtx); err != nil {
|
|
log.Printf("warning: database not reachable at startup: %v", err)
|
|
}
|
|
|
|
limiter := ratelimit.New(cfg.RedisURL)
|
|
if !limiter.Enabled() {
|
|
log.Print("warning: Redis not configured; public API rate limiting disabled")
|
|
}
|
|
|
|
readCache := cache.New(cfg.RedisURL)
|
|
if !readCache.Enabled() {
|
|
log.Print("warning: Redis not configured; public API read cache disabled")
|
|
}
|
|
h := handler.New(store.New(pool).WithCache(readCache), publicweb.Dist()).
|
|
WithRateLimit(limiter, cfg.AnonRateLimitPerMin).
|
|
WithQuotas(cfg.AnonTotalQuota, cfg.RegisteredRateLimitPerMin, cfg.RegisteredQuotaTotal).
|
|
WithClientIP(clientIPs)
|
|
|
|
srv := &http.Server{
|
|
Addr: cfg.Addr,
|
|
Handler: h.Router(),
|
|
ReadHeaderTimeout: 10 * time.Second,
|
|
ReadTimeout: 60 * time.Second,
|
|
WriteTimeout: 60 * time.Second,
|
|
IdleTimeout: 120 * time.Second,
|
|
}
|
|
|
|
log.Printf("OpenGoods API listening on %s", cfg.Addr)
|
|
serverErr := make(chan error, 1)
|
|
go func() {
|
|
serverErr <- srv.ListenAndServe()
|
|
}()
|
|
|
|
select {
|
|
case err := <-serverErr:
|
|
pool.Close()
|
|
if err != nil && err != http.ErrServerClosed {
|
|
log.Fatalf("server error: %v", err)
|
|
}
|
|
case <-runCtx.Done():
|
|
log.Print("shutdown signal received")
|
|
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 25*time.Second)
|
|
err := srv.Shutdown(shutdownCtx)
|
|
shutdownCancel()
|
|
if err != nil {
|
|
log.Printf("server shutdown: %v", err)
|
|
}
|
|
pool.Close()
|
|
if err := <-serverErr; err != nil && err != http.ErrServerClosed {
|
|
log.Printf("server error after shutdown: %v", err)
|
|
}
|
|
log.Print("OpenGoods API shutdown complete")
|
|
}
|
|
}
|