Update main.go
Browse files
main.go
CHANGED
@@ -2,12 +2,10 @@ package main
|
|
2 |
|
3 |
import (
|
4 |
"bufio"
|
5 |
-
"embed"
|
6 |
"encoding/json"
|
7 |
"errors"
|
8 |
"fmt"
|
9 |
"io"
|
10 |
-
"io/fs"
|
11 |
"log"
|
12 |
"net/http"
|
13 |
"os"
|
@@ -18,9 +16,6 @@ import (
|
|
18 |
"github.com/joho/godotenv"
|
19 |
)
|
20 |
|
21 |
-
//go:embed web/*
|
22 |
-
var staticFiles embed.FS
|
23 |
-
|
24 |
type Config struct {
|
25 |
APIPrefix string
|
26 |
APIKey string
|
@@ -85,32 +80,13 @@ func authMiddleware() gin.HandlerFunc {
|
|
85 |
func main() {
|
86 |
r := gin.Default()
|
87 |
r.Use(corsMiddleware())
|
88 |
-
|
89 |
-
//
|
90 |
-
//subFS, err := fs.Sub(staticFiles, "web")
|
91 |
-
//if err != nil {
|
92 |
-
// log.Fatal(err)
|
93 |
-
//}
|
94 |
-
//r.StaticFS("/web", http.FS(subFS))
|
95 |
-
|
96 |
-
//// 2. 根路径重定向到 /web
|
97 |
-
//r.GET("/", func(c *gin.Context) {
|
98 |
-
// c.Redirect(http.StatusMovedPermanently, "/web")
|
99 |
-
//})
|
100 |
-
// r.GET("/", func(c *gin.Context) {
|
101 |
-
// c.JSON(http.StatusOK, gin.H{"message": "API 服务运行中~"})
|
102 |
-
// })
|
103 |
-
// 3. 健康检查
|
104 |
r.GET("/ping", func(c *gin.Context) {
|
105 |
c.JSON(http.StatusOK, gin.H{"message": "pong"})
|
106 |
})
|
107 |
-
|
108 |
-
//
|
109 |
-
// authorized.Use(authMiddleware())
|
110 |
-
// {
|
111 |
-
// authorized.GET("/hf/v1/models", handleModels)
|
112 |
-
// authorized.POST("/hf/v1/chat/completions", handleCompletion)
|
113 |
-
// }
|
114 |
apiGroup := r.Group("/")
|
115 |
apiGroup.Use(authMiddleware()) // 可以选择性地提供 API 密钥
|
116 |
{
|
@@ -129,7 +105,8 @@ func main() {
|
|
129 |
// 新路径 /completions
|
130 |
apiGroup.POST("/completions", handleCompletion)
|
131 |
}
|
132 |
-
|
|
|
133 |
port := os.Getenv("PORT")
|
134 |
if port == "" {
|
135 |
port = "7860"
|
@@ -164,7 +141,6 @@ func handleCompletion(c *gin.Context) {
|
|
164 |
|
165 |
model := convertModel(req.Model)
|
166 |
content := prepareMessages(req.Messages)
|
167 |
-
// log.Printf("messages: %v", content)
|
168 |
|
169 |
reqBody := map[string]interface{}{
|
170 |
"model": model,
|
@@ -238,7 +214,6 @@ func handleCompletion(c *gin.Context) {
|
|
238 |
line = strings.TrimSpace(line)
|
239 |
// 忽略非 JSON 数据块(例如特殊标记 [DONE])
|
240 |
if line == "[DONE]" {
|
241 |
-
//log.Printf("响应行 DONE, 即将跳过")
|
242 |
break
|
243 |
}
|
244 |
var chunk map[string]interface{}
|
@@ -347,7 +322,6 @@ func handleCompletion(c *gin.Context) {
|
|
347 |
}
|
348 |
}
|
349 |
|
350 |
-
|
351 |
func requestToken() (string, error) {
|
352 |
url := "https://duckduckgo.com/duckchat/v1/status"
|
353 |
client := &http.Client{
|
@@ -493,4 +467,4 @@ func getIntEnv(key string, fallback int) int {
|
|
493 |
|
494 |
func getDurationEnv(key string, fallback int) time.Duration {
|
495 |
return time.Duration(getIntEnv(key, fallback)) * time.Millisecond
|
496 |
-
}
|
|
|
2 |
|
3 |
import (
|
4 |
"bufio"
|
|
|
5 |
"encoding/json"
|
6 |
"errors"
|
7 |
"fmt"
|
8 |
"io"
|
|
|
9 |
"log"
|
10 |
"net/http"
|
11 |
"os"
|
|
|
16 |
"github.com/joho/godotenv"
|
17 |
)
|
18 |
|
|
|
|
|
|
|
19 |
type Config struct {
|
20 |
APIPrefix string
|
21 |
APIKey string
|
|
|
80 |
func main() {
|
81 |
r := gin.Default()
|
82 |
r.Use(corsMiddleware())
|
83 |
+
|
84 |
+
// 健康检查
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
r.GET("/ping", func(c *gin.Context) {
|
86 |
c.JSON(http.StatusOK, gin.H{"message": "pong"})
|
87 |
})
|
88 |
+
|
89 |
+
// API 路由组
|
|
|
|
|
|
|
|
|
|
|
90 |
apiGroup := r.Group("/")
|
91 |
apiGroup.Use(authMiddleware()) // 可以选择性地提供 API 密钥
|
92 |
{
|
|
|
105 |
// 新路径 /completions
|
106 |
apiGroup.POST("/completions", handleCompletion)
|
107 |
}
|
108 |
+
|
109 |
+
// 从环境变量中读取端口号
|
110 |
port := os.Getenv("PORT")
|
111 |
if port == "" {
|
112 |
port = "7860"
|
|
|
141 |
|
142 |
model := convertModel(req.Model)
|
143 |
content := prepareMessages(req.Messages)
|
|
|
144 |
|
145 |
reqBody := map[string]interface{}{
|
146 |
"model": model,
|
|
|
214 |
line = strings.TrimSpace(line)
|
215 |
// 忽略非 JSON 数据块(例如特殊标记 [DONE])
|
216 |
if line == "[DONE]" {
|
|
|
217 |
break
|
218 |
}
|
219 |
var chunk map[string]interface{}
|
|
|
322 |
}
|
323 |
}
|
324 |
|
|
|
325 |
func requestToken() (string, error) {
|
326 |
url := "https://duckduckgo.com/duckchat/v1/status"
|
327 |
client := &http.Client{
|
|
|
467 |
|
468 |
func getDurationEnv(key string, fallback int) time.Duration {
|
469 |
return time.Duration(getIntEnv(key, fallback)) * time.Millisecond
|
470 |
+
}
|