settings.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package settings
  2. import (
  3. "github.com/spf13/cast"
  4. "gopkg.in/ini.v1"
  5. "log"
  6. "strings"
  7. "time"
  8. )
  9. var Conf *ini.File
  10. var (
  11. buildTime string
  12. LastModified string
  13. )
  14. type Server struct {
  15. HttpHost string `json:"http_host"`
  16. HttpPort string `json:"http_port"`
  17. RunMode string `json:"run_mode"`
  18. JwtSecret string `json:"jwt_secret"`
  19. NodeSecret string `json:"node_secret"`
  20. HTTPChallengePort string `json:"http_challenge_port"`
  21. Email string `json:"email"`
  22. Database string `json:"database"`
  23. StartCmd string `json:"start_cmd"`
  24. CADir string `json:"ca_dir"`
  25. Demo bool `json:"demo"`
  26. PageSize int `json:"page_size"`
  27. GithubProxy string `json:"github_proxy"`
  28. }
  29. type Nginx struct {
  30. AccessLogPath string `json:"access_log_path"`
  31. ErrorLogPath string `json:"error_log_path"`
  32. ConfigDir string `json:"config_dir"`
  33. PIDPath string `json:"pid_path"`
  34. TestConfigCmd string `json:"test_config_cmd"`
  35. ReloadCmd string `json:"reload_cmd"`
  36. RestartCmd string `json:"restart_cmd"`
  37. }
  38. type OpenAI struct {
  39. BaseUrl string `json:"base_url"`
  40. Token string `json:"token"`
  41. Proxy string `json:"proxy"`
  42. Model string `json:"model"`
  43. }
  44. var ServerSettings = Server{
  45. HttpHost: "0.0.0.0",
  46. HttpPort: "9000",
  47. RunMode: "debug",
  48. HTTPChallengePort: "9180",
  49. Database: "database",
  50. StartCmd: "login",
  51. Demo: false,
  52. PageSize: 10,
  53. CADir: "",
  54. GithubProxy: "",
  55. }
  56. var NginxSettings = Nginx{
  57. AccessLogPath: "",
  58. ErrorLogPath: "",
  59. }
  60. var OpenAISettings = OpenAI{}
  61. var ConfPath string
  62. var sections = map[string]interface{}{
  63. "server": &ServerSettings,
  64. "nginx": &NginxSettings,
  65. "openai": &OpenAISettings,
  66. }
  67. func init() {
  68. t := time.Unix(cast.ToInt64(buildTime), 0)
  69. LastModified = strings.ReplaceAll(t.Format(time.RFC1123), "UTC", "GMT")
  70. }
  71. func Init(confPath string) {
  72. ConfPath = confPath
  73. Setup()
  74. }
  75. func Setup() {
  76. var err error
  77. Conf, err = ini.LooseLoad(ConfPath)
  78. if err != nil {
  79. log.Fatalf("setting.Setup: %v\n", err)
  80. }
  81. MapTo()
  82. }
  83. func MapTo() {
  84. for k, v := range sections {
  85. mapTo(k, v)
  86. }
  87. }
  88. func ReflectFrom() {
  89. for k, v := range sections {
  90. reflectFrom(k, v)
  91. }
  92. }
  93. func mapTo(section string, v interface{}) {
  94. err := Conf.Section(section).MapTo(v)
  95. if err != nil {
  96. log.Fatalf("Cfg.MapTo %s err: %v", section, err)
  97. }
  98. }
  99. func reflectFrom(section string, v interface{}) {
  100. log.Print(section, v)
  101. err := Conf.Section(section).ReflectFrom(v)
  102. if err != nil {
  103. log.Fatalf("Cfg.ReflectFrom %s err: %v", section, err)
  104. }
  105. }
  106. func Save() (err error) {
  107. err = Conf.SaveTo(ConfPath)
  108. if err != nil {
  109. return
  110. }
  111. Setup()
  112. return
  113. }