diff options
author | Laria Carolin Chabowski <laria@laria.me> | 2021-01-01 14:46:54 +0100 |
---|---|---|
committer | Laria Carolin Chabowski <laria@laria.me> | 2021-01-01 14:46:54 +0100 |
commit | 85473656174b1b1d6221d3bb76cc12fa5f7f7e8d (patch) | |
tree | 37349eee4f64781f06e8597dc4c457801eb03a47 /config | |
download | laria.me-85473656174b1b1d6221d3bb76cc12fa5f7f7e8d.tar.gz laria.me-85473656174b1b1d6221d3bb76cc12fa5f7f7e8d.tar.bz2 laria.me-85473656174b1b1d6221d3bb76cc12fa5f7f7e8d.zip |
Initial commit
Diffstat (limited to 'config')
-rw-r--r-- | config/config.go | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..074c24c --- /dev/null +++ b/config/config.go @@ -0,0 +1,49 @@ +package config + +import ( + "encoding/json" + "os" + "path" +) + +type Config struct { + ContentRoot string + DbDsn string + TemplatePath string + StaticPath string `json:",omitempty"` + HttpLaddr string + Secret string + UpdateUrl string +} + +func loadConfig(configPath string) (*Config, error) { + f, err := os.Open(configPath) + if err != nil { + return nil, err + } + defer f.Close() + + var conf Config + + dec := json.NewDecoder(f) + if err = dec.Decode(&conf); err != nil { + return nil, err + } + + return &conf, nil +} + +func LoadConfig(configPath string) (*Config, error) { + if configPath == "" { + configDir, err := os.UserConfigDir() + if err != nil { + return nil, err + } + + configPath = path.Join(configDir, "laria.me", "config.json") + + return loadConfig(configPath) + } + + return loadConfig(configPath) +} |