Updating config to TOML (JSON sucks! I hate it! We shouldn't use it!)

This commit is contained in:
🐙PiperYxzzy
2025-10-13 20:23:06 +02:00
parent 0f93876c8b
commit acd23c2f45
6 changed files with 32 additions and 21 deletions

View File

@@ -1,2 +1,4 @@
# gin-gonic-prepack # gin-gonic-prepack
A pre-packaged Gin API with user and administrative structs, login and JWT authentication, and PSQL persistence. A pre-packaged Gin API with user and administrative structs, login and JWT authentication, and PSQL persistence.
# To run tests

View File

@@ -1,9 +1,11 @@
package config package config
import ( import (
"encoding/json" "io"
"log" "log"
"os" "os"
"github.com/BurntSushi/toml"
) )
type StackConfiguration struct { type StackConfiguration struct {
@@ -36,21 +38,27 @@ func GetConfigPath(filename string) string {
if Environment == "" { if Environment == "" {
Environment = "dev" Environment = "dev"
} }
return Environment + "/" + filename return "config/" + Environment + "/" + filename
} }
func LoadConfig() { func LoadConfig() {
file, err := os.Open(GetConfigPath("conf.json")) file, err := os.Open(GetConfigPath("conf.toml"))
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer file.Close() defer file.Close()
dec := json.NewDecoder(file)
if err := dec.Decode(&configInternal); err != nil { b, err := io.ReadAll(file)
if err != nil {
panic(err)
}
err = toml.Unmarshal(b, &configInternal)
if err != nil {
panic(err) panic(err)
} }
configInternal.ConfigLoaded = true configInternal.ConfigLoaded = true
log.Printf("Loaded Config for stack " + Environment) log.Printf("Loaded Config for stack '%s': %+v", Environment, configInternal)
} }

View File

@@ -1,15 +0,0 @@
{
"AllowFreshAdminGeneration": true,
"AdminEmails": ["admin@admin.invalid"],
"AdminHmacEnv": "ADMIN_HMAC_ENV",
"UserHmacEnv": "USER_HMAC_ENV",
"AuthedRateLimitConfig": "ratelimit.auth.json",
"UnauthedRateLimitConfig": "ratelimit.unauth.json",
"DbDialect": "sqlite",
"DbUrl": "prepack.db",
"DbUsername": "",
"DbPasswordSecret": "",
"DbPort": "",
"DbName": ""
}

13
config/dev/conf.toml Normal file
View File

@@ -0,0 +1,13 @@
AllowFreshAdminGeneration = true
AdminEmails = ["admin@admin.invalid"]
AdminHmacEnv = "ADMIN_HMAC_ENV"
UserHmacEnv = "USER_HMAC_ENV"
AuthedRateLimitConfig = "ratelimit.auth.json"
UnauthedRateLimitConfig = "ratelimit.unauth.json"
DbDialect = "sqlite"
DbUrl = "prepack.db"
DbUsername = ""
DbPasswordSecret = ""
DbPort = ""
DbName = ""

1
go.mod
View File

@@ -5,6 +5,7 @@ go 1.18
require github.com/gin-gonic/gin v1.7.7 require github.com/gin-gonic/gin v1.7.7
require ( require (
github.com/BurntSushi/toml v1.5.0 // indirect
github.com/KyleBanks/depth v1.2.1 // indirect github.com/KyleBanks/depth v1.2.1 // indirect
github.com/PuerkitoBio/purell v1.1.1 // indirect github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect

2
go.sum
View File

@@ -1,4 +1,6 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/toml v1.5.0 h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg=
github.com/BurntSushi/toml v1.5.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc= github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc=
github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE= github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE=
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs= github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=