Reorganise repo

This commit is contained in:
Fred Boniface
2024-04-05 22:23:42 +01:00
parent 7bba8407d5
commit 493fb41da8
35 changed files with 2 additions and 2 deletions

9
helpers/basicAuth.go Normal file
View File

@@ -0,0 +1,9 @@
package helpers
import "encoding/base64"
// Provides a BasicAuth string
func BasicAuth(username, password string) string {
authString := username + ":" + password
return base64.StdEncoding.EncodeToString([]byte(authString))
}

22
helpers/config.go Normal file
View File

@@ -0,0 +1,22 @@
package helpers
import (
"os"
)
// Version Constants
const versionNum string = "2024.3.0"
const versionSuffix string = "beta"
const Version string = versionNum + "-" + versionSuffix
// Environment Variables
var Runtime string = getRuntime()
// Functions
func getRuntime() string {
var runtimeEnv string = os.Getenv("runtime")
if runtimeEnv == "" {
runtimeEnv = "unknown"
}
return runtimeEnv
}

132
helpers/config_loader.go Normal file
View File

@@ -0,0 +1,132 @@
package helpers
import (
"errors"
"fmt"
"os"
"strings"
)
type ConfigParameter struct {
EnvVarName string `json:"env_var_name"`
ConfFilePath string `json:"conf_file_path"`
DefaultValue string `json:"default_value"`
FailIfAbsent bool `json:"fail_if_absent"`
}
type Configuration struct {
VstpOn bool `json:"vstp_on"`
NrodUser string `json:"nrod_user"`
NrodPass string `json:"nrod_pass"`
DbHost string `json:"db_host"`
DbPass string `json:"db_pass"`
DbUser string `json:"db_user"`
DbPort string `json:"db_port"`
}
func LoadConfig() (*Configuration, error) {
configParams := map[string]ConfigParameter{
"vstp_on": {
EnvVarName: "OWL_VSTP_ON",
ConfFilePath: "/owl/conf/vstp/on",
DefaultValue: "on",
FailIfAbsent: false,
},
"nrod_user": {
EnvVarName: "OWL_NROD_USER",
ConfFilePath: "/owl/conf/nrod/user",
FailIfAbsent: true,
},
"nrod_pass": {
EnvVarName: "OWL_NROD_PASS",
ConfFilePath: "/owl/conf/nrod/pass",
FailIfAbsent: true,
},
"db_host": {
EnvVarName: "OWL_DB_HOST",
ConfFilePath: "/owl/conf/db/host",
DefaultValue: "localhost",
FailIfAbsent: false,
},
"db_port": {
EnvVarName: "OWL_DB_PORT",
ConfFilePath: "/owl/conf/db/port",
DefaultValue: "27017",
FailIfAbsent: false,
},
"db_user": {
EnvVarName: "OWL_DB_USER",
ConfFilePath: "/owl/conf/db/user",
FailIfAbsent: true,
},
"db_pass": {
EnvVarName: "OWL_DB_PASS",
ConfFilePath: "/owl/conf/db/pass",
FailIfAbsent: true,
},
}
config := &Configuration{}
for key, param := range configParams {
if val, ok := os.LookupEnv(param.EnvVarName); ok {
config.setConfigValue(key, val)
continue
}
if data, err := os.ReadFile(param.ConfFilePath); err == nil {
config.setConfigValue(key, string(data))
continue
}
if param.DefaultValue != "" {
config.setConfigValue(key, param.DefaultValue)
continue
}
if param.FailIfAbsent {
return nil, errors.New("Failed to load configuration: " + key + " is required but not set")
}
}
return config, nil
}
// Applies configuration strings to the configuration struct
func (c *Configuration) setConfigValue(key, value string) {
value = strings.TrimSpace(value)
switch key {
case "nrod_user":
c.NrodUser = value
case "nrod_pass":
c.NrodPass = value
case "db_host":
c.DbHost = value
case "db_port":
c.DbPort = value
case "db_user":
c.DbUser = value
case "db_pass":
c.DbPass = value
case "vstp_on":
if value == "on" {
c.VstpOn = true
} else {
c.VstpOn = false
}
}
}
// Provides a method to print the configuration struct. Only when the DEBUG env is set to true
func (c *Configuration) PrintConfig() {
if os.Getenv("DEBUG") == "true" {
fmt.Println("Configuration:")
fmt.Println("VstpOn: ", c.VstpOn)
fmt.Println("NrodUser: ", c.NrodUser)
fmt.Println("NrodPass: ", c.NrodPass)
fmt.Println("DbHost: ", c.DbHost)
fmt.Println("DbUser: ", c.DbUser)
fmt.Println("DbPass: ", c.DbPass)
fmt.Println("DbPort: ", c.DbPort)
}
}

27
helpers/vstp.go Normal file
View File

@@ -0,0 +1,27 @@
package helpers
// An error with the VSTP messages is that speed values are shown incorrectly, but not for all services
// This maps the displayed speed to the correct speed.
var SpeedMap = map[string]string{
"22": "10",
"34": "15",
"56": "20",
"67": "30",
"78": "35",
"89": "40",
"101": "45",
"112": "50",
"123": "55",
"134": "60",
"157": "70",
"168": "75",
"179": "80",
"195": "87",
"201": "90",
"213": "95",
"224": "100",
"246": "110",
"280": "125",
"314": "140",
"417": "186",
}