fix(api): 限制回流频率并隐藏后台错误详情 (#43)

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
fu500f
2026-10-31 00:52:20 +00:00
co-authored by Devin AI
parent 36f9c95fa7
commit 8a29423451
7 changed files with 200 additions and 57 deletions
+7 -1
View File
@@ -8,6 +8,7 @@ import (
"log"
"net/http"
"os"
"strconv"
"time"
"github.com/jackc/pgx/v5/pgxpool"
@@ -32,6 +33,10 @@ 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")
}
// Password: prefer a bcrypt hash; otherwise hash a plaintext password.
var passwordHash []byte
@@ -72,7 +77,8 @@ func main() {
authn := auth.New(username, passwordHash, secret, 12*time.Hour)
usage := ratelimit.New(getenv("OPENGOODS_REDIS_URL", "redis://localhost:6379/0"))
h := adminhandler.New(adminstore.New(pool), authn, basePath, adminweb.Dist()).
WithUsage(usage)
WithUsage(usage).
WithBackflowRate(backflowRate)
srv := &http.Server{
Addr: addr,