From 72e9ee3e43410d2d4f1af49840d1f75e5e9e08b9 Mon Sep 17 00:00:00 2001 From: tfasano1 Date: Mon, 4 Sep 2023 15:20:21 -0500 Subject: [PATCH] Use TOML for config --- README.md | 6 +++--- config/config.go | 38 +++++++++++++++----------------------- env_example.json | 17 ----------------- env_example.toml | 14 ++++++++++++++ go.mod | 2 ++ go.sum | 2 ++ 6 files changed, 36 insertions(+), 43 deletions(-) delete mode 100644 env_example.json create mode 100644 env_example.toml diff --git a/README.md b/README.md index 45cbd62..b7ca6cb 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ fine with getting your hands dirty, but I plan on having it ready to go for more - Minimal user login/registration + sessions - Config file handling - Scheduled tasks -- Entire website compiles into a single binary (~10mb) (excluding env.json) +- Entire website compiles into a single binary (~10mb) (excluding env.toml) - Minimal dependencies (just standard library, postgres driver, and experimental package for bcrypt)
@@ -41,7 +41,7 @@ fine with getting your hands dirty, but I plan on having it ready to go for more 1. Clone 2. Delete the git folder, so you can start tracking in your own repo 3. Run `go get` to install dependencies -4. Copy env_example.json to env.json and fill in the values +4. Copy env_example.toml to env.toml and fill in the values 5. Run `go run main.go` to start the server 6. Rename the occurences of "GoWeb" to your app name 7. Start building your app! @@ -62,4 +62,4 @@ fine with getting your hands dirty, but I plan on having it ready to go for more - You and you alone are responsible for the security and everything else regarding your application. - It is not required, but I ask that when you use this project you give me credit by linking to this repository. - I also ask that when releasing self-hosted or other end-user applications that you release it under - the [GPLv3](https://www.gnu.org/licenses/gpl-3.0.html) license. This too is not required, but I would appreciate it. \ No newline at end of file + the [GPLv3](https://www.gnu.org/licenses/gpl-3.0.html) license. This too is not required, but I would appreciate it. diff --git a/config/config.go b/config/config.go index a107b28..c34ef09 100644 --- a/config/config.go +++ b/config/config.go @@ -9,45 +9,37 @@ import ( type Configuration struct { Db struct { - Ip string `json:"DbIp"` - Port string `json:"DbPort"` - Name string `json:"DbName"` - User string `json:"DbUser"` - Password string `json:"DbPassword"` - AutoMigrate bool `json:"DbAutoMigrate"` + Ip string `toml:"DbIp"` + Port string `toml:"DbPort"` + Name string `toml:"DbName"` + User string `toml:"DbUser"` + Password string `toml:"DbPassword"` + AutoMigrate bool `toml:"DbAutoMigrate"` } Listen struct { - Ip string `json:"HttpIp"` - Port string `json:"HttpPort"` + Ip string `toml:"HttpIp"` + Port string `toml:"HttpPort"` } Template struct { - BaseName string `json:"BaseTemplateName"` + BaseName string `toml:"BaseTemplateName"` } } // LoadConfig loads and returns a configuration struct func LoadConfig() Configuration { - c := flag.String("c", "env.json", "Path to the json configuration file") + c := flag.String("c", "env.toml", "Path to the toml configuration file") flag.Parse() - file, err := os.Open(*c) + file, err := os.ReadFile(*c) if err != nil { - panic("unable to open JSON config file: " + err.Error()) + panic("Unable to read TOML config file: " + err.Error()) } - defer func(file *os.File) { - err := file.Close() - if err != nil { - slog.Error("unable to close JSON config file: ", err) - } - }(file) - - decoder := json.NewDecoder(file) - Config := Configuration{} - err = decoder.Decode(&Config) + var Config Configuration + _, err = toml.Decode(string(file), &Config) if err != nil { - panic("unable to decode JSON config file: " + err.Error()) + panic("Unable to decode TOML config file: " + err.Error()) } return Config diff --git a/env_example.json b/env_example.json deleted file mode 100644 index 0708fdc..0000000 --- a/env_example.json +++ /dev/null @@ -1,17 +0,0 @@ -{ - "Db": { - "DbIp": "127.0.0.1", - "DbPort": "5432", - "DbName": "database", - "DbUser": "user", - "DbPassword": "password", - "DbAutoMigrate": true - }, - "Listen": { - "HttpIp": "127.0.0.1", - "HttpPort": "8090" - }, - "Template": { - "BaseTemplateName": "templates/base.html" - } -} \ No newline at end of file diff --git a/env_example.toml b/env_example.toml new file mode 100644 index 0000000..a68f1b7 --- /dev/null +++ b/env_example.toml @@ -0,0 +1,14 @@ +[Db] +DbIp = "127.0.0.1" +DbPort = "5432" +DbName = "test" +DbUser = "postgres" +DbPassword = "postgres" +DbAutoMigrate = true + +[Listen] +HttpIp = "127.0.0.1" +HttpPort = "8090" + +[Template] +BaseTemplateName = "templates/base.html" diff --git a/go.mod b/go.mod index f333db0..ef61cf0 100644 --- a/go.mod +++ b/go.mod @@ -6,3 +6,5 @@ require ( github.com/lib/pq v1.10.9 golang.org/x/crypto v0.12.0 ) + +require github.com/BurntSushi/toml v1.3.2 // indirect diff --git a/go.sum b/go.sum index 69cfc49..1a50e63 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8= +github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=