2016-08-24 23:21:06 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
|
2018-12-17 09:59:44 +01:00
|
|
|
yaml "gopkg.in/yaml.v2"
|
2016-08-24 23:21:06 +02:00
|
|
|
|
2017-06-04 20:34:31 +02:00
|
|
|
"github.com/chihaya/chihaya/frontend/http"
|
|
|
|
"github.com/chihaya/chihaya/frontend/udp"
|
2016-08-24 23:21:06 +02:00
|
|
|
"github.com/chihaya/chihaya/middleware"
|
2017-02-21 06:58:57 +01:00
|
|
|
|
2018-10-23 20:06:52 +02:00
|
|
|
// Imports to register middleware drivers.
|
2017-12-23 20:54:51 +01:00
|
|
|
_ "github.com/chihaya/chihaya/middleware/clientapproval"
|
|
|
|
_ "github.com/chihaya/chihaya/middleware/jwt"
|
2018-09-10 18:06:21 +02:00
|
|
|
_ "github.com/chihaya/chihaya/middleware/torrentapproval"
|
2017-12-23 20:54:51 +01:00
|
|
|
_ "github.com/chihaya/chihaya/middleware/varinterval"
|
|
|
|
|
2018-10-23 20:06:52 +02:00
|
|
|
// Imports to register storage drivers.
|
2017-02-21 06:58:57 +01:00
|
|
|
_ "github.com/chihaya/chihaya/storage/memory"
|
2018-02-09 09:09:17 +01:00
|
|
|
_ "github.com/chihaya/chihaya/storage/redis"
|
2016-08-24 23:21:06 +02:00
|
|
|
)
|
|
|
|
|
2017-02-21 06:58:57 +01:00
|
|
|
type storageConfig struct {
|
|
|
|
Name string `yaml:"name"`
|
|
|
|
Config interface{} `yaml:"config"`
|
|
|
|
}
|
|
|
|
|
2017-04-30 04:29:27 +02:00
|
|
|
// Config represents the configuration used for executing Chihaya.
|
|
|
|
type Config struct {
|
2017-12-23 20:54:51 +01:00
|
|
|
middleware.ResponseConfig `yaml:",inline"`
|
|
|
|
PrometheusAddr string `yaml:"prometheus_addr"`
|
|
|
|
HTTPConfig http.Config `yaml:"http"`
|
|
|
|
UDPConfig udp.Config `yaml:"udp"`
|
|
|
|
Storage storageConfig `yaml:"storage"`
|
|
|
|
PreHooks []middleware.HookConfig `yaml:"prehooks"`
|
|
|
|
PostHooks []middleware.HookConfig `yaml:"posthooks"`
|
2016-08-24 23:21:06 +02:00
|
|
|
}
|
2016-09-01 03:09:46 +02:00
|
|
|
|
2017-12-23 20:54:51 +01:00
|
|
|
// PreHookNames returns only the names of the configured middleware.
|
|
|
|
func (cfg Config) PreHookNames() (names []string) {
|
|
|
|
for _, hook := range cfg.PreHooks {
|
|
|
|
names = append(names, hook.Name)
|
2016-09-01 03:09:46 +02:00
|
|
|
}
|
|
|
|
|
2017-12-23 20:54:51 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// PostHookNames returns only the names of the configured middleware.
|
|
|
|
func (cfg Config) PostHookNames() (names []string) {
|
|
|
|
for _, hook := range cfg.PostHooks {
|
|
|
|
names = append(names, hook.Name)
|
2016-09-01 03:09:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2017-04-30 04:29:27 +02:00
|
|
|
|
|
|
|
// ConfigFile represents a namespaced YAML configation file.
|
|
|
|
type ConfigFile struct {
|
|
|
|
Chihaya Config `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
|
|
|
|
}
|