forked from linweiyuan/go-chatgpt-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
94 lines (78 loc) · 2.16 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"log"
"os"
"strings"
http "github.com/bogdanfinn/fhttp"
"github.com/gin-gonic/gin"
"github.com/linweiyuan/go-chatgpt-api/api"
"github.com/linweiyuan/go-chatgpt-api/api/chatgpt"
"github.com/linweiyuan/go-chatgpt-api/api/imitate"
"github.com/linweiyuan/go-chatgpt-api/api/platform"
_ "github.com/linweiyuan/go-chatgpt-api/env"
"github.com/linweiyuan/go-chatgpt-api/middleware"
)
func init() {
gin.ForceConsoleColor()
gin.SetMode(gin.ReleaseMode)
}
func main() {
router := gin.Default()
router.Use(middleware.CORS())
router.Use(middleware.Authorization())
setupChatGPTAPIs(router)
setupPlatformAPIs(router)
setupPandoraAPIs(router)
setupImitateAPIs(router)
router.NoRoute(api.Proxy)
router.GET("/", func(c *gin.Context) {
c.String(http.StatusOK, api.ReadyHint)
})
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
err := router.Run(":" + port)
if err != nil {
log.Fatal("failed to start server: " + err.Error())
}
}
func setupChatGPTAPIs(router *gin.Engine) {
chatgptGroup := router.Group("/chatgpt")
{
chatgptGroup.POST("/login", chatgpt.Login)
chatgptGroup.POST("/backend-api/login", chatgpt.Login) // add support for other projects
conversationGroup := chatgptGroup.Group("/backend-api/conversation")
{
conversationGroup.POST("", chatgpt.CreateConversation)
}
}
}
func setupPlatformAPIs(router *gin.Engine) {
platformGroup := router.Group("/platform")
{
platformGroup.POST("/login", platform.Login)
platformGroup.POST("/v1/login", platform.Login)
apiGroup := platformGroup.Group("/v1")
{
apiGroup.POST("/chat/completions", platform.CreateChatCompletions)
apiGroup.POST("/completions", platform.CreateCompletions)
}
}
}
func setupPandoraAPIs(router *gin.Engine) {
router.Any("/api/*path", func(c *gin.Context) {
c.Request.URL.Path = strings.ReplaceAll(c.Request.URL.Path, "/api", "/chatgpt/backend-api")
router.HandleContext(c)
})
}
func setupImitateAPIs(router *gin.Engine) {
imitateGroup := router.Group("/imitate")
{
imitateGroup.POST("/login", chatgpt.Login)
apiGroup := imitateGroup.Group("/v1")
{
apiGroup.POST("/chat/completions", imitate.CreateChatCompletions)
}
}
}