feat(keys): role-based gateway keys with admin management UI and per-user model scope

This commit is contained in:
root
2026-08-09 10:01:40 +08:00
parent dec03238dd
commit 3408c9cb1f
10 changed files with 901 additions and 82 deletions

View File

@ -4,6 +4,7 @@ import (
"encoding/json"
"io"
"net/http"
"strconv"
"strings"
"llmsproxy/internal/config"
@ -122,5 +123,15 @@ func (g *Gateway) handleStatsAPI(w http.ResponseWriter, r *http.Request) {
return
}
limit := 500
writeJSON(w, http.StatusOK, g.stats.Snapshot(limit))
if v := r.URL.Query().Get("limit"); v != "" {
if n, err := strconv.Atoi(v); err == nil && n > 0 && n <= 5000 {
limit = n
}
}
key := r.URL.Query().Get("key")
if reqRole(r.Context()) != "admin" {
// user keys may only see their own usage
key = keyID(reqKey(r.Context()))
}
writeJSON(w, http.StatusOK, g.stats.Snapshot(limit, key))
}