main.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package main
  2. import (
  3. "fmt"
  4. "gopkg.in/natefinch/lumberjack.v2"
  5. "gopkg.in/yaml.v2"
  6. "io/ioutil"
  7. "os"
  8. )
  9. var ListenAddress string
  10. var WWWRootPath string
  11. var BackendPrefix string
  12. var BackendTarget string
  13. var IsWindowsTestEnv string
  14. var ConfigFilePath string
  15. var ConfigData FrontendConfigYAML
  16. var AccessLogLevelConfigData *AccessLoggerLevelConfig
  17. var ErrorLogOutputLevel DWSILogLevel
  18. var AccessLog *AccessLogger
  19. var ErrorLog *CommonLogger
  20. func main() {
  21. fmt.Println("--- zDWSI Default Frontend ---")
  22. GetConfigEnv()
  23. ParseYAMLConfig()
  24. InitLogger()
  25. StartServer()
  26. }
  27. func GetConfigEnv() {
  28. fmt.Println("Get Basic Config From Environment Variables...")
  29. ListenAddress = os.Getenv("listen")
  30. WWWRootPath = os.Getenv("wwwroot")
  31. BackendPrefix = os.Getenv("backend_prefix")
  32. BackendTarget = os.Getenv("backend_target")
  33. IsWindowsTestEnv = os.Getenv("is_win_test_env")
  34. if ListenAddress == "" {
  35. fmt.Println("No environment variable 'listen', use default value ':8080'")
  36. ListenAddress = ":8080"
  37. }
  38. if WWWRootPath == "" {
  39. fmt.Println("No environment variable 'wwwroot', use default value '/data/wwwroot/'")
  40. WWWRootPath = "/data/wwwroot/"
  41. }
  42. if BackendPrefix == "" {
  43. fmt.Println("No environment variable 'backend_prefix', use default value 'api'")
  44. BackendPrefix = "api"
  45. }
  46. if BackendTarget == "" {
  47. fmt.Println("No environment variable 'backend_target', use default value 'http://localhost:9090/'")
  48. BackendTarget = "http://localhost:9090/"
  49. }
  50. ConfigFilePath = "/data/frontend/zdwsi.frontend.config.yaml"
  51. if IsWindowsTestEnv == "yes" {
  52. fmt.Println("Windows Test Enviroment Mode Enabled.")
  53. ConfigFilePath = "zdwsi.frontend.config.yaml"
  54. }
  55. fmt.Println("Listen Address: ", ListenAddress)
  56. fmt.Println("WWW Root Path: ", WWWRootPath)
  57. fmt.Println("Backend Prefix: ", BackendPrefix)
  58. fmt.Println("Backend Route Path: ", fmt.Sprintf("/%s/", BackendPrefix))
  59. fmt.Println("Backend Proxy Target: ", BackendTarget)
  60. }
  61. func ParseYAMLConfig() {
  62. fmt.Println("Load Config YAML ...")
  63. jdata, err := ioutil.ReadFile(ConfigFilePath)
  64. if err != nil {
  65. fmt.Println("failed read config file: ", err)
  66. os.Exit(ExitCode_YAMLFileReadError)
  67. }
  68. err = yaml.Unmarshal(jdata, &ConfigData)
  69. if err != nil {
  70. fmt.Println("failed parse config YAML: ", err)
  71. os.Exit(ExitCode_YAMLParseError)
  72. }
  73. err, AccessLogLevelConfigData = ConvertLogLevelConfig(ConfigData.LogConfig.AccessLoggerDetailConfig)
  74. if err != nil {
  75. fmt.Println("failed parse config YAML in semantics: ", err)
  76. os.Exit(ExitCode_YAMLSemanticsError)
  77. }
  78. ErrorLogOutputLevel = transLevelStringIntoLevel(ConfigData.LogConfig.ErrorLoggerDetailConfig.OutputLevel)
  79. fmt.Println("Config Loaded.")
  80. err = yaml.Unmarshal(jdata, &ConfigData)
  81. if err != nil {
  82. fmt.Println("failed parse config YAML: ", err)
  83. os.Exit(ExitCode_YAMLParseError)
  84. }
  85. if ConfigData.CommonConfig.IsDebugModeOn {
  86. fmt.Println("Debug Mode is Enable.")
  87. fmt.Println("Print All Config For Debug:")
  88. tmpyaml, err := yaml.Marshal(ConfigData)
  89. if err != nil {
  90. fmt.Println("Failed Print Config: ", err)
  91. } else {
  92. fmt.Printf("%v\n\n", string(tmpyaml))
  93. }
  94. }
  95. }
  96. func InitLogger() {
  97. cfg := ConfigData.LogConfig.RollingLogConfig.ErrLogCfg
  98. elw := &lumberjack.Logger{
  99. Filename: cfg.FileName,
  100. MaxSize: cfg.MaxSize,
  101. MaxAge: cfg.MaxAge,
  102. MaxBackups: cfg.MaxBackups,
  103. LocalTime: cfg.UseLocalTime,
  104. Compress: cfg.Compress,
  105. }
  106. _, _ = fmt.Fprintln(elw, "Lumberjack Logger Write Test")
  107. ErrorLog = NewCommonLogger(ErrorLogOutputLevel, elw)
  108. ErrorLog.SetFormatter(
  109. ConfigData.LogConfig.ErrorLoggerDetailConfig.LogFormat,
  110. ConfigData.LogConfig.ErrorLoggerDetailConfig.UseUTC,
  111. )
  112. cfg = ConfigData.LogConfig.RollingLogConfig.AccessLogCfg
  113. AccessLog = NewAccessLogger()
  114. if ConfigData.LogConfig.AccessLogEnable {
  115. alw := &lumberjack.Logger{
  116. Filename: cfg.FileName,
  117. MaxSize: cfg.MaxSize,
  118. MaxAge: cfg.MaxAge,
  119. MaxBackups: cfg.MaxBackups,
  120. LocalTime: cfg.UseLocalTime,
  121. Compress: cfg.Compress,
  122. }
  123. AccessLog.UseLogger(
  124. alw,
  125. AccessLogLevelConfigData,
  126. ConfigData.LogConfig.AccessLoggerDetailConfig.LogFormat,
  127. ConfigData.LogConfig.AccessLoggerDetailConfig.UseUTC,
  128. )
  129. }
  130. }