package main import ( "fmt" "gopkg.in/natefinch/lumberjack.v2" "gopkg.in/yaml.v2" "io/ioutil" "os" ) var ListenAddress string var WWWRootPath string var BackendPrefix string var BackendTarget string var IsWindowsTestEnv string var ConfigFilePath string var ConfigData FrontendConfigYAML var AccessLogLevelConfigData *AccessLoggerLevelConfig var ErrorLogOutputLevel DWSILogLevel var AccessLog *AccessLogger var ErrorLog *CommonLogger func main() { fmt.Println("--- zDWSI Default Frontend ---") GetConfigEnv() ParseYAMLConfig() InitLogger() StartServer() } func GetConfigEnv() { fmt.Println("Get Basic Config From Environment Variables...") ListenAddress = os.Getenv("listen") WWWRootPath = os.Getenv("wwwroot") BackendPrefix = os.Getenv("backend_prefix") BackendTarget = os.Getenv("backend_target") IsWindowsTestEnv = os.Getenv("is_win_test_env") if ListenAddress == "" { fmt.Println("No environment variable 'listen', use default value ':8080'") ListenAddress = ":8080" } if WWWRootPath == "" { fmt.Println("No environment variable 'wwwroot', use default value '/data/wwwroot/'") WWWRootPath = "/data/wwwroot/" } if BackendPrefix == "" { fmt.Println("No environment variable 'backend_prefix', use default value 'api'") BackendPrefix = "api" } if BackendTarget == "" { fmt.Println("No environment variable 'backend_target', use default value 'http://localhost:9090/'") BackendTarget = "http://localhost:9090/" } ConfigFilePath = "/data/frontend/zdwsi.frontend.config.yaml" if IsWindowsTestEnv == "yes" { fmt.Println("Windows Test Enviroment Mode Enabled.") ConfigFilePath = "zdwsi.frontend.config.yaml" } fmt.Println("Listen Address: ", ListenAddress) fmt.Println("WWW Root Path: ", WWWRootPath) fmt.Println("Backend Prefix: ", BackendPrefix) fmt.Println("Backend Route Path: ", fmt.Sprintf("/%s/", BackendPrefix)) fmt.Println("Backend Proxy Target: ", BackendTarget) } func ParseYAMLConfig() { fmt.Println("Load Config YAML ...") jdata, err := ioutil.ReadFile(ConfigFilePath) if err != nil { fmt.Println("failed read config file: ", err) os.Exit(ExitCode_YAMLFileReadError) } err = yaml.Unmarshal(jdata, &ConfigData) if err != nil { fmt.Println("failed parse config YAML: ", err) os.Exit(ExitCode_YAMLParseError) } err, AccessLogLevelConfigData = ConvertLogLevelConfig(ConfigData.LogConfig.AccessLoggerDetailConfig) if err != nil { fmt.Println("failed parse config YAML in semantics: ", err) os.Exit(ExitCode_YAMLSemanticsError) } ErrorLogOutputLevel = transLevelStringIntoLevel(ConfigData.LogConfig.ErrorLoggerDetailConfig.OutputLevel) fmt.Println("Config Loaded.") err = yaml.Unmarshal(jdata, &ConfigData) if err != nil { fmt.Println("failed parse config YAML: ", err) os.Exit(ExitCode_YAMLParseError) } if ConfigData.CommonConfig.IsDebugModeOn { fmt.Println("Debug Mode is Enable.") fmt.Println("Print All Config For Debug:") tmpyaml, err := yaml.Marshal(ConfigData) if err != nil { fmt.Println("Failed Print Config: ", err) } else { fmt.Printf("%v\n\n", string(tmpyaml)) } } } func InitLogger() { cfg := ConfigData.LogConfig.RollingLogConfig.ErrLogCfg elw := &lumberjack.Logger{ Filename: cfg.FileName, MaxSize: cfg.MaxSize, MaxAge: cfg.MaxAge, MaxBackups: cfg.MaxBackups, LocalTime: cfg.UseLocalTime, Compress: cfg.Compress, } _, _ = fmt.Fprintln(elw, "Lumberjack Logger Write Test") ErrorLog = NewCommonLogger(ErrorLogOutputLevel, elw) ErrorLog.SetFormatter( ConfigData.LogConfig.ErrorLoggerDetailConfig.LogFormat, ConfigData.LogConfig.ErrorLoggerDetailConfig.UseUTC, ) cfg = ConfigData.LogConfig.RollingLogConfig.AccessLogCfg AccessLog = NewAccessLogger() if ConfigData.LogConfig.AccessLogEnable { alw := &lumberjack.Logger{ Filename: cfg.FileName, MaxSize: cfg.MaxSize, MaxAge: cfg.MaxAge, MaxBackups: cfg.MaxBackups, LocalTime: cfg.UseLocalTime, Compress: cfg.Compress, } AccessLog.UseLogger( alw, AccessLogLevelConfigData, ConfigData.LogConfig.AccessLoggerDetailConfig.LogFormat, ConfigData.LogConfig.AccessLoggerDetailConfig.UseUTC, ) } }