dapi / initialize /router.go
sanbo
update sth. at 2024-12-24 12:41:40
8fb5936
package initialize
import (
"aurora/middlewares"
"net/http"
"os"
"github.com/gin-gonic/gin"
)
func RegisterRouter() *gin.Engine {
handler := NewHandle(
checkProxy(),
)
router := gin.Default()
router.Use(middlewares.Cors)
// // 访问根目录时,跳转到 /web
// router.GET("/", func(c *gin.Context) {
// c.JSON(200, gin.H{
// "message": "Hello, world!",
// })
// })
router.GET("/", func(c *gin.Context) {
c.Redirect(http.StatusFound, "/web")
})
router.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
// 注册 prefixGroup 路由
prefixGroup := os.Getenv("PREFIX")
if prefixGroup != "" {
registerGroupRoutes(router.Group(prefixGroup), handler)
}
// 注册无前缀的路由
registerGroupRoutes(router.Group(""), handler)
return router
}
func registerGroupRoutes(group *gin.RouterGroup, handler *Handler) {
// OPTIONS 路由
group.OPTIONS("/v1/chat/completions", optionsHandler)
group.OPTIONS("/v1/chat/models", optionsHandler)
group.OPTIONS("/completions", optionsHandler)
group.OPTIONS("/models", optionsHandler)
group.OPTIONS("/api/v1/chat/completions", optionsHandler)
group.OPTIONS("/api/v1/models", optionsHandler)
group.OPTIONS("/hf/v1/chat/completions", optionsHandler)
group.OPTIONS("/hf/v1/models", optionsHandler)
// POST 路由
group.POST("/v1/chat/completions", middlewares.Authorization, handler.duckduckgo)
group.POST("/api/v1/chat/completions", middlewares.Authorization, handler.duckduckgo)
group.POST("/completions", middlewares.Authorization, handler.duckduckgo)
group.POST("/hf/v1/chat/completions", middlewares.Authorization, handler.duckduckgo)
// GET 路由
group.GET("/v1/models", middlewares.Authorization, handler.engines)
group.GET("/api/v1/models", middlewares.Authorization, handler.engines)
group.GET("/models", middlewares.Authorization, handler.engines)
group.GET("/hf/v1/models", middlewares.Authorization, handler.engines)
}