把 health.html 从外部依赖改为 //go:embed 内嵌, 实现真正的"单 exe 分发"。 - 新建 router/dashboard.go,用 //go:embed 嵌入 health.html - router/router.go:/dashboard handler 改为 w.Write(dashboardHTML) - setting/config.go:删 CheckStaticFiles(无需文件检查) - main.go:删 setting.CheckStaticFiles() 调用 - Dockerfile:删 COPY health.html 步骤(已嵌入) - .gitignore:加 release/(build 产物不入 git) - 物理移动 health.html 到 router/(embed 限制:同包或子包) 效果: - 之前: release 包 = tts-api.exe + health.html (2 个文件) - 之后: release 包 = tts-api.exe (1 个文件,~7 MB) - 用户部署无需额外文件,/dashboard 路由直接工作
36 lines
1.1 KiB
Go
36 lines
1.1 KiB
Go
package router
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gorilla/mux"
|
|
"github.com/volcano-tts/tts-api/controller"
|
|
"github.com/volcano-tts/tts-api/metrics"
|
|
"github.com/volcano-tts/tts-api/middleware"
|
|
)
|
|
|
|
func Setup() *mux.Router {
|
|
r := mux.NewRouter()
|
|
|
|
r.Use(middleware.SecurityHeaders)
|
|
r.Use(middleware.RateLimitWithMetrics)
|
|
r.Use(middleware.ConcurrencyLimitWithMetrics)
|
|
r.Use(middleware.Logger)
|
|
|
|
r.HandleFunc("/v1/audio/speech", controller.OpenaiTTSHandler).Methods("POST", "OPTIONS")
|
|
r.HandleFunc("/health", controller.HealthHandler).Methods("GET")
|
|
r.HandleFunc("/dashboard", func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
w.Write(dashboardHTML)
|
|
}).Methods("GET")
|
|
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
http.Redirect(w, r, "/dashboard", http.StatusFound)
|
|
}).Methods("GET")
|
|
|
|
// /metrics 不做鉴权(对齐 /health 策略),但仍然走 RateLimit / ConcurrencyLimit。
|
|
// Prometheus 抓取不带 Origin,因此经过 CORS 中间件时会直接 pass-through。
|
|
r.Handle("/metrics", metrics.Meter.Handler()).Methods("GET")
|
|
|
|
return r
|
|
}
|