budget-go/config/config.go
2024-09-25 11:21:23 +02:00

75 lines
1.3 KiB
Go

package config
import (
"fmt"
"log"
"gopkg.in/ini.v1"
)
type database struct {
Dsn string
}
type logging struct {
Level string
}
type security struct {
Secret string
}
type server struct {
BaseUrl string
Address string
Port int
}
type collabora struct {
Url string
BudgetFile string
}
type file struct {
Path string
}
func (c *collabora) HostingDiscoveryUrl() string {
return fmt.Sprintf("%s/hosting/discovery", c.Url)
}
type Config struct {
Database database
Log logging
Security security
Server server
Collabora collabora
File file
}
var config *Config
func Get() *Config {
if config == nil {
config = &Config{}
}
return config
}
func (c *Config) Load(file string) {
cfg, err := ini.Load(file)
if err != nil {
log.Fatal(err)
}
config.Database.Dsn = cfg.Section("database").Key("dsn").String()
config.Log.Level = cfg.Section("log").Key("level").String()
config.Security.Secret = cfg.Section("security").Key("secret").String()
config.Server.Address = cfg.Section("server").Key("address").String()
config.Server.Port, _ = cfg.Section("server").Key("port").Int()
config.Server.BaseUrl = cfg.Section("server").Key("base_url").String()
config.Collabora.Url = cfg.Section("collabora").Key("url").String()
config.File.Path = cfg.Section("file").Key("path").String()
}