@@ -2,10 +2,10 @@ package config
22
33import (
44 _ "embed"
5+ "strings"
56
67 "github.com/spf13/viper"
78
8- "github.com/chaitin/MonkeyCode/backend/pkg/cvt"
99 "github.com/chaitin/MonkeyCode/backend/pkg/logger"
1010)
1111
@@ -20,9 +20,7 @@ type Config struct {
2020 BaseUrl string `mapstructure:"base_url"`
2121
2222 Server struct {
23- Http struct {
24- Host string `mapstructure:"host"`
25- } `mapstructure:"http"`
23+ Addr string `mapstructure:"addr"`
2624 } `mapstructure:"server"`
2725
2826 Admin struct {
@@ -51,52 +49,61 @@ type Config struct {
5149 } `mapstructure:"redis"`
5250
5351 LLMProxy struct {
54- Timeout string `mapstructure:"timeout"`
55- KeepAlive string `mapstructure:"keep_alive"`
56- ClientPoolSize int `mapstructure:"client_pool_size"`
57- RequestLogPath string `mapstructure:"request_log_path"`
52+ Timeout string `mapstructure:"timeout"`
53+ KeepAlive string `mapstructure:"keep_alive"`
54+ ClientPoolSize int `mapstructure:"client_pool_size"`
55+ StreamClientPoolSize int `mapstructure:"stream_client_pool_size"`
56+ RequestLogPath string `mapstructure:"request_log_path"`
5857 } `mapstructure:"llm_proxy"`
5958
6059 InitModel struct {
61- ModelName string `mapstructure:"model_name "`
62- ModelKey string `mapstructure:"model_key "`
63- ModelURL string `mapstructure:"model_url "`
60+ Name string `mapstructure:"name "`
61+ Key string `mapstructure:"key "`
62+ URL string `mapstructure:"url "`
6463 } `mapstructure:"init_model"`
6564
6665 Extension struct {
6766 Baseurl string `mapstructure:"baseurl"`
6867 } `mapstructure:"extension"`
6968}
7069
71- func Init (dir string ) (* Config , error ) {
72- viper .SetConfigName ("config" )
73- viper .SetConfigType ("yaml" )
74- viper .AddConfigPath (dir )
75- if err := viper .ReadInConfig (); err != nil {
76- return nil , err
77- }
70+ func Init () (* Config , error ) {
71+ v := viper .New ()
72+ v .AutomaticEnv ()
73+ v .SetEnvPrefix ("MONKEYCODE" )
74+ v .SetEnvKeyReplacer (strings .NewReplacer ("." , "_" ))
75+
76+ v .SetDefault ("debug" , false )
77+ v .SetDefault ("logger.level" , "info" )
78+ v .SetDefault ("base_url" , "" )
79+ v .SetDefault ("server.addr" , ":8888" )
80+ v .SetDefault ("admin.user" , "admin" )
81+ v .SetDefault ("admin.password" , "" )
82+ v .SetDefault ("session.expire_day" , 30 )
83+ v .SetDefault ("database.master" , "" )
84+ v .SetDefault ("database.slave" , "" )
85+ v .SetDefault ("database.max_open_conns" , 50 )
86+ v .SetDefault ("database.max_idle_conns" , 10 )
87+ v .SetDefault ("database.conn_max_lifetime" , 30 )
88+ v .SetDefault ("redis.host" , "monkeycode-redis" )
89+ v .SetDefault ("redis.port" , "6379" )
90+ v .SetDefault ("redis.pass" , "" )
91+ v .SetDefault ("redis.db" , 0 )
92+ v .SetDefault ("redis.idle_conn" , 20 )
93+ v .SetDefault ("llm_proxy.timeout" , "30s" )
94+ v .SetDefault ("llm_proxy.keep_alive" , "60s" )
95+ v .SetDefault ("llm_proxy.client_pool_size" , 100 )
96+ v .SetDefault ("llm_proxy.stream_client_pool_size" , 5000 )
97+ v .SetDefault ("llm_proxy.request_log_path" , "/app/request/logs" )
98+ v .SetDefault ("init_model.name" , "qwen2.5-coder-3b-instruct" )
99+ v .SetDefault ("init_model.key" , "" )
100+ v .SetDefault ("init_model.url" , "https://model-square.app.baizhi.cloud/v1" )
101+ v .SetDefault ("extension.baseurl" , "https://release.baizhi.cloud" )
78102
79103 c := Config {}
80- if err := viper .Unmarshal (& c ); err != nil {
104+ if err := v .Unmarshal (& c ); err != nil {
81105 return nil , err
82106 }
83107
84- c = defaultValue (c )
85108 return & c , nil
86109}
87-
88- func defaultValue (c Config ) Config {
89- c .Server .Http .Host = cvt .ZeroWithDefault (c .Server .Http .Host , ":8888" )
90- c .Redis .IdleConn = cvt .ZeroWithDefault (c .Redis .IdleConn , 20 )
91- c .Database .MaxOpenConns = cvt .ZeroWithDefault (c .Database .MaxOpenConns , 50 )
92- c .Database .MaxIdleConns = cvt .ZeroWithDefault (c .Database .MaxIdleConns , 10 )
93- c .Database .ConnMaxLifetime = cvt .ZeroWithDefault (c .Database .ConnMaxLifetime , 30 )
94- c .Session .ExpireDay = cvt .ZeroWithDefault (c .Session .ExpireDay , 15 )
95-
96- c .LLMProxy .Timeout = cvt .ZeroWithDefault (c .LLMProxy .Timeout , "30s" )
97- c .LLMProxy .KeepAlive = cvt .ZeroWithDefault (c .LLMProxy .KeepAlive , "60s" )
98- c .LLMProxy .ClientPoolSize = cvt .ZeroWithDefault (c .LLMProxy .ClientPoolSize , 100 )
99- c .LLMProxy .RequestLogPath = cvt .ZeroWithDefault (c .LLMProxy .RequestLogPath , "/app/request/logs" )
100-
101- return c
102- }
0 commit comments