Move structs to core package

This commit is contained in:
TwinProduction
2019-09-06 20:25:31 -04:00
parent eea38c8618
commit ee479be716
6 changed files with 153 additions and 81 deletions

View File

@ -1,24 +1,15 @@
package config
import (
"github.com/TwinProduction/gatus/core"
"gopkg.in/yaml.v2"
"io/ioutil"
)
type Config struct {
Services []Service `yaml:"services"`
Services []*core.Service `yaml:"services"`
}
type Service struct {
Name string `yaml:"name"`
Url string `yaml:"url"`
Interval uint `yaml:"interval"`
FailureThreshold uint `yaml:"failure-threshold"`
Conditions []Condition `yaml:"conditions"`
}
type Condition string
var config *Config
func Get() *Config {
@ -41,5 +32,13 @@ func ReadConfigurationFile(fileName string) *Config {
func ParseConfigBytes(yamlBytes []byte) *Config {
config = &Config{}
yaml.Unmarshal(yamlBytes, config)
for _, service := range config.Services {
if service.FailureThreshold == 0 {
service.FailureThreshold = 1
}
if service.Interval == 0 {
service.Interval = 10
}
}
return config
}

View File

@ -7,16 +7,14 @@ func TestParseConfigBytes(t *testing.T) {
services:
- name: twinnation
url: https://twinnation.org/actuator/health
interval: 10
interval: 15
failure-threshold: 3
conditions:
- "$STATUS == 200"
- name: github
url: https://github.com
interval: 9
failure-threshold: 2
conditions:
- "$STATUS != 405"
- "$STATUS != 400"
- "$STATUS != 500"
`))
if len(config.Services) != 2 {
@ -28,17 +26,17 @@ services:
if config.Services[1].Url != "https://github.com" {
t.Errorf("URL should have been %s", "https://github.com")
}
if config.Services[0].Interval != 10 {
t.Errorf("Interval should have been %d", 10)
if config.Services[0].Interval != 15 {
t.Errorf("Interval should have been %d", 15)
}
if config.Services[1].Interval != 9 {
t.Errorf("Interval should have been %d", 9)
if config.Services[1].Interval != 10 {
t.Errorf("Interval should have been %d, because it is the default value", 10)
}
if config.Services[0].FailureThreshold != 3 {
t.Errorf("FailureThreshold should have been %d", 3)
}
if config.Services[1].FailureThreshold != 2 {
t.Errorf("FailureThreshold should have been %d", 2)
if config.Services[1].FailureThreshold != 1 {
t.Errorf("FailureThreshold should have been %d, because it is the default value", 1)
}
if len(config.Services[0].Conditions) != 1 {
t.Errorf("There should have been %d conditions", 1)