-
Notifications
You must be signed in to change notification settings - Fork 34
/
init.go
69 lines (61 loc) · 1.33 KB
/
init.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
package main
import (
"fmt"
"github.com/KevinZonda/float32/exec/svr/db"
"github.com/KevinZonda/float32/exec/svr/shared"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"github.com/sashabaranov/go-openai"
"log"
"os"
"strings"
)
func initAll() {
godotenv.Load(".env")
fmt.Println("Work at:", os.Getenv("PWD"))
initChatGPT()
initGin()
initDb()
}
func initDb() {
dbUrl := os.Getenv("DB_URL")
if dbUrl == "" {
log.Println("DB_URL not set, HISTORY will not work")
return
}
db.InitDb(dbUrl)
}
func initChatGPT() {
token := os.Getenv("OPENAI")
ep := os.Getenv("OPENAI_ENDPOINT")
cfg := openai.DefaultConfig(token)
if ep != "" {
cfg.BaseURL = ep
}
shared.Cli = openai.NewClientWithConfig(cfg)
}
var g *gin.Engine
func initGin() {
g = gin.New()
g.Use(gin.Logger(), gin.Recovery())
config := cors.DefaultConfig()
if strings.TrimSpace(os.Getenv("DEBUG")) == "1" {
gin.SetMode(gin.DebugMode)
config.AllowAllOrigins = true
g.Use(cors.New(config))
} else {
gin.SetMode(gin.ReleaseMode)
ao := os.Getenv("ALLOW_ORIGINS")
config.AllowOrigins = strings.Split(ao, " ")
//config.AllowOrigins = []string{"https://float32.app"}
}
g.Use(cors.New(config))
}
func startGin() {
listenAddr := os.Getenv("LISTEN_ADDR")
if listenAddr == "" {
listenAddr = ":8080"
}
g.Run(listenAddr)
}