api/pkg/plugins/http_mock/protocol_options.go
Peter Kurfer c3e362c8e5
Add some basic docs
- unify http_mock and dns_mock in config styles
- panic if pattern is not a valid regexp in http_mock
- add basic doc structure
2020-04-07 00:17:07 +02:00

49 lines
918 B
Go

package main
import (
"github.com/spf13/viper"
"regexp"
)
const (
rulesConfigKey = "rules"
patternConfigKey = "pattern"
responseConfigKey = "response"
)
type targetRule struct {
pattern *regexp.Regexp
response string
}
func (tr targetRule) Pattern() *regexp.Regexp {
return tr.pattern
}
func (tr targetRule) Response() string {
return tr.response
}
type httpOptions struct {
Rules []targetRule
}
func loadFromConfig(config *viper.Viper) httpOptions {
options := httpOptions{}
anonRules := config.Get(rulesConfigKey).([]interface{})
for _, i := range anonRules {
innerData := i.(map[interface{}]interface{})
if rulePattern, err := regexp.Compile(innerData[patternConfigKey].(string)); err == nil {
options.Rules = append(options.Rules, targetRule{
pattern: rulePattern,
response: innerData[responseConfigKey].(string),
})
} else {
panic(err)
}
}
return options
}