GoWeb/config/config.go

46 lines
957 B
Go
Raw Permalink Normal View History

package config
import (
"flag"
2023-09-04 20:21:26 +00:00
"github.com/BurntSushi/toml"
"os"
)
type Configuration struct {
Db struct {
2023-09-04 20:20:21 +00:00
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 {
2023-09-04 20:20:21 +00:00
Ip string `toml:"HttpIp"`
Port string `toml:"HttpPort"`
}
Template struct {
2023-09-04 20:20:21 +00:00
BaseName string `toml:"BaseTemplateName"`
}
}
// LoadConfig loads and returns a configuration struct
func LoadConfig() Configuration {
2023-09-04 20:20:21 +00:00
c := flag.String("c", "env.toml", "Path to the toml configuration file")
flag.Parse()
2023-09-04 20:20:21 +00:00
file, err := os.ReadFile(*c)
if err != nil {
2023-09-04 20:20:21 +00:00
panic("Unable to read TOML config file: " + err.Error())
}
2023-09-04 20:20:21 +00:00
var Config Configuration
_, err = toml.Decode(string(file), &Config)
if err != nil {
2023-09-04 20:20:21 +00:00
panic("Unable to decode TOML config file: " + err.Error())
}
return Config
}