Updating config to TOML (JSON sucks! I hate it! We shouldn't use it!)
This commit is contained in:
@@ -1,2 +1,4 @@
|
||||
# gin-gonic-prepack
|
||||
A pre-packaged Gin API with user and administrative structs, login and JWT authentication, and PSQL persistence.
|
||||
|
||||
# To run tests
|
||||
@@ -1,9 +1,11 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
)
|
||||
|
||||
type StackConfiguration struct {
|
||||
@@ -36,21 +38,27 @@ func GetConfigPath(filename string) string {
|
||||
if Environment == "" {
|
||||
Environment = "dev"
|
||||
}
|
||||
return Environment + "/" + filename
|
||||
return "config/" + Environment + "/" + filename
|
||||
}
|
||||
|
||||
func LoadConfig() {
|
||||
file, err := os.Open(GetConfigPath("conf.json"))
|
||||
file, err := os.Open(GetConfigPath("conf.toml"))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
configInternal.ConfigLoaded = true
|
||||
|
||||
log.Printf("Loaded Config for stack " + Environment)
|
||||
log.Printf("Loaded Config for stack '%s': %+v", Environment, configInternal)
|
||||
}
|
||||
|
||||
@@ -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
13
config/dev/conf.toml
Normal 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
1
go.mod
@@ -5,6 +5,7 @@ go 1.18
|
||||
require github.com/gin-gonic/gin v1.7.7
|
||||
|
||||
require (
|
||||
github.com/BurntSushi/toml v1.5.0 // indirect
|
||||
github.com/KyleBanks/depth v1.2.1 // indirect
|
||||
github.com/PuerkitoBio/purell v1.1.1 // indirect
|
||||
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
|
||||
|
||||
2
go.sum
2
go.sum
@@ -1,4 +1,6 @@
|
||||
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/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE=
|
||||
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
|
||||
|
||||
Reference in New Issue
Block a user