feat: add docker env vars start server
This commit is contained in:
@@ -0,0 +1,63 @@
|
|||||||
|
package setting
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/unknwon/com"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
var optionValueMap = map[string]string{}
|
||||||
|
|
||||||
|
// getOptionEnvValue 获取必须声明的环境变量
|
||||||
|
func getOptionEnvValue(key string, defaultV string) string {
|
||||||
|
value := os.Getenv(key)
|
||||||
|
result := ""
|
||||||
|
if value == "" {
|
||||||
|
result = defaultV
|
||||||
|
} else {
|
||||||
|
result = value
|
||||||
|
}
|
||||||
|
optionValueMap[key] = result
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// getMustEnvValue 获取必须声明的环境变量
|
||||||
|
func getMustEnvValue(key string) string {
|
||||||
|
value := os.Getenv(key)
|
||||||
|
if value == "" {
|
||||||
|
log.Printf("[message-nest] you must assign env: %s", key)
|
||||||
|
return ""
|
||||||
|
} else {
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// printOptionValue 打印可选环境变量值
|
||||||
|
func printOptionValue() {
|
||||||
|
for key, val := range optionValueMap {
|
||||||
|
log.Printf("[message-nest] current option env : %s, value: %s", key, val)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// loadConfigFromEnv 从环境变量加载配置
|
||||||
|
func loadConfigFromEnv() {
|
||||||
|
AppSetting.JwtSecret = getOptionEnvValue("JWT_SECRET", "message-nest")
|
||||||
|
AppSetting.LogLevel = getOptionEnvValue("LOG_LEVEL", "INFO")
|
||||||
|
AppSetting.InitData = getOptionEnvValue("INIT_DATA", "")
|
||||||
|
|
||||||
|
ServerSetting.RunMode = getOptionEnvValue("RUN_MODE", "release")
|
||||||
|
ServerSetting.HttpPort = 8000
|
||||||
|
ServerSetting.ReadTimeout = 60
|
||||||
|
ServerSetting.WriteTimeout = 60
|
||||||
|
|
||||||
|
DatabaseSetting.Type = "mysql"
|
||||||
|
DatabaseSetting.Host = getMustEnvValue("MYSQL_HOST")
|
||||||
|
DatabaseSetting.Port = com.StrTo(getMustEnvValue("MYSQL_PORT")).MustInt()
|
||||||
|
DatabaseSetting.User = getMustEnvValue("MYSQL_USER")
|
||||||
|
DatabaseSetting.Password = getMustEnvValue("MYSQL_PASSWORD")
|
||||||
|
DatabaseSetting.Name = getMustEnvValue("MYSQL_DB")
|
||||||
|
DatabaseSetting.TablePrefix = getMustEnvValue("MYSQL_TABLE_PREFIX")
|
||||||
|
DatabaseSetting.SqlDebug = getOptionEnvValue("SQL_DEBUG", "disable")
|
||||||
|
|
||||||
|
printOptionValue()
|
||||||
|
}
|
||||||
+22
-8
@@ -2,6 +2,7 @@ package setting
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-ini/ini"
|
"github.com/go-ini/ini"
|
||||||
@@ -43,17 +44,30 @@ var DatabaseSetting = &Database{}
|
|||||||
|
|
||||||
var cfg *ini.File
|
var cfg *ini.File
|
||||||
|
|
||||||
|
func fileExists(filePath string) bool {
|
||||||
|
_, err := os.Stat(filePath)
|
||||||
|
return !os.IsNotExist(err)
|
||||||
|
}
|
||||||
|
|
||||||
// Setup initialize the configuration instance
|
// Setup initialize the configuration instance
|
||||||
func Setup() {
|
func Setup() {
|
||||||
var err error
|
var err error
|
||||||
cfg, err = ini.Load("conf/app.ini")
|
intPath := "conf/app.ini"
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("setting.Setup, fail to parse 'conf/app.ini': %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
mapTo("app", AppSetting)
|
if fileExists(intPath) {
|
||||||
mapTo("server", ServerSetting)
|
log.Printf("[message-nest] start server from %s.", intPath)
|
||||||
mapTo("database", DatabaseSetting)
|
cfg, err = ini.Load(intPath)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("[message-nest] setting.Setup, fail to parse 'conf/app.ini': %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
mapTo("app", AppSetting)
|
||||||
|
mapTo("server", ServerSetting)
|
||||||
|
mapTo("database", DatabaseSetting)
|
||||||
|
} else {
|
||||||
|
log.Printf("[message-nest] %s is not exists, start server from env vars.", intPath)
|
||||||
|
loadConfigFromEnv()
|
||||||
|
}
|
||||||
|
|
||||||
ServerSetting.ReadTimeout = ServerSetting.ReadTimeout * time.Second
|
ServerSetting.ReadTimeout = ServerSetting.ReadTimeout * time.Second
|
||||||
ServerSetting.WriteTimeout = ServerSetting.WriteTimeout * time.Second
|
ServerSetting.WriteTimeout = ServerSetting.WriteTimeout * time.Second
|
||||||
@@ -63,6 +77,6 @@ func Setup() {
|
|||||||
func mapTo(section string, v interface{}) {
|
func mapTo(section string, v interface{}) {
|
||||||
err := cfg.Section(section).MapTo(v)
|
err := cfg.Section(section).MapTo(v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Cfg.MapTo %s err: %v", section, err)
|
log.Fatalf("[message-nest] Cfg.MapTo %s err: %v", section, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user