Add support for environment variables in configuration file

This commit is contained in:
TwinProduction 2019-12-04 17:27:27 -05:00
parent 753435dc37
commit c90ca8fa85
9 changed files with 26 additions and 21 deletions

View File

@ -17,13 +17,15 @@ services:
url: https://twinnation.org/actuator/health
interval: 15s # Duration to wait between every status check (opt. default: 10s)
conditions:
- "$STATUS == 200"
- "[STATUS] == 200"
- name: github
url: https://api.github.com/healthz
conditions:
- "$STATUS == 200"
- "[STATUS] == 200"
```
Note that you can also add environment variables in the your configuration file (i.e. `$DOMAIN`, `${DOMAIN}`)
## Docker

View File

@ -4,8 +4,8 @@ services:
url: https://twinnation.org/actuator/health
interval: 15s
conditions:
- "$STATUS == 200"
- "[STATUS] == 200"
- name: github
url: https://api.github.com/healthz
conditions:
- "$STATUS == 200"
- "[STATUS] == 200"

View File

@ -64,6 +64,9 @@ func readConfigurationFile(fileName string) (config *Config, err error) {
}
func parseAndValidateConfigBytes(yamlBytes []byte) (config *Config, err error) {
// Expand environment variables
yamlBytes = []byte(os.ExpandEnv(string(yamlBytes)))
// Parse configuration file
err = yaml.Unmarshal(yamlBytes, &config)
// Check if the configuration file at least has services.
if config == nil || len(config.Services) == 0 {

View File

@ -13,12 +13,12 @@ services:
url: https://twinnation.org/actuator/health
interval: 15s
conditions:
- "$STATUS == 200"
- "[STATUS] == 200"
- name: github
url: https://api.github.com/healthz
conditions:
- "$STATUS != 400"
- "$STATUS != 500"
- "[STATUS] != 400"
- "[STATUS] != 500"
`))
if err != nil {
t.Error("No error should've been returned")
@ -53,7 +53,7 @@ services:
- name: twinnation
url: https://twinnation.org/actuator/health
conditions:
- "$STATUS == 200"
- "[STATUS] == 200"
`))
if err != nil {
t.Error("No error should've been returned")
@ -76,7 +76,7 @@ services:
- name: twinnation
url: https://twinnation.org/actuator/health
conditions:
- "$STATUS == 200"
- "[STATUS] == 200"
`))
if err != nil {
t.Error("No error should've been returned")

View File

@ -140,9 +140,9 @@ func sanitizeAndResolve(list []string, result *Result) []string {
for _, element := range list {
element = strings.TrimSpace(element)
switch strings.ToUpper(element) {
case "$STATUS":
case "[STATUS]":
element = strconv.Itoa(result.HttpStatus)
case "$IP":
case "[IP]":
element = result.Ip
default:
}

View File

@ -5,7 +5,7 @@ import (
)
func TestEvaluateWithIp(t *testing.T) {
condition := Condition("$IP == 127.0.0.1")
condition := Condition("[IP] == 127.0.0.1")
result := &Result{Ip: "127.0.0.1"}
condition.Evaluate(result)
if !result.ConditionResults[0].Success {
@ -14,7 +14,7 @@ func TestEvaluateWithIp(t *testing.T) {
}
func TestEvaluateWithStatus(t *testing.T) {
condition := Condition("$STATUS == 201")
condition := Condition("[STATUS] == 201")
result := &Result{HttpStatus: 201}
condition.Evaluate(result)
if !result.ConditionResults[0].Success {
@ -23,7 +23,7 @@ func TestEvaluateWithStatus(t *testing.T) {
}
func TestEvaluateWithFailure(t *testing.T) {
condition := Condition("$STATUS == 200")
condition := Condition("[STATUS] == 200")
result := &Result{HttpStatus: 500}
condition.Evaluate(result)
if result.ConditionResults[0].Success {
@ -32,7 +32,7 @@ func TestEvaluateWithFailure(t *testing.T) {
}
func TestIntegrationEvaluateConditions(t *testing.T) {
condition := Condition("$STATUS == 200")
condition := Condition("[STATUS] == 200")
service := Service{
Name: "GitHub",
Url: "https://api.github.com/healthz",
@ -48,7 +48,7 @@ func TestIntegrationEvaluateConditions(t *testing.T) {
}
func TestIntegrationEvaluateConditionsWithFailure(t *testing.T) {
condition := Condition("$STATUS == 500")
condition := Condition("[STATUS] == 500")
service := Service{
Name: "GitHub",
Url: "https://api.github.com/healthz",

View File

@ -4,12 +4,12 @@ services:
url: https://twinnation.org/actuator/health
interval: 10s
conditions:
- "$STATUS == 200"
- "[STATUS] == 200"
- name: GitHub
url: https://api.github.com/healthz
conditions:
- "$STATUS == 200"
- "[STATUS] == 200"
- name: Example
url: https://example.com/
conditions:
- "$STATUS == 200"
- "[STATUS] == 200"

View File

@ -3,4 +3,4 @@ services:
url: http://example.org
interval: 30s
conditions:
- "$STATUS == 200"
- "[STATUS] == 200"