config.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package config
  2. import (
  3. "git.qianqiusoft.com/qianqiusoft/light-apiengine/logs"
  4. "os"
  5. )
  6. type ApiConfig struct {
  7. RunMode string
  8. HttpPort int64
  9. LogMode string
  10. AppName string
  11. DataSource string
  12. SyncDb bool
  13. AutoRefresh bool
  14. EnableLdapServer bool
  15. LdapPort int64
  16. }
  17. var AppConfig ApiConfig
  18. var _config *Config = nil
  19. func init() {
  20. ParseConfig()
  21. logs.Debug(AppConfig)
  22. }
  23. func ParseConfig() {
  24. var err error
  25. configPath := "conf/app.conf"
  26. workEnv := os.Getenv("WORKENV")
  27. if workEnv != "" {
  28. configPath = "conf/app-" + workEnv + ".conf"
  29. }
  30. _config, err = LoadConfiguration(configPath)
  31. if err != nil {
  32. logs.Error(err.Error())
  33. AppConfig.AppName = ""
  34. AppConfig.HttpPort = 8080
  35. AppConfig.LogMode = "debug"
  36. AppConfig.RunMode = "debug"
  37. AppConfig.DataSource = ""
  38. AppConfig.SyncDb = true
  39. AppConfig.AutoRefresh = false
  40. } else {
  41. AppConfig.AppName = _config.String("app_name", "")
  42. AppConfig.HttpPort = _config.Integer("http_port", 8080)
  43. AppConfig.LogMode = _config.String("log_mode", "debug")
  44. AppConfig.RunMode = _config.String("run_mode", "debug")
  45. AppConfig.DataSource = _config.String("data_source", "")
  46. AppConfig.SyncDb = _config.Boolean("sync_db", true)
  47. AppConfig.AutoRefresh = _config.Boolean("auto_refresh", false)
  48. AppConfig.EnableLdapServer = _config.Boolean("enable_ldap_server", true)
  49. AppConfig.LdapPort = _config.Integer("ldap_port", 389)
  50. }
  51. }
  52. func (c *ApiConfig) GetKey(key string) string {
  53. if _config == nil {
  54. ParseConfig()
  55. }
  56. return _config.String(key, "")
  57. }
  58. func (c *ApiConfig) GetInt(key string) int64 {
  59. if _config == nil {
  60. ParseConfig()
  61. }
  62. return _config.Integer(key, 0)
  63. }
  64. func (c *ApiConfig) GetBool(key string, def bool) bool {
  65. if _config == nil {
  66. ParseConfig()
  67. }
  68. return _config.Boolean(key, def)
  69. }