Delete initialize
Browse files- initialize/handlers.go +0 -116
- initialize/proxy.go +0 -48
- initialize/router.go +0 -66
initialize/handlers.go
DELETED
|
@@ -1,116 +0,0 @@
|
|
| 1 |
-
package initialize
|
| 2 |
-
|
| 3 |
-
import (
|
| 4 |
-
duckgoConvert "aurora/conversion/requests/duckgo"
|
| 5 |
-
"aurora/httpclient/bogdanfinn"
|
| 6 |
-
"aurora/internal/duckgo"
|
| 7 |
-
"aurora/internal/proxys"
|
| 8 |
-
officialtypes "aurora/typings/official"
|
| 9 |
-
|
| 10 |
-
"github.com/gin-gonic/gin"
|
| 11 |
-
)
|
| 12 |
-
|
| 13 |
-
type Handler struct {
|
| 14 |
-
proxy *proxys.IProxy
|
| 15 |
-
}
|
| 16 |
-
|
| 17 |
-
func NewHandle(proxy *proxys.IProxy) *Handler {
|
| 18 |
-
return &Handler{proxy: proxy}
|
| 19 |
-
}
|
| 20 |
-
|
| 21 |
-
func optionsHandler(c *gin.Context) {
|
| 22 |
-
// Set headers for CORS
|
| 23 |
-
c.Header("Access-Control-Allow-Origin", "*")
|
| 24 |
-
c.Header("Access-Control-Allow-Methods", "POST")
|
| 25 |
-
c.Header("Access-Control-Allow-Headers", "*")
|
| 26 |
-
c.JSON(200, gin.H{
|
| 27 |
-
"message": "pong",
|
| 28 |
-
})
|
| 29 |
-
}
|
| 30 |
-
|
| 31 |
-
func (h *Handler) duckduckgo(c *gin.Context) {
|
| 32 |
-
var original_request officialtypes.APIRequest
|
| 33 |
-
err := c.BindJSON(&original_request)
|
| 34 |
-
if err != nil {
|
| 35 |
-
c.JSON(400, gin.H{"error": gin.H{
|
| 36 |
-
"message": "Request must be proper JSON",
|
| 37 |
-
"type": "invalid_request_error",
|
| 38 |
-
"param": nil,
|
| 39 |
-
"code": err.Error(),
|
| 40 |
-
}})
|
| 41 |
-
return
|
| 42 |
-
}
|
| 43 |
-
proxyUrl := h.proxy.GetProxyIP()
|
| 44 |
-
client := bogdanfinn.NewStdClient()
|
| 45 |
-
token, err := duckgo.InitXVQD(client, proxyUrl)
|
| 46 |
-
if err != nil {
|
| 47 |
-
c.JSON(500, gin.H{
|
| 48 |
-
"error": err.Error(),
|
| 49 |
-
})
|
| 50 |
-
return
|
| 51 |
-
}
|
| 52 |
-
|
| 53 |
-
translated_request := duckgoConvert.ConvertAPIRequest(original_request)
|
| 54 |
-
response, err := duckgo.POSTconversation(client, translated_request, token, proxyUrl)
|
| 55 |
-
if err != nil {
|
| 56 |
-
c.JSON(500, gin.H{
|
| 57 |
-
"error": "request conversion error",
|
| 58 |
-
})
|
| 59 |
-
return
|
| 60 |
-
}
|
| 61 |
-
|
| 62 |
-
defer response.Body.Close()
|
| 63 |
-
if duckgo.Handle_request_error(c, response) {
|
| 64 |
-
return
|
| 65 |
-
}
|
| 66 |
-
var response_part string
|
| 67 |
-
response_part = duckgo.Handler(c, response, translated_request, original_request.Stream)
|
| 68 |
-
if c.Writer.Status() != 200 {
|
| 69 |
-
return
|
| 70 |
-
}
|
| 71 |
-
if !original_request.Stream {
|
| 72 |
-
c.JSON(200, officialtypes.NewChatCompletionWithModel(response_part, translated_request.Model))
|
| 73 |
-
} else {
|
| 74 |
-
c.String(200, "data: [DONE]\n\n")
|
| 75 |
-
}
|
| 76 |
-
}
|
| 77 |
-
|
| 78 |
-
func (h *Handler) engines(c *gin.Context) {
|
| 79 |
-
type ResData struct {
|
| 80 |
-
ID string `json:"id"`
|
| 81 |
-
Object string `json:"object"`
|
| 82 |
-
Created int `json:"created"`
|
| 83 |
-
OwnedBy string `json:"owned_by"`
|
| 84 |
-
}
|
| 85 |
-
|
| 86 |
-
type JSONData struct {
|
| 87 |
-
Object string `json:"object"`
|
| 88 |
-
Data []ResData `json:"data"`
|
| 89 |
-
}
|
| 90 |
-
|
| 91 |
-
modelS := JSONData{
|
| 92 |
-
Object: "list",
|
| 93 |
-
}
|
| 94 |
-
var resModelList []ResData
|
| 95 |
-
|
| 96 |
-
// Supported models
|
| 97 |
-
modelIDs := []string{
|
| 98 |
-
"gpt-4o-mini",
|
| 99 |
-
"o3-mini",
|
| 100 |
-
"claude-3-haiku-20240307",
|
| 101 |
-
"meta-llama/Llama-3.3-70B-Instruct-Turbo",
|
| 102 |
-
"mistralai/Mixtral-8x7B-Instruct-v0.1",
|
| 103 |
-
}
|
| 104 |
-
|
| 105 |
-
for _, modelID := range modelIDs {
|
| 106 |
-
resModelList = append(resModelList, ResData{
|
| 107 |
-
ID: modelID,
|
| 108 |
-
Object: "model",
|
| 109 |
-
Created: 1685474247,
|
| 110 |
-
OwnedBy: "duckduckgo",
|
| 111 |
-
})
|
| 112 |
-
}
|
| 113 |
-
|
| 114 |
-
modelS.Data = resModelList
|
| 115 |
-
c.JSON(200, modelS)
|
| 116 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
initialize/proxy.go
DELETED
|
@@ -1,48 +0,0 @@
|
|
| 1 |
-
package initialize
|
| 2 |
-
|
| 3 |
-
import (
|
| 4 |
-
"aurora/internal/proxys"
|
| 5 |
-
"bufio"
|
| 6 |
-
"log/slog"
|
| 7 |
-
"net/url"
|
| 8 |
-
"os"
|
| 9 |
-
)
|
| 10 |
-
|
| 11 |
-
func checkProxy() *proxys.IProxy {
|
| 12 |
-
var proxies []string
|
| 13 |
-
proxyUrl := os.Getenv("PROXY_URL")
|
| 14 |
-
if proxyUrl != "" {
|
| 15 |
-
proxies = append(proxies, proxyUrl)
|
| 16 |
-
}
|
| 17 |
-
|
| 18 |
-
if _, err := os.Stat("proxies.txt"); err == nil {
|
| 19 |
-
file, _ := os.Open("proxies.txt")
|
| 20 |
-
defer file.Close()
|
| 21 |
-
scanner := bufio.NewScanner(file)
|
| 22 |
-
for scanner.Scan() {
|
| 23 |
-
proxy := scanner.Text()
|
| 24 |
-
parsedURL, err := url.Parse(proxy)
|
| 25 |
-
if err != nil {
|
| 26 |
-
slog.Warn("proxy url is invalid", "url", proxy, "err", err)
|
| 27 |
-
continue
|
| 28 |
-
}
|
| 29 |
-
|
| 30 |
-
// 如果缺少端口信息,不是完整的代理链接
|
| 31 |
-
if parsedURL.Port() != "" {
|
| 32 |
-
proxies = append(proxies, proxy)
|
| 33 |
-
} else {
|
| 34 |
-
continue
|
| 35 |
-
}
|
| 36 |
-
}
|
| 37 |
-
}
|
| 38 |
-
|
| 39 |
-
if len(proxies) == 0 {
|
| 40 |
-
proxy := os.Getenv("http_proxy")
|
| 41 |
-
if proxy != "" {
|
| 42 |
-
proxies = append(proxies, proxy)
|
| 43 |
-
}
|
| 44 |
-
}
|
| 45 |
-
|
| 46 |
-
proxyIP := proxys.NewIProxyIP(proxies)
|
| 47 |
-
return &proxyIP
|
| 48 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
initialize/router.go
DELETED
|
@@ -1,66 +0,0 @@
|
|
| 1 |
-
package initialize
|
| 2 |
-
|
| 3 |
-
import (
|
| 4 |
-
"aurora/middlewares"
|
| 5 |
-
"net/http"
|
| 6 |
-
"os"
|
| 7 |
-
|
| 8 |
-
"github.com/gin-gonic/gin"
|
| 9 |
-
)
|
| 10 |
-
|
| 11 |
-
func RegisterRouter() *gin.Engine {
|
| 12 |
-
handler := NewHandle(
|
| 13 |
-
checkProxy(),
|
| 14 |
-
)
|
| 15 |
-
|
| 16 |
-
router := gin.Default()
|
| 17 |
-
router.Use(middlewares.Cors)
|
| 18 |
-
// // 访问根目录时,跳转到 /web
|
| 19 |
-
// router.GET("/", func(c *gin.Context) {
|
| 20 |
-
// c.JSON(200, gin.H{
|
| 21 |
-
// "message": "Hello, world!",
|
| 22 |
-
// })
|
| 23 |
-
// })
|
| 24 |
-
router.GET("/", func(c *gin.Context) {
|
| 25 |
-
c.Redirect(http.StatusFound, "/web")
|
| 26 |
-
})
|
| 27 |
-
|
| 28 |
-
router.GET("/ping", func(c *gin.Context) {
|
| 29 |
-
c.JSON(200, gin.H{
|
| 30 |
-
"message": "pong",
|
| 31 |
-
})
|
| 32 |
-
})
|
| 33 |
-
|
| 34 |
-
// 注册 prefixGroup 路由
|
| 35 |
-
prefixGroup := os.Getenv("PREFIX")
|
| 36 |
-
if prefixGroup != "" {
|
| 37 |
-
registerGroupRoutes(router.Group(prefixGroup), handler)
|
| 38 |
-
}
|
| 39 |
-
// 注册无前缀的路由
|
| 40 |
-
registerGroupRoutes(router.Group(""), handler)
|
| 41 |
-
|
| 42 |
-
return router
|
| 43 |
-
}
|
| 44 |
-
func registerGroupRoutes(group *gin.RouterGroup, handler *Handler) {
|
| 45 |
-
// OPTIONS 路由
|
| 46 |
-
group.OPTIONS("/v1/chat/completions", optionsHandler)
|
| 47 |
-
group.OPTIONS("/v1/chat/models", optionsHandler)
|
| 48 |
-
group.OPTIONS("/completions", optionsHandler)
|
| 49 |
-
group.OPTIONS("/models", optionsHandler)
|
| 50 |
-
group.OPTIONS("/api/v1/chat/completions", optionsHandler)
|
| 51 |
-
group.OPTIONS("/api/v1/models", optionsHandler)
|
| 52 |
-
group.OPTIONS("/hf/v1/chat/completions", optionsHandler)
|
| 53 |
-
group.OPTIONS("/hf/v1/models", optionsHandler)
|
| 54 |
-
|
| 55 |
-
// POST 路由
|
| 56 |
-
group.POST("/v1/chat/completions", middlewares.Authorization, handler.duckduckgo)
|
| 57 |
-
group.POST("/api/v1/chat/completions", middlewares.Authorization, handler.duckduckgo)
|
| 58 |
-
group.POST("/completions", middlewares.Authorization, handler.duckduckgo)
|
| 59 |
-
group.POST("/hf/v1/chat/completions", middlewares.Authorization, handler.duckduckgo)
|
| 60 |
-
|
| 61 |
-
// GET 路由
|
| 62 |
-
group.GET("/v1/models", middlewares.Authorization, handler.engines)
|
| 63 |
-
group.GET("/api/v1/models", middlewares.Authorization, handler.engines)
|
| 64 |
-
group.GET("/models", middlewares.Authorization, handler.engines)
|
| 65 |
-
group.GET("/hf/v1/models", middlewares.Authorization, handler.engines)
|
| 66 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|