Skip to content

Commit

Permalink
feat: added configuration files validator
Browse files Browse the repository at this point in the history
  • Loading branch information
DeeStarks committed Aug 9, 2022
1 parent 6608c65 commit 56e5909
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 6 deletions.
51 changes: 45 additions & 6 deletions utils/serializer.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package utils

import (
"errors"
"log"
"os"
"strings"

"gopkg.in/yaml.v3"
)

type AppConf struct {
Name string `yaml:"name"` // Name of application
Type string `yaml:"type"` // "server" or "static"
Listeners []string `yaml:"listeners"` // Load will be automatically balanced across listners. Required if "Renderer" is "server"
RootDirectory string `yaml:"root"` // Path to root directory. Required for "static" rendering
ClientAddr string `yaml:"client"` // Client address
Tunnelled bool `yaml:"tunnelled"` // Share service to a remote network. This will be redundant if the "ClientAddr" is set
Name string `yaml:"name" json:"name"` // Name of application
Type string `yaml:"type" json:"type"` // "server" or "static"
Listeners []string `yaml:"listeners" json:"listeners,omitempty"` // Load will be automatically balanced across listners. Required if "Renderer" is "server"
RootDirectory string `yaml:"root" json:"root_directory,omitempty"` // Path to root directory. Required for "static" rendering
ClientAddr string `yaml:"client" json:"client_address,omitempty"` // Client address
Tunnelled bool `yaml:"tunnelled" json:"tunnelled"` // Share service to a remote network. This will be redundant if the "ClientAddr" is set
}

// Deserialize app yaml file
Expand All @@ -35,3 +37,40 @@ func DeserializeAppYAML(file *os.File) (AppConf, error) {
}
return conf, nil
}

// Validate and fix errors in configuration
func ValidateConf(conf AppConf) (AppConf, error) {
// Set name to lowercase letters
conf.Name = strings.ToLower(conf.Name)

// Validate type
conf.Type = strings.ToLower(conf.Type)
if conf.Type != "server" && conf.Type != "static" {
return AppConf{}, errors.New("expected type of \"server\" or \"static\"")
}

if conf.Type == "server" {
if len(conf.Listeners) == 0 {
return AppConf{}, errors.New("type of \"server\" requires at least a \"listener\" or a server address")
}

// Set the static root as empty
conf.RootDirectory = ""
}

// RootDirectory
if conf.Type == "static" {
if conf.RootDirectory == "" {
return AppConf{}, errors.New("expected \"root\" directory for a \"static\" type")
}

// Empty listeners
conf.Listeners = []string{}
}

// Set "Tunnelled" to false if "ClientAddr" is passed
if len(conf.ClientAddr) > 0 {
conf.Tunnelled = false
}
return conf, nil
}
88 changes: 88 additions & 0 deletions utils/serializer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package utils_test

import (
"reflect"
"testing"

"github.com/DeeStarks/conoid/utils"
)

func TestCValidateConf(t *testing.T) {
tests := []struct {
conf utils.AppConf
expected utils.AppConf
}{
{
conf: utils.AppConf{
Name: "test1",
Type: "server",
RootDirectory: ".",
},
expected: utils.AppConf{},
},
{
conf: utils.AppConf{
Name: "test2",
Type: "static",
Listeners: []string{
"127.0.0.1:8000",
},
},
expected: utils.AppConf{},
},
{
conf: utils.AppConf{
Name: "test3",
Type: "server",
Listeners: []string{
"127.0.0.1:8000",
},
ClientAddr: "124.567.785.34:33004",
Tunnelled: true,
},
expected: utils.AppConf{
Name: "test3",
Type: "server",
Listeners: []string{
"127.0.0.1:8000",
},
ClientAddr: "124.567.785.34:33004",
Tunnelled: false,
},
},
{
conf: utils.AppConf{
Name: "test4",
Type: "static",
RootDirectory: "./myapp/",
ClientAddr: "",
Tunnelled: true,
},
expected: utils.AppConf{
Name: "test4",
Type: "static",
Listeners: []string{},
RootDirectory: "./myapp/",
ClientAddr: "",
Tunnelled: true,
},
},
{
conf: utils.AppConf{
Name: "test5",
Type: "mytype",
RootDirectory: "./myapp/",
ClientAddr: "",
Tunnelled: true,
},
expected: utils.AppConf{},
},
}

for _, tc := range tests {
conf, _ := utils.ValidateConf(tc.conf)
if !reflect.DeepEqual(conf, tc.expected) {
t.Errorf("Expected %v, got %v", tc.expected, conf)
}
}
}

0 comments on commit 56e5909

Please sign in to comment.