-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
62 lines (51 loc) · 1.41 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"github.com/rs/zerolog"
"github.com/spf13/viper"
"errors"
"os"
)
const (
ConfigPathDev string = "/home/mr-papi/SoftwareCode/Projects/mypeople"
ConfigPathSystem string = "/etc/mypeople/"
ConfigPathUser string = "/home/mr-papi/.config/mypeople"
ConfigFileName string = "mypeople-config"
ConfigFileType string = "toml"
)
type configuration struct {
LogFile string `mapstructure:"LogFile"`
DbDriver string `mapstructure:"DbDriver"`
DbSource string `mapstructure:"DbSource"`
DbSourceMem string `mapstructure:"DbSourceMem"`
DbSourceDev string `mapstructure:"DbSourceDev"`
}
var (
Config configuration
LogFile *os.File
Logger zerolog.Logger
)
func InitConfig() (err error) {
viper.SetConfigName(ConfigFileName)
viper.SetConfigType(ConfigFileType)
viper.AddConfigPath(ConfigPathDev)
viper.AddConfigPath(ConfigPathSystem)
viper.AddConfigPath(ConfigPathUser)
err = viper.ReadInConfig()
if err != nil {
panic(errors.New("failed to load config file"))
}
err = viper.Unmarshal(&Config)
if err != nil {
panic(errors.New("failed to save config"))
}
return nil
}
func InitLogger() (err error) {
LogFile, err = os.OpenFile(Config.LogFile, os.O_WRONLY|os.O_APPEND, os.ModePerm)
if err != nil {
panic(errors.New("failed to open logfile"))
}
fileWriter := zerolog.New(LogFile).With().Logger()
Logger = zerolog.New(fileWriter).With().Timestamp().Logger()
return nil
}