feat(govai): 0617 优化首批 — 安全/私有化/深度研究/服务层/可观测性
借鉴 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 应用迁移并烟测。
This commit is contained in:
@@ -4,7 +4,9 @@ import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/enterprise-ai-platform/server/internal/cache"
|
||||
"github.com/enterprise-ai-platform/server/internal/middleware"
|
||||
"github.com/enterprise-ai-platform/server/internal/response"
|
||||
"github.com/go-chi/chi/v5"
|
||||
@@ -12,15 +14,28 @@ import (
|
||||
)
|
||||
|
||||
type StoreHandler struct {
|
||||
pool *pgxpool.Pool
|
||||
pool *pgxpool.Pool
|
||||
cache cache.Cache
|
||||
}
|
||||
|
||||
func NewStoreHandler(pool *pgxpool.Pool) *StoreHandler {
|
||||
return &StoreHandler{pool: pool}
|
||||
func NewStoreHandler(pool *pgxpool.Pool, c cache.Cache) *StoreHandler {
|
||||
if c == nil {
|
||||
c = cache.NewMemory()
|
||||
}
|
||||
return &StoreHandler{pool: pool, cache: c}
|
||||
}
|
||||
|
||||
// storeListTTL 热点只读列表的缓存时效(短 TTL,避免显式失效的复杂度)。
|
||||
const storeListTTL = 60 * time.Second
|
||||
|
||||
func (h *StoreHandler) ListCategories(w http.ResponseWriter, r *http.Request) {
|
||||
orgID := r.URL.Query().Get("org_id")
|
||||
cacheKey := "store:categories:" + orgID
|
||||
var cached []map[string]any
|
||||
if h.cache.GetJSON(r.Context(), cacheKey, &cached) {
|
||||
response.JSON(w, http.StatusOK, cached)
|
||||
return
|
||||
}
|
||||
query := `SELECT c.id, c.name, c.slug, c.icon, c.description, c.sort_order,
|
||||
COALESCE((SELECT COUNT(*) FROM applications a WHERE a.category_id = c.id AND a.status = 'approved'), 0) AS app_count
|
||||
FROM categories c WHERE c.status = 'active'`
|
||||
@@ -52,6 +67,10 @@ func (h *StoreHandler) ListCategories(w http.ResponseWriter, r *http.Request) {
|
||||
"app_count": appCount,
|
||||
})
|
||||
}
|
||||
if cats == nil {
|
||||
cats = []map[string]any{}
|
||||
}
|
||||
h.cache.SetJSON(r.Context(), cacheKey, cats, storeListTTL)
|
||||
response.JSON(w, http.StatusOK, cats)
|
||||
}
|
||||
|
||||
@@ -242,6 +261,12 @@ func (h *StoreHandler) GetApp(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func (h *StoreHandler) Featured(w http.ResponseWriter, r *http.Request) {
|
||||
orgID := r.URL.Query().Get("org_id")
|
||||
cacheKey := "store:featured:" + orgID
|
||||
var cached []map[string]any
|
||||
if h.cache.GetJSON(r.Context(), cacheKey, &cached) {
|
||||
response.JSON(w, http.StatusOK, cached)
|
||||
return
|
||||
}
|
||||
query := `
|
||||
SELECT a.id, a.name, a.slug, a.description, a.icon_url,
|
||||
c.name as category_name, c.slug as category_slug,
|
||||
@@ -264,11 +289,18 @@ func (h *StoreHandler) Featured(w http.ResponseWriter, r *http.Request) {
|
||||
defer rows.Close()
|
||||
|
||||
apps := scanAppList(rows)
|
||||
h.cache.SetJSON(r.Context(), cacheKey, apps, storeListTTL)
|
||||
response.JSON(w, http.StatusOK, apps)
|
||||
}
|
||||
|
||||
func (h *StoreHandler) Rankings(w http.ResponseWriter, r *http.Request) {
|
||||
orgID := r.URL.Query().Get("org_id")
|
||||
cacheKey := "store:rankings:" + orgID
|
||||
var cached []map[string]any
|
||||
if h.cache.GetJSON(r.Context(), cacheKey, &cached) {
|
||||
response.JSON(w, http.StatusOK, cached)
|
||||
return
|
||||
}
|
||||
query := `
|
||||
SELECT a.id, a.name, a.slug, a.description, a.icon_url,
|
||||
c.name as category_name, c.slug as category_slug,
|
||||
@@ -291,6 +323,7 @@ func (h *StoreHandler) Rankings(w http.ResponseWriter, r *http.Request) {
|
||||
defer rows.Close()
|
||||
|
||||
apps := scanAppList(rows)
|
||||
h.cache.SetJSON(r.Context(), cacheKey, apps, storeListTTL)
|
||||
response.JSON(w, http.StatusOK, apps)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user