-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
73 lines (61 loc) · 1.66 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
package main
import (
"github.com/gin-gonic/gin"
"github.com/huweiATgithub/chatgpt-apiserver/apiserver"
"log"
"net/http"
)
func main() {
server := Initialize()
server.Start()
}
type ControllerConfig struct {
Type string `mapstructure:"type"`
Weight int `mapstructure:"weight"`
Config map[string]string `mapstructure:"config"`
ConfigFile string `mapstructure:"config_file"`
}
type PoolConfig struct {
Type string `mapstructure:"type"`
}
type Config struct {
Port string `mapstructure:"port"`
Pool PoolConfig `mapstructure:"pool"`
Controllers []ControllerConfig `mapstructure:"controllers"`
}
type Server struct {
r *gin.Engine
config *Config
}
const defaultWeight = 1
func (s *Server) Start() {
if err := s.r.Run(":" + s.config.Port); err != nil {
log.Fatal(err)
}
}
func statusOK(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"status": "ok",
})
}
type ControllerFactory func(*ControllerConfig) (apiserver.Controller, error)
var ControllerFactories = map[string]ControllerFactory{
"openai": NewOpenAIController,
}
func NewOpenAIController(controllerConfig *ControllerConfig) (controller apiserver.Controller, err error) {
var openaiConfig *apiserver.OpenAIConfig
if controllerConfig.ConfigFile != "" {
openaiConfig, err = apiserver.NewOpenAIConfigFromFile(controllerConfig.ConfigFile)
if err != nil {
return
}
controller = apiserver.NewOpenAIController(*openaiConfig)
} else {
openaiConfig, err = apiserver.NewOpenAIConfigFromMap(controllerConfig.Config)
if err != nil {
return
}
}
controller = apiserver.NewOpenAIController(*openaiConfig)
return
}