Compare commits
4 Commits
master
...
toml_confi
Author | SHA1 | Date | |
---|---|---|---|
|
308198ee8b | ||
|
ac19e2515a | ||
|
60006b6e4e | ||
|
72e9ee3e43 |
@ -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
|
- Minimal user login/registration + sessions
|
||||||
- Config file handling
|
- Config file handling
|
||||||
- Scheduled tasks
|
- 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)
|
- Minimal dependencies (just standard library, postgres driver, and experimental package for bcrypt)
|
||||||
|
|
||||||
<hr>
|
<hr>
|
||||||
@ -41,7 +41,7 @@ fine with getting your hands dirty, but I plan on having it ready to go for more
|
|||||||
1. Clone
|
1. Clone
|
||||||
2. Delete the git folder, so you can start tracking in your own repo
|
2. Delete the git folder, so you can start tracking in your own repo
|
||||||
3. Run `go get` to install dependencies
|
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
|
5. Run `go run main.go` to start the server
|
||||||
6. Rename the occurences of "GoWeb" to your app name
|
6. Rename the occurences of "GoWeb" to your app name
|
||||||
7. Start building your app!
|
7. Start building your app!
|
||||||
|
@ -1,53 +1,44 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"flag"
|
"flag"
|
||||||
"log/slog"
|
"github.com/BurntSushi/toml"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Configuration struct {
|
type Configuration struct {
|
||||||
Db struct {
|
Db struct {
|
||||||
Ip string `json:"DbIp"`
|
Ip string `toml:"DbIp"`
|
||||||
Port string `json:"DbPort"`
|
Port string `toml:"DbPort"`
|
||||||
Name string `json:"DbName"`
|
Name string `toml:"DbName"`
|
||||||
User string `json:"DbUser"`
|
User string `toml:"DbUser"`
|
||||||
Password string `json:"DbPassword"`
|
Password string `toml:"DbPassword"`
|
||||||
AutoMigrate bool `json:"DbAutoMigrate"`
|
AutoMigrate bool `toml:"DbAutoMigrate"`
|
||||||
}
|
}
|
||||||
|
|
||||||
Listen struct {
|
Listen struct {
|
||||||
Ip string `json:"HttpIp"`
|
Ip string `toml:"HttpIp"`
|
||||||
Port string `json:"HttpPort"`
|
Port string `toml:"HttpPort"`
|
||||||
}
|
}
|
||||||
|
|
||||||
Template struct {
|
Template struct {
|
||||||
BaseName string `json:"BaseTemplateName"`
|
BaseName string `toml:"BaseTemplateName"`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadConfig loads and returns a configuration struct
|
// LoadConfig loads and returns a configuration struct
|
||||||
func LoadConfig() Configuration {
|
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()
|
flag.Parse()
|
||||||
file, err := os.Open(*c)
|
file, err := os.ReadFile(*c)
|
||||||
if err != nil {
|
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) {
|
var Config Configuration
|
||||||
err := file.Close()
|
_, err = toml.Decode(string(file), &Config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("unable to close JSON config file: ", err)
|
panic("Unable to decode TOML config file: " + err.Error())
|
||||||
}
|
|
||||||
}(file)
|
|
||||||
|
|
||||||
decoder := json.NewDecoder(file)
|
|
||||||
Config := Configuration{}
|
|
||||||
err = decoder.Decode(&Config)
|
|
||||||
if err != nil {
|
|
||||||
panic("unable to decode JSON config file: " + err.Error())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return Config
|
return Config
|
||||||
|
@ -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"
|
|
||||||
}
|
|
||||||
}
|
|
14
env_example.toml
Normal file
14
env_example.toml
Normal file
@ -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"
|
2
go.mod
2
go.mod
@ -6,3 +6,5 @@ require (
|
|||||||
github.com/lib/pq v1.10.9
|
github.com/lib/pq v1.10.9
|
||||||
golang.org/x/crypto v0.13.0
|
golang.org/x/crypto v0.13.0
|
||||||
)
|
)
|
||||||
|
|
||||||
|
require github.com/BurntSushi/toml v1.3.2
|
||||||
|
2
go.sum
2
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 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
|
||||||
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||||
golang.org/x/crypto v0.13.0 h1:mvySKfSWJ+UKUii46M40LOvyWfN0s2U+46/jDd0e6Ck=
|
golang.org/x/crypto v0.13.0 h1:mvySKfSWJ+UKUii46M40LOvyWfN0s2U+46/jDd0e6Ck=
|
||||||
|
Loading…
Reference in New Issue
Block a user