config.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package config
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "strings"
  6. yaml "gopkg.in/yaml.v2"
  7. )
  8. const (
  9. LOG_LEVEL_OFF = iota
  10. LOG_LEVEL_INFO
  11. LOG_LEVEL_DEBUG
  12. )
  13. type ConfigT struct {
  14. // Domain
  15. Url string
  16. Domain string
  17. // Logging
  18. LogLevel int `yaml:"log_level"`
  19. // Secret keys
  20. Keys struct {
  21. CookieStoreKey string `yaml:"cookie_store_key"`
  22. JWTSigningKey string `yaml:"jwt_signing_key"`
  23. }
  24. // Admin credentials
  25. Admin struct {
  26. Username string
  27. Password string
  28. }
  29. // Database
  30. Orm struct {
  31. Connection string
  32. Options string
  33. Reset bool
  34. AutoMigrate bool
  35. Regenerate bool
  36. }
  37. Ldap struct {
  38. Host string
  39. AdminCN string `yaml:"admin_cn"`
  40. AdminPassword string `yaml:"admin_password"`
  41. TeachersDN string `yaml:"teachers_dn"`
  42. PeopleDN string `yaml:"people_dn"`
  43. GroupsDN string `yaml:"groups_dn"`
  44. MailGIDNumber string `yaml:"mail_gid_number"`
  45. FirstUIDNumber string `yaml:"first_uid_number"`
  46. MailDirBasePath string `yaml:"maildir_base_path"`
  47. MailQuota string `yaml:"mail_quota"`
  48. }
  49. Smtp struct {
  50. Host string
  51. Port int
  52. Username string
  53. Password string
  54. From string
  55. Cc string
  56. }
  57. }
  58. var Config *ConfigT
  59. func init() {
  60. Config = new(ConfigT)
  61. }
  62. // ReadFile reads the config file placed at the given path.
  63. func ReadFile(path string, config *ConfigT) error {
  64. cf, err := ioutil.ReadFile(path)
  65. if err != nil {
  66. return err
  67. }
  68. if err := yaml.Unmarshal(cf, config); err != nil {
  69. return err
  70. }
  71. return nil
  72. }
  73. // Read reads the config data from the given slice.
  74. func Read(data []byte, config *ConfigT) error {
  75. if err := yaml.Unmarshal(data, config); err != nil {
  76. return err
  77. }
  78. return nil
  79. }
  80. func (conf *ConfigT) DomainDN() (out string) {
  81. for _, dc := range strings.Split(conf.Domain, ".") {
  82. out += "dc=" + dc + ","
  83. }
  84. return strings.TrimRight(out, ",")
  85. }
  86. func (conf *ConfigT) AdminCN() string {
  87. return fmt.Sprintf("cn=%s,%s", conf.Ldap.AdminCN, conf.DomainDN())
  88. }