config.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package config
  2. import (
  3. "io/ioutil"
  4. yaml "gopkg.in/yaml.v2"
  5. )
  6. const (
  7. LOG_LEVEL_OFF = iota
  8. LOG_LEVEL_INFO
  9. LOG_LEVEL_DEBUG
  10. )
  11. type ConfigT struct {
  12. // Domain
  13. Domain string
  14. // Logging
  15. LogLevel int `yaml:"log_level"`
  16. // Secret keys
  17. Keys struct {
  18. CookieStoreKey string `yaml:"cookie_store_key"`
  19. JWTSigningKey string `yaml:"jwt_signing_key"`
  20. }
  21. // Admin credentials
  22. Admin struct {
  23. Username string
  24. Password string
  25. }
  26. // Database
  27. Orm struct {
  28. Connection string
  29. Options string
  30. Reset bool
  31. AutoMigrate bool
  32. Regenerate bool
  33. }
  34. }
  35. var Config *ConfigT
  36. func init() {
  37. Config = new(ConfigT)
  38. }
  39. // ReadFile reads the config file placed at the given path.
  40. func ReadFile(path string, config *ConfigT) error {
  41. cf, err := ioutil.ReadFile(path)
  42. if err != nil {
  43. return err
  44. }
  45. if err := yaml.Unmarshal(cf, config); err != nil {
  46. return err
  47. }
  48. return nil
  49. }
  50. // Read reads the config data from the given slice.
  51. func Read(data []byte, config *ConfigT) error {
  52. if err := yaml.Unmarshal(data, config); err != nil {
  53. return err
  54. }
  55. return nil
  56. }