c949204662
借鉴 odysseus 的能力设计,全程净室实现、零 AGPL 代码、不引入 AGPL 依赖。
T1 提示注入防护: pkg/promptguard 包裹外部/知识库内容为不可信数据,buildMessages 移出 system 指令区。 T2 安全 CI: .github/workflows(ci+security: govulncheck/gitleaks/actionlint/hadolint/trivy)+dependabot+.hadolint.yaml;go.mod 加 toolchain go1.25.11 修复 20 个 stdlib CVE。 T3 管理员 2FA: 迁移 000016 + RFC6238 TOTP/备份码(pkg/auth, 零依赖) + 登录流程集成(后端)。 T4 本地模型: LLM/embedding 支持本地 vLLM/Ollama(OpenAI 兼容, 鉴权头条件发送, NoAuth) + docs/local-deploy.md。 T6 深度研究: 迁移 000017 + Python research-worker(净室多步流水线, 检索避开 SearXNG) + Go research 服务/handler/路由。 T7 service 层: 新增 internal/service/{research,twofa}, 2FA 业务逻辑从胖 handler 下沉, 接口注入可单测。 T10 缓存/可观测性: internal/cache(Redis+内存, 优雅降级) 接入 store 热点列表; Prometheus 指标+/metrics; docs/openapi.yaml。 验证: go build/vet/test ./... 全绿(8 包); research-worker 12 单测过; 真实 PG 应用迁移并烟测。
53 lines
1.5 KiB
Go
53 lines
1.5 KiB
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
chimw "github.com/go-chi/chi/v5/middleware"
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
|
)
|
|
|
|
var (
|
|
httpRequestsTotal = promauto.NewCounterVec(
|
|
prometheus.CounterOpts{
|
|
Name: "govai_http_requests_total",
|
|
Help: "HTTP 请求总数,按方法、路由模板、状态码统计。",
|
|
},
|
|
[]string{"method", "route", "status"},
|
|
)
|
|
httpRequestDuration = promauto.NewHistogramVec(
|
|
prometheus.HistogramOpts{
|
|
Name: "govai_http_request_duration_seconds",
|
|
Help: "HTTP 请求耗时(秒),按方法与路由模板统计。",
|
|
Buckets: prometheus.DefBuckets,
|
|
},
|
|
[]string{"method", "route"},
|
|
)
|
|
)
|
|
|
|
// Metrics 是记录 Prometheus 指标的全局中间件。
|
|
// 使用 chi 的路由模板(而非原始路径)作为标签,避免高基数。
|
|
func Metrics(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
start := time.Now()
|
|
ww := chimw.NewWrapResponseWriter(w, r.ProtoMajor)
|
|
|
|
next.ServeHTTP(ww, r)
|
|
|
|
route := chi.RouteContext(r.Context()).RoutePattern()
|
|
if route == "" {
|
|
route = "unmatched"
|
|
}
|
|
status := ww.Status()
|
|
if status == 0 {
|
|
status = http.StatusOK
|
|
}
|
|
httpRequestsTotal.WithLabelValues(r.Method, route, strconv.Itoa(status)).Inc()
|
|
httpRequestDuration.WithLabelValues(r.Method, route).Observe(time.Since(start).Seconds())
|
|
})
|
|
}
|