2022-10-20 16:57:41 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
2023-09-04 20:21:26 +00:00
|
|
|
"github.com/BurntSushi/toml"
|
2022-10-20 16:57:41 +00:00
|
|
|
"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"`
|
2022-10-20 16:57:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Listen struct {
|
2023-09-04 20:20:21 +00:00
|
|
|
Ip string `toml:"HttpIp"`
|
|
|
|
Port string `toml:"HttpPort"`
|
2022-10-20 16:57:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Template struct {
|
2023-09-04 20:20:21 +00:00
|
|
|
BaseName string `toml:"BaseTemplateName"`
|
2022-10-20 16:57:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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")
|
2022-10-20 16:57:41 +00:00
|
|
|
flag.Parse()
|
2023-09-04 20:20:21 +00:00
|
|
|
file, err := os.ReadFile(*c)
|
2022-10-20 16:57:41 +00:00
|
|
|
if err != nil {
|
2023-09-04 20:20:21 +00:00
|
|
|
panic("Unable to read TOML config file: " + err.Error())
|
2022-10-20 16:57:41 +00:00
|
|
|
}
|
|
|
|
|
2023-09-04 20:20:21 +00:00
|
|
|
var Config Configuration
|
|
|
|
_, err = toml.Decode(string(file), &Config)
|
2022-10-20 16:57:41 +00:00
|
|
|
if err != nil {
|
2023-09-04 20:20:21 +00:00
|
|
|
panic("Unable to decode TOML config file: " + err.Error())
|
2022-10-20 16:57:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Config
|
|
|
|
}
|