2019-11-04 15:00:46 +01:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2020-01-17 11:06:28 +01:00
|
|
|
"net/url"
|
2020-01-17 07:59:57 +01:00
|
|
|
|
|
|
|
"git.andreafazzi.eu/andrea/oef/reflect"
|
2020-01-17 11:06:28 +01:00
|
|
|
yaml "gopkg.in/yaml.v2"
|
2019-11-04 15:00:46 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
LOG_LEVEL_OFF = iota
|
|
|
|
LOG_LEVEL_INFO
|
|
|
|
LOG_LEVEL_DEBUG
|
2020-01-17 11:06:28 +01:00
|
|
|
)
|
2020-01-17 07:59:57 +01:00
|
|
|
|
2020-01-17 11:06:28 +01:00
|
|
|
const (
|
2020-01-17 07:59:57 +01:00
|
|
|
CreateLabel = iota
|
|
|
|
ReadAllLabel
|
|
|
|
ReadLabel
|
|
|
|
UpdateLabel
|
|
|
|
DeleteLabel
|
2019-11-04 15:00:46 +01:00
|
|
|
)
|
|
|
|
|
2020-01-17 07:59:57 +01:00
|
|
|
type PathPattern struct {
|
|
|
|
PathPattern string
|
|
|
|
RedirectPattern string
|
|
|
|
Methods []string
|
|
|
|
Permission int
|
|
|
|
}
|
|
|
|
|
2019-11-04 15:00:46 +01:00
|
|
|
type ConfigT struct {
|
|
|
|
|
|
|
|
// Domain
|
|
|
|
|
|
|
|
Url string
|
|
|
|
Domain string
|
|
|
|
|
2019-12-03 11:14:29 +01:00
|
|
|
// Language
|
|
|
|
|
|
|
|
Language string
|
|
|
|
|
2019-11-04 15:00:46 +01:00
|
|
|
// Logging
|
|
|
|
|
|
|
|
LogLevel int `yaml:"log_level"`
|
|
|
|
|
2020-01-31 12:29:28 +01:00
|
|
|
// Enable profiling
|
|
|
|
Profiling bool
|
|
|
|
|
2019-11-04 15:00:46 +01:00
|
|
|
// Secret keys
|
|
|
|
|
|
|
|
Keys struct {
|
|
|
|
CookieStoreKey string `yaml:"cookie_store_key"`
|
|
|
|
JWTSigningKey string `yaml:"jwt_signing_key"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Admin credentials
|
|
|
|
|
|
|
|
Admin struct {
|
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
}
|
|
|
|
|
2019-12-03 15:24:01 +01:00
|
|
|
// Subscriber credentials
|
|
|
|
|
|
|
|
Subscriber struct {
|
|
|
|
Password string
|
|
|
|
}
|
|
|
|
|
2020-01-17 07:59:57 +01:00
|
|
|
// Handlers
|
|
|
|
|
|
|
|
Handlers struct {
|
2020-01-31 12:29:28 +01:00
|
|
|
PathPatterns map[string]PathPattern
|
|
|
|
APIPathPatterns map[string]PathPattern
|
|
|
|
Permissions map[string]map[string][]int
|
2020-01-29 11:49:05 +01:00
|
|
|
AllowSessionURLQuery bool `yaml:"allow_session_url_query"`
|
2020-01-17 07:59:57 +01:00
|
|
|
}
|
|
|
|
|
2019-11-04 15:00:46 +01:00
|
|
|
// 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
|
2019-12-05 15:08:37 +01:00
|
|
|
Bcc string
|
2020-01-15 13:40:35 +01:00
|
|
|
Subject string
|
2019-11-04 15:00:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-17 07:59:57 +01:00
|
|
|
var (
|
|
|
|
actions []string = []string{"Create", "ReadAll", "Read", "Update", "Delete"}
|
|
|
|
)
|
2019-11-04 15:00:46 +01:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2020-01-17 07:59:57 +01:00
|
|
|
func (pp PathPattern) RedirectPath(model string, id ...uint) string {
|
|
|
|
if len(id) > 0 {
|
|
|
|
return fmt.Sprintf(pp.RedirectPattern, model, id[0], model)
|
2019-11-04 15:00:46 +01:00
|
|
|
}
|
2020-01-17 07:59:57 +01:00
|
|
|
return fmt.Sprintf(pp.RedirectPattern, model, model)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pp PathPattern) Path(model string) string {
|
|
|
|
return fmt.Sprintf(pp.PathPattern, model)
|
2019-11-04 15:00:46 +01:00
|
|
|
}
|
|
|
|
|
2020-01-17 12:53:35 +01:00
|
|
|
func (c *ConfigT) ReadAllPath(model interface{}, path, format string) string {
|
|
|
|
return path + "?" + c.query(model, ReadAllLabel, format).Encode()
|
2020-01-17 11:06:28 +01:00
|
|
|
}
|
|
|
|
|
2020-01-17 12:53:35 +01:00
|
|
|
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()
|
2020-01-17 12:11:13 +01:00
|
|
|
}
|
|
|
|
|
2020-01-23 17:54:41 +01:00
|
|
|
func (c *ConfigT) DeletePath(model interface{}, path, format string) string {
|
|
|
|
return path + "?format=html"
|
|
|
|
}
|
|
|
|
|
2020-01-20 12:07:02 +01:00
|
|
|
func (c *ConfigT) CreatePattern() PathPattern {
|
|
|
|
return c.Handlers.PathPatterns[actions[CreateLabel]]
|
|
|
|
}
|
|
|
|
|
2020-01-17 11:06:28 +01:00
|
|
|
func (c *ConfigT) ReadAllPattern() PathPattern {
|
|
|
|
return c.Handlers.PathPatterns[actions[ReadAllLabel]]
|
|
|
|
}
|
|
|
|
|
2020-01-17 12:11:13 +01:00
|
|
|
func (c *ConfigT) ReadPattern() PathPattern {
|
|
|
|
return c.Handlers.PathPatterns[actions[ReadLabel]]
|
|
|
|
}
|
|
|
|
|
2020-01-23 17:54:41 +01:00
|
|
|
func (c *ConfigT) DeletePattern() PathPattern {
|
|
|
|
return c.Handlers.PathPatterns[actions[DeleteLabel]]
|
|
|
|
}
|
|
|
|
|
2020-01-17 12:53:35 +01:00
|
|
|
func (c *ConfigT) query(model interface{}, action int, format string) url.Values {
|
|
|
|
var tplContent string
|
2020-01-17 12:11:13 +01:00
|
|
|
|
2020-01-17 11:06:28 +01:00
|
|
|
values := make(url.Values)
|
|
|
|
|
|
|
|
values.Add("format", format)
|
2020-01-17 12:53:35 +01:00
|
|
|
|
|
|
|
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)
|
2020-01-17 11:06:28 +01:00
|
|
|
values.Add("tpl_layout", "base") // FIXME: use config value
|
|
|
|
|
|
|
|
return values
|
2019-11-04 15:00:46 +01:00
|
|
|
}
|