65 lines
2.2 KiB
Go
65 lines
2.2 KiB
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
managementHandlers "github.com/router-for-me/CLIProxyAPI/v7/internal/api/handlers/management"
|
|
"github.com/router-for-me/CLIProxyAPI/v7/sdk/api/handlers/openai"
|
|
)
|
|
|
|
const oauthCallbackSuccessHTML = `<html><head><meta charset="utf-8"><title>Authentication successful</title><script>setTimeout(function(){window.close();},5000);</script></head><body><h1>Authentication successful!</h1><p>You can close this window.</p><p>This window will close automatically in 5 seconds.</p></body></html>`
|
|
|
|
func (s *Server) setupRoutes() {
|
|
healthzHandler := func(c *gin.Context) {
|
|
if c.Request.Method == http.MethodHead {
|
|
c.Status(http.StatusOK)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"status": "ok"})
|
|
}
|
|
s.engine.GET("/healthz", healthzHandler)
|
|
s.engine.HEAD("/healthz", healthzHandler)
|
|
|
|
s.engine.GET("/management.html", s.serveManagementControlPanel)
|
|
s.engine.HEAD("/management.html", s.serveManagementControlPanel)
|
|
s.engine.GET("/management-assets/*filepath", s.serveManagementAsset)
|
|
s.engine.HEAD("/management-assets/*filepath", s.serveManagementAsset)
|
|
|
|
openAIHandlers := openai.NewOpenAIAPIHandler(s.handlers)
|
|
responsesHandlers := openai.NewOpenAIResponsesAPIHandler(s.handlers)
|
|
v1 := s.engine.Group("/v1")
|
|
v1.Use(AuthMiddleware(s.accessManager))
|
|
{
|
|
v1.GET("/models", openAIHandlers.OpenAIModels)
|
|
v1.POST("/chat/completions", openAIHandlers.ChatCompletions)
|
|
v1.GET("/responses", responsesHandlers.ResponsesWebsocket)
|
|
v1.POST("/responses", responsesHandlers.Responses)
|
|
}
|
|
|
|
s.engine.GET("/", func(c *gin.Context) {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"message": "Vibe Proxy",
|
|
"endpoints": []string{
|
|
"GET /v1/models",
|
|
"POST /v1/chat/completions",
|
|
"GET /v1/responses",
|
|
"POST /v1/responses",
|
|
},
|
|
})
|
|
})
|
|
|
|
s.engine.GET("/codex/callback", func(c *gin.Context) {
|
|
code := c.Query("code")
|
|
state := c.Query("state")
|
|
errStr := c.Query("error")
|
|
if errStr == "" {
|
|
errStr = c.Query("error_description")
|
|
}
|
|
if state != "" {
|
|
_, _ = managementHandlers.WriteOAuthCallbackFileForPendingSession(s.cfg.AuthDir, "codex", state, code, errStr)
|
|
}
|
|
c.Header("Content-Type", "text/html; charset=utf-8")
|
|
c.String(http.StatusOK, oauthCallbackSuccessHTML)
|
|
})
|
|
}
|