diff --git a/.gitignore b/.gitignore index 6ff7a25..203e6d4 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ .vscode .DS_Store +.vercel diff --git a/Groq2API.exe b/Groq2API.exe new file mode 100644 index 0000000..8c1653f Binary files /dev/null and b/Groq2API.exe differ diff --git a/api/route.go b/api/route.go new file mode 100644 index 0000000..4d310dc --- /dev/null +++ b/api/route.go @@ -0,0 +1,60 @@ +package api + +import ( + "github.com/gin-gonic/gin" + "net/http" + + "github.com/Star-Studio-Develop/Groq2API/initialize/auth" + "github.com/Star-Studio-Develop/Groq2API/initialize/stream" + "github.com/Star-Studio-Develop/Groq2API/initialize/user" + "github.com/Star-Studio-Develop/Groq2API/initialize/utils" +) + +func RegisterRoutes(router *gin.Engine) { + router.POST("/fetch-jwt", func(c *gin.Context) { + refreshToken := c.PostForm("refreshToken") + jwt, err := auth.FetchJWT(refreshToken) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + return + } + c.JSON(http.StatusOK, gin.H{"jwt": jwt}) + }) + + router.GET("/user/profile", func(c *gin.Context) { + jwt := c.GetHeader("Authorization") + orgID, err := user.FetchUserProfile(jwt) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + return + } + c.JSON(http.StatusOK, gin.H{"org_id": orgID}) + }) + + router.POST("/stream/fetch", func(c *gin.Context) { + var messages []stream.model.Message + if err := c.BindJSON(&messages); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid input"}) + return + } + jwt := c.GetHeader("Authorization") + orgID := c.PostForm("orgID") + modelType := c.PostForm("modelType") + maxTokens := c.GetInt64("maxTokens") + + response, err := stream.FetchStream(jwt, orgID, messages, modelType, maxTokens) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + return + } + c.DataFromReader(http.StatusOK, response.ContentLength, response.Header.Get("Content-Type"), response.Body, nil) + }) + + router.Use(utils.SetCorsHeaders) +} + +func main() { + router := initialize.RegisterRouter() + RegisterRoutes(router) + router.Run(":8080") +} diff --git a/api/router.go b/api/router.go new file mode 100644 index 0000000..9222993 --- /dev/null +++ b/api/router.go @@ -0,0 +1,19 @@ +package api + +import ( + "net/http" + + "github.com/Star-Studio-Develop/Groq2API/initialize" + "github.com/gorilla/mux" +) + +var router *mux.Router + +func init() { + // 初始化全局router,这假设initialize包中有一个返回*mux.Router的SetupRouter函数 + router = initialize.SetupRouter() +} + +func Listen(w http.ResponseWriter, r *http.Request) { + router.ServeHTTP(w, r) +} diff --git a/go.mod b/go.mod index 795cff7..7447678 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,8 @@ require ( ) require ( + github.com/aws/aws-lambda-go v1.46.0 // indirect + github.com/gorilla/mux v1.8.1 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect golang.org/x/sys v0.19.0 // indirect diff --git a/go.sum b/go.sum index 1c38477..3a2744f 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,9 @@ +github.com/aws/aws-lambda-go v1.46.0 h1:UWVnvh2h2gecOlFhHQfIPQcD8pL/f7pVCutmFl+oXU8= +github.com/aws/aws-lambda-go v1.46.0/go.mod h1:dpMpZgvWx5vuQJfBt0zqBha60q7Dd7RfgJv23DymV8A= github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= +github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= +github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= diff --git a/initialize/router.go b/initialize/router.go new file mode 100644 index 0000000..49f9f4a --- /dev/null +++ b/initialize/router.go @@ -0,0 +1,45 @@ +package router + +import ( + "net/http" + "os" + "time" + + "github.com/gorilla/mux" + "github.com/patrickmn/go-cache" + "github.com/rs/zerolog/log" +) + +// SetupRouter 初始化和返回*mux.Router +func SetupRouter() *mux.Router { + r := mux.NewRouter() + + // 创建缓存实例 + c := cache.New(4*time.Minute, 1*time.Minute) + + // 设置根路由重定向 + r.HandleFunc("/", rootHandler).Methods("GET") + + // 设置API端点 + r.HandleFunc("/v1/chat/completions", func(w http.ResponseWriter, r *http.Request) { + chatCompletionsHandler(w, r, c) + }).Methods("POST") + + return r +} + +// StartServer 启动HTTP服务器 +func StartServer() { + r := SetupRouter() + + // 获取环境变量中的端口或使用默认值 + port := os.Getenv("PORT") + if port == "" { + port = "8080" + } + + log.Info().Msgf("Server is listening on :%s", port) + if err := http.ListenAndServe(":"+port, r); err != nil { + log.Fatal().Err(err).Msg("Failed to start server") + } +} diff --git a/main.go b/main.go index 152e6ad..2608049 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,8 @@ package main import ( "bytes" "encoding/json" + "io" + "github.com/Star-Studio-Develop/Groq2API/initialize/auth" "github.com/Star-Studio-Develop/Groq2API/initialize/model" "github.com/Star-Studio-Develop/Groq2API/initialize/stream" @@ -10,8 +12,9 @@ import ( "github.com/Star-Studio-Develop/Groq2API/initialize/utils" "github.com/patrickmn/go-cache" "github.com/rs/zerolog/log" - "io" + "net/http" + "os" "strings" "time" ) @@ -154,16 +157,20 @@ func rootHandler(w http.ResponseWriter, r *http.Request) { } func main() { - // Create a cache with a default expiration time of 4 minutes, and which - // purges expired items every 1 minutes c := cache.New(4*time.Minute, 1*time.Minute) http.HandleFunc("/", rootHandler) http.HandleFunc("/v1/chat/completions", func(w http.ResponseWriter, r *http.Request) { chatCompletionsHandler(w, r, c) }) - log.Info().Msg("Server is listening on :8080") - if err := http.ListenAndServe(":8080", nil); err != nil { + // Retrieve port from environment variable or default to 8080 + port := os.Getenv("PORT") + if port == "" { + port = "8080" + } + + log.Info().Msgf("Server is listening on :%s", port) + if err := http.ListenAndServe(":"+port, nil); err != nil { log.Fatal().Err(err).Msg("Failed to start server") } } diff --git a/vercel.json b/vercel.json new file mode 100644 index 0000000..16fc77e --- /dev/null +++ b/vercel.json @@ -0,0 +1,8 @@ +{ + "routes": [ + { + "src": "/.*", + "dest": "/api/router.go" + } + ] + } \ No newline at end of file