2026-08-15 14:38:14 +08:00
|
|
|
package router
|
2026-05-23 20:32:12 +08:00
|
|
|
|
|
|
|
|
import (
|
2026-08-29 20:44:33 +08:00
|
|
|
_ "embed"
|
2026-05-23 20:32:12 +08:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
|
"github.com/volcano-tts/tts-api/controller"
|
2026-08-29 20:44:33 +08:00
|
|
|
"github.com/volcano-tts/tts-api/installer"
|
2026-08-15 13:30:35 +08:00
|
|
|
"github.com/volcano-tts/tts-api/metrics"
|
2026-05-23 20:32:12 +08:00
|
|
|
"github.com/volcano-tts/tts-api/middleware"
|
|
|
|
|
)
|
|
|
|
|
|
2026-08-29 20:44:33 +08:00
|
|
|
//go:embed health.html
|
|
|
|
|
var dashboardHTML []byte
|
|
|
|
|
|
|
|
|
|
//go:embed setup.html
|
|
|
|
|
var setupHTML []byte
|
|
|
|
|
|
|
|
|
|
// Setup 返回主路由。
|
|
|
|
|
// 中间件顺序(由外向内):
|
|
|
|
|
// SecurityHeaders → InstallGuard → RateLimit → ConcurrencyLimit → Logger → handler
|
|
|
|
|
// 关键: InstallGuard 必须在 RateLimit 之前,避免安装模式被限流计数污染。
|
2026-05-23 20:32:12 +08:00
|
|
|
func Setup() *mux.Router {
|
|
|
|
|
r := mux.NewRouter()
|
|
|
|
|
|
2026-05-24 15:33:27 +08:00
|
|
|
r.Use(middleware.SecurityHeaders)
|
2026-08-29 20:44:33 +08:00
|
|
|
r.Use(middleware.InstallGuard(installer.GetMode))
|
2026-08-15 13:30:35 +08:00
|
|
|
r.Use(middleware.RateLimitWithMetrics)
|
|
|
|
|
r.Use(middleware.ConcurrencyLimitWithMetrics)
|
2026-05-23 20:32:12 +08:00
|
|
|
r.Use(middleware.Logger)
|
|
|
|
|
|
2026-08-29 20:44:33 +08:00
|
|
|
// 安装相关路由(InstallGuard 已在 setup 模式放行;完成后由 controller 二次校验 404)
|
|
|
|
|
r.HandleFunc("/setup", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
|
|
|
_, _ = w.Write(setupHTML)
|
|
|
|
|
}).Methods("GET")
|
|
|
|
|
r.HandleFunc("/api/setup/status", controller.SetupStatusHandler).Methods("GET")
|
|
|
|
|
r.HandleFunc("/api/setup/prefill", controller.SetupPrefillHandler).Methods("GET")
|
|
|
|
|
r.HandleFunc("/api/setup", controller.SetupSubmitHandler).Methods("POST")
|
|
|
|
|
|
|
|
|
|
// 业务路由
|
2026-05-23 20:32:12 +08:00
|
|
|
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) {
|
2026-08-27 11:44:44 +08:00
|
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
2026-08-29 20:44:33 +08:00
|
|
|
_, _ = w.Write(dashboardHTML)
|
2026-05-23 20:32:12 +08:00
|
|
|
}).Methods("GET")
|
|
|
|
|
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
2026-08-29 20:44:33 +08:00
|
|
|
// 安装模式下,根路径跳 /setup(给运维一个明显入口)
|
|
|
|
|
if installer.GetMode() == installer.ModeSetup {
|
|
|
|
|
http.Redirect(w, r, "/setup", http.StatusFound)
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-05-23 20:32:12 +08:00
|
|
|
http.Redirect(w, r, "/dashboard", http.StatusFound)
|
|
|
|
|
}).Methods("GET")
|
|
|
|
|
|
2026-08-15 13:30:35 +08:00
|
|
|
// /metrics 不做鉴权(对齐 /health 策略),但仍然走 RateLimit / ConcurrencyLimit。
|
|
|
|
|
// Prometheus 抓取不带 Origin,因此经过 CORS 中间件时会直接 pass-through。
|
|
|
|
|
r.Handle("/metrics", metrics.Meter.Handler()).Methods("GET")
|
|
|
|
|
|
2026-05-23 20:32:12 +08:00
|
|
|
return r
|
|
|
|
|
}
|