config.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package main
  2. import (
  3. "encoding/json"
  4. "flag"
  5. "fmt"
  6. "io/ioutil"
  7. "log"
  8. "os"
  9. "strings"
  10. )
  11. type GlobalConfig struct {
  12. Allowed_hosts string `json:"allowed_hosts"`
  13. Primary_host string `json:"primary_mail_host"`
  14. Verbose bool `json:"verbose"`
  15. Mysql_table string `json:"mail_table"`
  16. Mysql_db string `json:"mysql_db"`
  17. Mysql_host string `json:"mysql_host"`
  18. Mysql_pass string `json:"mysql_pass"`
  19. Mysql_user string `json:"mysql_user"`
  20. Servers []ServerConfig `json:"servers"`
  21. Pid_file string `json:"pid_file,omitempty"`
  22. Save_workers_size int `json:"save_workers_size"`
  23. Redis_expire_seconds int `json:"redis_expire_seconds"`
  24. Redis_interface string `json:"redis_interface"`
  25. }
  26. type ServerConfig struct {
  27. Is_enabled bool `json:"is_enabled"`
  28. Host_name string `json:"host_name"`
  29. Max_size int `json:"max_size"`
  30. Private_key_file string `json:"private_key_file"`
  31. Public_key_file string `json:"public_key_file"`
  32. Timeout int `json:"timeout"`
  33. Listen_interface string `json:"listen_interface"`
  34. Start_tls_on bool `json:"start_tls_on,omitempty"`
  35. Tls_always_on bool `json:"tls_always_on,omitempty"`
  36. Max_clients int `json:"max_clients"`
  37. Log_file string `json:"log_file"`
  38. }
  39. var mainConfig GlobalConfig
  40. var flagVerbose, flagIface, flagConfigFile string
  41. // config is read at startup, or when a SIG_HUP is caught
  42. func readConfig() {
  43. log.SetOutput(os.Stdout)
  44. // parse command line arguments
  45. if !flag.Parsed() {
  46. flag.StringVar(&flagConfigFile, "config", "goguerrilla.conf", "Path to the configuration file")
  47. flag.StringVar(&flagVerbose, "v", "n", "Verbose, [y | n] ")
  48. flag.StringVar(&flagIface, "if", "", "Interface and port to listen on, eg. 127.0.0.1:2525 ")
  49. flag.Parse()
  50. }
  51. // load in the config.
  52. b, err := ioutil.ReadFile(flagConfigFile)
  53. if err != nil {
  54. log.Fatalln("Could not read config file", err)
  55. }
  56. mainConfig = GlobalConfig{}
  57. err = json.Unmarshal(b, &mainConfig)
  58. //fmt.Println(theConfig)
  59. //fmt.Println(fmt.Sprintf("allowed hosts: %s", theConfig.Allowed_hosts))
  60. //log.Fatalln("Could not parse config file:", theConfig)
  61. if err != nil {
  62. fmt.Println("Could not parse config file:", err)
  63. log.Fatalln("Could not parse config file:", err)
  64. }
  65. // copy command line flag over so it takes precedence
  66. if len(flagVerbose) > 0 && strings.ToUpper(flagVerbose) == "Y" {
  67. mainConfig.Verbose = true
  68. }
  69. if len(flagIface) > 0 {
  70. mainConfig.Servers[0].Listen_interface = flagIface
  71. }
  72. // map the allow hosts for easy lookup
  73. if len(mainConfig.Allowed_hosts) > 0 {
  74. if arr := strings.Split(mainConfig.Allowed_hosts, ","); len(arr) > 0 {
  75. for i := 0; i < len(arr); i++ {
  76. allowedHosts[arr[i]] = true
  77. }
  78. }
  79. } else {
  80. log.Fatalln("Config error, GM_ALLOWED_HOSTS must be s string.")
  81. }
  82. if mainConfig.Pid_file == "" {
  83. mainConfig.Pid_file = "/var/run/go-guerrilla.pid"
  84. }
  85. return
  86. }