package config import ( "fmt" "io/ioutil" "net/url" "git.andreafazzi.eu/andrea/oef/reflect" yaml "gopkg.in/yaml.v2" ) const ( LOG_LEVEL_OFF = iota LOG_LEVEL_INFO LOG_LEVEL_DEBUG ) const ( CreateLabel = iota ReadAllLabel ReadLabel UpdateLabel DeleteLabel ) type PathPattern struct { PathPattern string RedirectPattern string Methods []string Permission int } type ConfigT struct { // Domain Url string Domain string // Language Language string // Logging LogLevel int `yaml:"log_level"` // Secret keys Keys struct { CookieStoreKey string `yaml:"cookie_store_key"` JWTSigningKey string `yaml:"jwt_signing_key"` } // Admin credentials Admin struct { Username string Password string } // Subscriber credentials Subscriber struct { Password string } // Handlers Handlers struct { PathPatterns map[string]PathPattern APIPathPatterns map[string]PathPattern Permissions map[string]map[string][]int } // Database Orm struct { Connection string Options string Reset bool AutoMigrate bool Regenerate bool } Smtp struct { Host string Port int Username string Password string From string Cc string Bcc string Subject string } } var ( actions []string = []string{"Create", "ReadAll", "Read", "Update", "Delete"} ) // ReadFile reads the config file placed at the given path. func ReadFile(path string, config *ConfigT) error { cf, err := ioutil.ReadFile(path) if err != nil { return err } if err := yaml.Unmarshal(cf, config); err != nil { return err } return nil } // Read reads the config data from the given slice. func Read(data []byte, config *ConfigT) error { if err := yaml.Unmarshal(data, config); err != nil { return err } return nil } func (pp PathPattern) RedirectPath(model string, id ...uint) string { if len(id) > 0 { return fmt.Sprintf(pp.RedirectPattern, model, id[0], model) } return fmt.Sprintf(pp.RedirectPattern, model, model) } func (pp PathPattern) Path(model string) string { return fmt.Sprintf(pp.PathPattern, model) } func (c *ConfigT) ReadAllPath(model interface{}, path, format string) string { return path + "?" + c.query(model, ReadAllLabel, format).Encode() } func (c *ConfigT) ReadPath(model interface{}, path, format string) string { return path + "?" + c.query(model, ReadLabel, format).Encode() } func (c *ConfigT) CreatePath(model interface{}, path, format string) string { return path + "?" + c.query(model, CreateLabel, format).Encode() } func (c *ConfigT) ReadAllPattern() PathPattern { return c.Handlers.PathPatterns[actions[ReadAllLabel]] } func (c *ConfigT) ReadPattern() PathPattern { return c.Handlers.PathPatterns[actions[ReadLabel]] } func (c *ConfigT) query(model interface{}, action int, format string) url.Values { var tplContent string values := make(url.Values) values.Add("format", format) switch action { case CreateLabel: tplContent = reflect.ModelNameLowerPlural(model) + "_add_update" case ReadAllLabel: tplContent = reflect.ModelNameLowerPlural(model) case ReadLabel: tplContent = reflect.ModelNameLowerPlural(model) + "_show" } values.Add("tpl_content", tplContent) values.Add("tpl_layout", "base") // FIXME: use config value return values }