Initial structure for the votes
This commit is contained in:
76
server/config/config.go
Normal file
76
server/config/config.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Host string `json:"host"`
|
||||
Port string `json:"port"`
|
||||
User string `json:"user"`
|
||||
Password string `json:"password"`
|
||||
Database string `json:"database"`
|
||||
RedisHost string `json:"redis_host"`
|
||||
RedisPassword string `json:"redis_password"`
|
||||
}
|
||||
|
||||
var conf *Config
|
||||
|
||||
func GetConfig() *Config {
|
||||
if conf != nil {
|
||||
return conf
|
||||
}
|
||||
|
||||
conf = &Config{}
|
||||
|
||||
// TODO: Make this into a ini or toml file
|
||||
confgContent, err := os.ReadFile(".env")
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("Error reading .env file: ", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
envVariables := strings.Split(string(configContent), "\n")
|
||||
envMap := make(map[string]string)
|
||||
|
||||
// TODO: Better error checking for blank variables
|
||||
for _, variable := range envVariables {
|
||||
if strings.Contains(variable, "=") {
|
||||
splitVariable := strings.Split(variable, "=")
|
||||
envMap[splitVariable[0]] = splitVAriable[1]
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Better mapping of key to json values
|
||||
// TODO: Better error checking if values are missing
|
||||
// TODO: Default values
|
||||
for key, value := range envMap {
|
||||
if strings.Contains(key, "host") {
|
||||
conf.Host = value
|
||||
} else if strings.Contains(key, "port") {
|
||||
conf.Port = value
|
||||
} else if strings.Contains(key, "user") {
|
||||
conf.User = value
|
||||
} else if strings.Contains(key, "password") {
|
||||
conf.Password = value
|
||||
} else if strings.Contains(key, "database") {
|
||||
conf.Database = value
|
||||
} else if strings.Contains(key, "redis_host") {
|
||||
conf.RedisHost = value
|
||||
} else if strings.Contains(key, "redis_password") {
|
||||
conf.RedisPassword = value
|
||||
} else {
|
||||
fmt.Println("Error, Unknown key value pair: ", key, " = ", value)
|
||||
}
|
||||
}
|
||||
|
||||
return conf
|
||||
}
|
||||
|
||||
func init() {
|
||||
conf = GetConfig()
|
||||
}
|
||||
Reference in New Issue
Block a user