Merge remote-tracking branch 'origin/main' into devin/1793408086-goods-46-graceful-shutdown

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
This commit is contained in:
z8jd98
2026-10-31 01:39:49 +00:00
35 changed files with 1337 additions and 141 deletions
+27 -3
View File
@@ -9,6 +9,7 @@ import (
"net/http"
"os"
"os/signal"
"strconv"
"syscall"
"time"
@@ -19,6 +20,8 @@ import (
"github.com/baicai2026-baicai/goods/api/internal/adminstore"
"github.com/baicai2026-baicai/goods/api/internal/adminweb"
"github.com/baicai2026-baicai/goods/api/internal/auth"
"github.com/baicai2026-baicai/goods/api/internal/cache"
"github.com/baicai2026-baicai/goods/api/internal/clientip"
"github.com/baicai2026-baicai/goods/api/internal/ratelimit"
)
@@ -34,6 +37,22 @@ func main() {
dbURL := getenv("OPENGOODS_DATABASE_URL", "postgres://opengoods:opengoods@localhost:5432/opengoods?sslmode=disable")
basePath := getenv("GOODS_ADMIN_BASE_PATH", "/ping")
username := getenv("GOODS_ADMIN_USER", "admin")
backflowRate, err := strconv.Atoi(getenv("GOODS_BACKFLOW_RATE_PER_HOUR", "30"))
if err != nil || backflowRate < 1 {
log.Fatalf("invalid GOODS_BACKFLOW_RATE_PER_HOUR: must be a positive integer")
}
tokenTTL := 12 * time.Hour
if value := os.Getenv("GOODS_ADMIN_TOKEN_TTL"); value != "" {
parsed, err := time.ParseDuration(value)
if err != nil || parsed <= 0 {
log.Fatalf("invalid GOODS_ADMIN_TOKEN_TTL %q: expected a positive duration", value)
}
tokenTTL = parsed
}
clientIPs, err := clientip.Parse(getenv("OPENGOODS_TRUSTED_PROXIES", ""))
if err != nil {
log.Fatalf("invalid OPENGOODS_TRUSTED_PROXIES: %v", err)
}
runCtx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
@@ -71,10 +90,15 @@ func main() {
log.Printf("warning: database not reachable at startup: %v", err)
}
authn := auth.New(username, passwordHash, secret, 12*time.Hour)
usage := ratelimit.New(getenv("OPENGOODS_REDIS_URL", "redis://localhost:6379/0"))
authn := auth.New(username, passwordHash, secret, tokenTTL)
redisURL := getenv("OPENGOODS_REDIS_URL", "redis://localhost:6379/0")
usage := ratelimit.New(redisURL)
readCache := cache.New(redisURL)
h := adminhandler.New(adminstore.New(pool), authn, basePath, adminweb.Dist()).
WithUsage(usage)
WithUsage(usage).
WithBackflowRate(backflowRate).
WithCache(readCache).
WithClientIP(clientIPs)
srv := &http.Server{
Addr: addr,
+7 -1
View File
@@ -13,6 +13,7 @@ import (
"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"
@@ -22,6 +23,10 @@ import (
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()
@@ -47,7 +52,8 @@ func main() {
}
h := handler.New(store.New(pool).WithCache(readCache), publicweb.Dist()).
WithRateLimit(limiter, cfg.AnonRateLimitPerMin).
WithQuotas(cfg.AnonTotalQuota, cfg.RegisteredRateLimitPerMin, cfg.RegisteredQuotaTotal)
WithQuotas(cfg.AnonTotalQuota, cfg.RegisteredRateLimitPerMin, cfg.RegisteredQuotaTotal).
WithClientIP(clientIPs)
srv := &http.Server{
Addr: cfg.Addr,