config.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. Domain string
  40. AdminCN string `yaml:"admin_cn"`
  41. AdminPassword string `yaml:"admin_password"`
  42. TeachersDN string `yaml:"teachers_dn"`
  43. PeopleDN string `yaml:"people_dn"`
  44. GroupsDN string `yaml:"groups_dn"`
  45. MailGIDNumber string `yaml:"mail_gid_number"`
  46. FirstUIDNumber string `yaml:"first_uid_number"`
  47. MailDirBasePath string `yaml:"maildir_base_path"`
  48. MailQuota string `yaml:"mail_quota"`
  49. }
  50. Smtp struct {
  51. Host string
  52. Port int
  53. Username string
  54. Password string
  55. From string
  56. Cc string
  57. }
  58. }
  59. var Config *ConfigT
  60. func init() {
  61. Config = new(ConfigT)
  62. }
  63. // ReadFile reads the config file placed at the given path.
  64. func ReadFile(path string, config *ConfigT) error {
  65. cf, err := ioutil.ReadFile(path)
  66. if err != nil {
  67. return err
  68. }
  69. if err := yaml.Unmarshal(cf, config); err != nil {
  70. return err
  71. }
  72. return nil
  73. }
  74. // Read reads the config data from the given slice.
  75. func Read(data []byte, config *ConfigT) error {
  76. if err := yaml.Unmarshal(data, config); err != nil {
  77. return err
  78. }
  79. return nil
  80. }
  81. func (conf *ConfigT) DomainDN() (out string) {
  82. for _, dc := range strings.Split(conf.Domain, ".") {
  83. out += "dc=" + dc + ","
  84. }
  85. return strings.TrimRight(out, ",")
  86. }
  87. func (conf *ConfigT) AdminCN() string {
  88. return fmt.Sprintf("cn=%s,%s", conf.Ldap.AdminCN, conf.DomainDN())
  89. }