share-knowledge-server/initialize/viper.go

176 lines
4.5 KiB
Go

/*
* @Date: 2021-03-22 09:38:24
* @LastEditors: viletyy
* @LastEditTime: 2021-03-22 10:12:33
* @FilePath: /potato/initialize/viper.go
*/
package initialize
import (
"fmt"
"os"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
"github.com/viletyy/yolk/convert"
"gitlink.org.cn/Gitlink/share-knowledge-server/global"
)
func Viper() *viper.Viper {
v := viper.New()
v.SetConfigFile("config.yaml")
err := v.ReadInConfig()
if err != nil {
panic(fmt.Errorf("Fatal error config file: %s \n", err))
}
v.WatchConfig()
v.OnConfigChange(func(e fsnotify.Event) {
fmt.Println("config file changed:", e.Name)
if err := v.Unmarshal(&global.GO_CONFIG); err != nil {
fmt.Println(err)
}
})
if err := v.Unmarshal(&global.GO_CONFIG); err != nil {
fmt.Println(err)
}
loadEnvConfig()
return v
}
func loadEnvConfig() {
if os.Getenv("APP_NAME") != "" {
global.GO_CONFIG.App.Name = os.Getenv("APP_NAME")
}
if os.Getenv("APP_VERSION") != "" {
global.GO_CONFIG.App.Version = os.Getenv("APP_VERSION")
}
if os.Getenv("APP_DOMAIN") != "" {
global.GO_CONFIG.App.Domain = os.Getenv("APP_DOMAIN")
}
if os.Getenv("APP_RUN_MODE") != "" {
global.GO_CONFIG.App.RunMode = os.Getenv("APP_RUN_MODE")
}
app_default_context_timeout, _ := convert.StrTo(os.Getenv("APP_DEFAULT_CONTEXT_TIMEOUT")).Int64()
if app_default_context_timeout > 0 {
global.GO_CONFIG.App.DefaultContextTimeout = app_default_context_timeout
}
app_page_size, _ := convert.StrTo(os.Getenv("APP_PAGE_SISE")).Int64()
if app_page_size > 0 {
global.GO_CONFIG.App.PageSize = app_page_size
}
server_http_port, _ := convert.StrTo(os.Getenv("SERVER_HTTP_PORT")).Int64()
if server_http_port > 0 {
global.GO_CONFIG.Server.HttpPort = server_http_port
}
server_grpc_port, _ := convert.StrTo(os.Getenv("SERVER_GRPC_PORT")).Int64()
if server_grpc_port > 0 {
global.GO_CONFIG.Server.GrpcPort = server_grpc_port
}
server_read_timeout, _ := convert.StrTo(os.Getenv("SERVER_READ_TIMEOUT")).Int64()
if server_read_timeout > 0 {
global.GO_CONFIG.Server.ReadTimeout = server_read_timeout
}
server_write_timeout, _ := convert.StrTo(os.Getenv("SERVER_WRITE_TIMEOUT")).Int64()
if server_write_timeout > 0 {
global.GO_CONFIG.Server.WriteTimeout = server_write_timeout
}
if os.Getenv("DATABASE_TYPE") != "" {
global.GO_CONFIG.Database.Type = os.Getenv("DATABASE_TYPE")
}
if os.Getenv("DATABASE_USER") != "" {
global.GO_CONFIG.Database.User = os.Getenv("DATABASE_USER")
}
if os.Getenv("DATABASE_PASSWORD") != "" {
global.GO_CONFIG.Database.Password = os.Getenv("DATABASE_PASSWORD")
}
if os.Getenv("DATABASE_HOST") != "" {
global.GO_CONFIG.Database.Host = os.Getenv("DATABASE_HOST")
}
database_port, _ := convert.StrTo(os.Getenv("DATABASE_PORT")).Int64()
if database_port > 0 {
global.GO_CONFIG.Database.Port = database_port
}
if os.Getenv("DATABASE_NAME") != "" {
global.GO_CONFIG.Database.Name = os.Getenv("DATABASE_NAME")
}
if os.Getenv("DATABASE_TABLE_PREFIX") != "" {
global.GO_CONFIG.Database.TablePrefix = os.Getenv("DATABASE_TABLE_PREFIX")
}
if os.Getenv("REDIS_HOST") != "" {
global.GO_CONFIG.Redis.Host = os.Getenv("REDIS_HOST")
}
redis_port, _ := convert.StrTo(os.Getenv("REDIS_PORT")).Int64()
if redis_port > 0 {
global.GO_CONFIG.Redis.Port = redis_port
}
if os.Getenv("REDIS_PASSWORD") != "" {
global.GO_CONFIG.Redis.Password = os.Getenv("REDIS_PASSWORD")
}
redis_db, _ := convert.StrTo(os.Getenv("REDIS_DB")).Int64()
if redis_db > 0 {
global.GO_CONFIG.Redis.Db = redis_db
}
if os.Getenv("ZAP_LEVEL") != "" {
global.GO_CONFIG.Zap.Level = os.Getenv("ZAP_LEVEL")
}
if os.Getenv("ZAP_FORMAT") != "" {
global.GO_CONFIG.Zap.Format = os.Getenv("ZAP_FORMAT")
}
if os.Getenv("ZAP_PREFIX") != "" {
global.GO_CONFIG.Zap.Prefix = os.Getenv("ZAP_PREFIX")
}
if os.Getenv("ZAP_DIRECTOR") != "" {
global.GO_CONFIG.Zap.Director = os.Getenv("ZAP_DIRECTOR")
}
if os.Getenv("ZAP_LINK_NAME") != "" {
global.GO_CONFIG.Zap.LinkName = os.Getenv("ZAP_LINK_NAME")
}
zap_show_line, _ := convert.StrTo(os.Getenv("LOG_IN_CONSOLE")).Bool()
if !zap_show_line {
global.GO_CONFIG.Zap.ShowLine = zap_show_line
}
if os.Getenv("ZAP_ENCODE_LEVEL") != "" {
global.GO_CONFIG.Zap.EncodeLevel = os.Getenv("ZAP_ENCODE_LEVEL")
}
if os.Getenv("ZAP_STACKTRACE_KEY") != "" {
global.GO_CONFIG.Zap.StacktraceKey = os.Getenv("ZAP_STACKTRACE_KEY")
}
zap_log_in_console, _ := convert.StrTo(os.Getenv("LOG_IN_CONSOLE")).Bool()
if !zap_log_in_console {
global.GO_CONFIG.Zap.LogInConsole = zap_log_in_console
}
}