forked from acheong08/ChatGPT-to-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
77 lines (68 loc) · 1.59 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
package main
import (
"encoding/json"
"freechatgpt/internal/tokens"
"os"
"strings"
"github.com/acheong08/endless"
"github.com/gin-gonic/gin"
)
var HOST string
var PORT string
var ACCESS_TOKENS tokens.AccessToken
func init() {
HOST = os.Getenv("SERVER_HOST")
PORT = os.Getenv("SERVER_PORT")
if HOST == "" {
HOST = "127.0.0.1"
}
if PORT == "" {
PORT = "8080"
}
accessToken := os.Getenv("ACCESS_TOKENS")
if accessToken != "" {
accessTokens := strings.Split(accessToken, ",")
ACCESS_TOKENS = tokens.NewAccessToken(accessTokens)
}
// Check if access_tokens.json exists
if _, err := os.Stat("access_tokens.json"); os.IsNotExist(err) {
// Create the file
file, err := os.Create("access_tokens.json")
if err != nil {
panic(err)
}
defer file.Close()
} else {
// Load the tokens
file, err := os.Open("access_tokens.json")
if err != nil {
panic(err)
}
defer file.Close()
decoder := json.NewDecoder(file)
var token_list []string
err = decoder.Decode(&token_list)
if err != nil {
return
}
ACCESS_TOKENS = tokens.NewAccessToken(token_list)
}
}
func main() {
router := gin.Default()
router.Use(cors)
router.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
admin_routes := router.Group("/admin")
admin_routes.Use(adminCheck)
/// Admin routes
admin_routes.PATCH("/password", passwordHandler)
admin_routes.PATCH("/tokens", adminCheck, tokensHandler)
/// Public routes
router.OPTIONS("/v1/chat/completions", optionsHandler)
router.POST("/v1/chat/completions", nightmare)
endless.ListenAndServe(HOST+":"+PORT, router)
}