u
This commit is contained in:
1 parent
200f72eb86
commit
91ae4cb474
19 files changed
+650
-148
No files matched your search
@@ -0,0 +1,59 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"base-go-v2/internal/types"
|
||||
"github.com/spf13/viper"
|
||||
"time"
|
||||
)
|
||||
|
||||
// App 全局唯一导出配置实例,初始化默认值
|
||||
var App = &types.AppConfig{
|
||||
Server: types.AppConfig{}.Server,
|
||||
JWT: types.AppConfig{}.JWT,
|
||||
Db: types.AppConfig{}.Db,
|
||||
Snowflake: types.AppConfig{}.Snowflake,
|
||||
}
|
||||
|
||||
// 给部分字段设置默认值
|
||||
func init() {
|
||||
App.Server.Port = 8080
|
||||
App.JWT.Secret = "default_secret"
|
||||
App.JWT.Expiry = time.Hour
|
||||
|
||||
// 数据库只给连接池默认值
|
||||
App.Db.MaxOpenConns = 10
|
||||
App.Db.MaxIdleConns = 5
|
||||
App.Db.ConnMaxLifetime = 30 * time.Minute
|
||||
|
||||
App.Snowflake.DatacenterID = 1
|
||||
App.Snowflake.WorkerID = 1
|
||||
}
|
||||
|
||||
// InitApplicationConfig 初始化配置
|
||||
func InitApplicationConfig(configPath string) error {
|
||||
v := viper.New()
|
||||
v.SetConfigFile(configPath)
|
||||
v.SetConfigType("yaml")
|
||||
|
||||
if err := v.ReadInConfig(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := v.UnmarshalKey("server", &App.Server); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := v.UnmarshalKey("jwt", &App.JWT); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := v.UnmarshalKey("snowflake", &App.Snowflake); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := v.UnmarshalKey("db", &App.Db); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user