修复 Gemini 兼容路由启动失败
This commit is contained in:
@@ -38,6 +38,42 @@ type geminiUploadSession struct {
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
var geminiGenerateContentRoutePrefixes = []string{
|
||||
"/v1beta/models/",
|
||||
"/v1/models/",
|
||||
"/models/",
|
||||
}
|
||||
|
||||
func (s *Server) registerGeminiGenerateContentRoutes(mux *http.ServeMux) {
|
||||
handler := s.auth.Require(auth.PermissionBasic, http.HandlerFunc(s.geminiGenerateContent))
|
||||
for _, prefix := range geminiGenerateContentRoutePrefixes {
|
||||
mux.Handle("POST "+prefix, geminiGenerateContentRouteHandler(prefix, handler))
|
||||
}
|
||||
}
|
||||
|
||||
func geminiGenerateContentRouteHandler(prefix string, next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
model, ok := geminiGenerateContentModelFromPath(prefix, r.URL.Path)
|
||||
if !ok {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
r.SetPathValue("model", model)
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
func geminiGenerateContentModelFromPath(prefix string, requestPath string) (string, bool) {
|
||||
if !strings.HasPrefix(requestPath, prefix) {
|
||||
return "", false
|
||||
}
|
||||
model, ok := strings.CutSuffix(strings.TrimPrefix(requestPath, prefix), ":generateContent")
|
||||
if !ok || strings.TrimSpace(model) == "" || strings.Contains(model, "/") {
|
||||
return "", false
|
||||
}
|
||||
return model, true
|
||||
}
|
||||
|
||||
func (s *Server) geminiGenerateContent(w http.ResponseWriter, r *http.Request) {
|
||||
user, ok := auth.UserFromContext(r.Context())
|
||||
if !ok {
|
||||
|
||||
Reference in New Issue
Block a user