57 lines
1.1 KiB
Go
57 lines
1.1 KiB
Go
// Package config defines the configuration of our app
|
|
package config
|
|
|
|
import (
|
|
"log"
|
|
"os"
|
|
"sync"
|
|
|
|
"gopkg.in/yaml.v2"
|
|
)
|
|
|
|
type YamlBackend struct {
|
|
Path string `yaml:"path"`
|
|
}
|
|
|
|
// Configuration defines the base configuration of the expenses app
|
|
type Configuration struct {
|
|
Port string `yaml:"port"`
|
|
AuthKey string `yaml:"authKey"`
|
|
DataBackend string `yaml:"dataBackend"`
|
|
YamlBackend YamlBackend `yaml:"yamlBackend"`
|
|
}
|
|
|
|
var (
|
|
instance *Configuration
|
|
once sync.Once
|
|
confPath string
|
|
)
|
|
|
|
// GetInstance returns the instance of the configuration of the app.
|
|
func GetInstance() *Configuration {
|
|
once.Do(func() {
|
|
instance = &Configuration{}
|
|
instance.loadConfiguration()
|
|
})
|
|
|
|
return instance
|
|
}
|
|
|
|
// SetConfig sets the path for the configuration file.
|
|
func SetConfig(path string) {
|
|
confPath = path
|
|
}
|
|
|
|
func (c *Configuration) loadConfiguration() {
|
|
file, err := os.ReadFile(confPath)
|
|
if err != nil {
|
|
log.Fatalf("Unable to open configuration file [%s]: %s", confPath,
|
|
err.Error())
|
|
}
|
|
|
|
err = yaml.Unmarshal(file, &instance)
|
|
if err != nil {
|
|
log.Fatalf("Error parsing JSON: %s", err.Error())
|
|
}
|
|
}
|