2016-08-24 23:21:06 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
|
|
|
|
httpfrontend "github.com/chihaya/chihaya/frontend/http"
|
|
|
|
udpfrontend "github.com/chihaya/chihaya/frontend/udp"
|
|
|
|
"github.com/chihaya/chihaya/middleware"
|
2016-09-02 02:30:53 +02:00
|
|
|
"github.com/chihaya/chihaya/middleware/clientapproval"
|
2016-09-01 03:09:46 +02:00
|
|
|
"github.com/chihaya/chihaya/middleware/jwt"
|
2016-10-24 14:16:21 +02:00
|
|
|
"github.com/chihaya/chihaya/middleware/varinterval"
|
2016-08-24 23:21:06 +02:00
|
|
|
"github.com/chihaya/chihaya/storage/memory"
|
|
|
|
)
|
|
|
|
|
2016-09-01 03:09:46 +02:00
|
|
|
type hookConfig struct {
|
|
|
|
Name string `yaml:"name"`
|
|
|
|
Config interface{} `yaml:"config"`
|
|
|
|
}
|
|
|
|
|
2016-08-24 23:21:06 +02:00
|
|
|
// ConfigFile represents a namespaced YAML configation file.
|
|
|
|
type ConfigFile struct {
|
|
|
|
MainConfigBlock struct {
|
2016-10-12 06:26:25 +02:00
|
|
|
middleware.Config `yaml:",inline"`
|
|
|
|
PrometheusAddr string `yaml:"prometheus_addr"`
|
|
|
|
HTTPConfig httpfrontend.Config `yaml:"http"`
|
|
|
|
UDPConfig udpfrontend.Config `yaml:"udp"`
|
|
|
|
Storage memory.Config `yaml:"storage"`
|
|
|
|
PreHooks []hookConfig `yaml:"prehooks"`
|
|
|
|
PostHooks []hookConfig `yaml:"posthooks"`
|
2016-08-24 23:21:06 +02:00
|
|
|
} `yaml:"chihaya"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseConfigFile returns a new ConfigFile given the path to a YAML
|
|
|
|
// configuration file.
|
|
|
|
//
|
|
|
|
// It supports relative and absolute paths and environment variables.
|
|
|
|
func ParseConfigFile(path string) (*ConfigFile, error) {
|
|
|
|
if path == "" {
|
|
|
|
return nil, errors.New("no config path specified")
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Open(os.ExpandEnv(path))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
contents, err := ioutil.ReadAll(f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var cfgFile ConfigFile
|
|
|
|
err = yaml.Unmarshal(contents, &cfgFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &cfgFile, nil
|
|
|
|
}
|
2016-09-01 03:09:46 +02:00
|
|
|
|
|
|
|
// CreateHooks creates instances of Hooks for all of the PreHooks and PostHooks
|
|
|
|
// configured in a ConfigFile.
|
|
|
|
func (cfg ConfigFile) CreateHooks() (preHooks, postHooks []middleware.Hook, err error) {
|
|
|
|
for _, hookCfg := range cfg.MainConfigBlock.PreHooks {
|
|
|
|
cfgBytes, err := yaml.Marshal(hookCfg.Config)
|
|
|
|
if err != nil {
|
|
|
|
panic("failed to remarshal valid YAML")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch hookCfg.Name {
|
|
|
|
case "jwt":
|
|
|
|
var jwtCfg jwt.Config
|
|
|
|
err := yaml.Unmarshal(cfgBytes, &jwtCfg)
|
|
|
|
if err != nil {
|
2016-09-02 02:30:53 +02:00
|
|
|
return nil, nil, errors.New("invalid JWT middleware config: " + err.Error())
|
2016-09-01 03:09:46 +02:00
|
|
|
}
|
2016-09-26 18:00:28 +02:00
|
|
|
hook, err := jwt.NewHook(jwtCfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, errors.New("invalid JWT middleware config: " + err.Error())
|
|
|
|
}
|
|
|
|
preHooks = append(preHooks, hook)
|
2016-09-02 02:30:53 +02:00
|
|
|
case "client approval":
|
|
|
|
var caCfg clientapproval.Config
|
|
|
|
err := yaml.Unmarshal(cfgBytes, &caCfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, errors.New("invalid client approval middleware config: " + err.Error())
|
|
|
|
}
|
|
|
|
hook, err := clientapproval.NewHook(caCfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, errors.New("invalid client approval middleware config: " + err.Error())
|
|
|
|
}
|
|
|
|
preHooks = append(preHooks, hook)
|
2016-10-24 14:16:21 +02:00
|
|
|
case "interval variation":
|
|
|
|
var viCfg varinterval.Config
|
|
|
|
err := yaml.Unmarshal(cfgBytes, &viCfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, errors.New("invalid interval variation middleware config: " + err.Error())
|
|
|
|
}
|
|
|
|
hook, err := varinterval.New(viCfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, errors.New("invalid interval variation middleware config: " + err.Error())
|
|
|
|
}
|
|
|
|
preHooks = append(preHooks, hook)
|
2016-09-01 03:09:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, hookCfg := range cfg.MainConfigBlock.PostHooks {
|
|
|
|
switch hookCfg.Name {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|