Adding not just the api but also some initial code for posting members
This commit is contained in:
@@ -1,15 +1,15 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
DBPath string `json:"db_path"`
|
||||
RedisHost string `json:"redis_host"`
|
||||
DBPath string `json:"db_path"`
|
||||
RedisHost string `json:"redis_host"`
|
||||
RedisPassword string `json:"redis_password"`
|
||||
}
|
||||
|
||||
@@ -17,6 +17,8 @@ var conf *Config
|
||||
var conf_path string = ".env"
|
||||
|
||||
func GetConfig() *Config {
|
||||
_ = GenerateEnvFileIfNotExists("./sjles-pta-vote.db")
|
||||
|
||||
if conf != nil {
|
||||
return conf
|
||||
}
|
||||
@@ -25,9 +27,8 @@ func GetConfig() *Config {
|
||||
|
||||
// TODO: Make this into a ini or toml file
|
||||
configContent, err := os.ReadFile(conf_path)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("Error reading .env file: ", err)
|
||||
log.Printf("Error reading .env file: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
@@ -42,11 +43,6 @@ func GetConfig() *Config {
|
||||
}
|
||||
}
|
||||
|
||||
if err := json.Unmarshal([]byte(configContent), conf); err != nil {
|
||||
fmt.Println("Error unmarshalling config file: ", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// TODO: Better mapping of key to json values
|
||||
// TODO: Better error checking if values are missing
|
||||
// TODO: Default values
|
||||
@@ -58,7 +54,7 @@ func GetConfig() *Config {
|
||||
} else if strings.Contains(key, "redis_password") {
|
||||
conf.RedisPassword = value
|
||||
} else {
|
||||
fmt.Println("Error, Unknown key value pair: ", key, " = ", value)
|
||||
log.Printf("Error, Unknown key value pair: %s = %s", key, value)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,4 +63,13 @@ func GetConfig() *Config {
|
||||
|
||||
func SetConfig(init_conf *Config) {
|
||||
conf = init_conf
|
||||
}
|
||||
|
||||
func GenerateEnvFileIfNotExists(dbPath string) error {
|
||||
_, err := os.Stat(".env")
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
envContent := fmt.Sprintf("db_path=\"%s\"\n", dbPath)
|
||||
return os.WriteFile(".env", []byte(envContent), 0644)
|
||||
}
|
||||
Reference in New Issue
Block a user