-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
63 lines (53 loc) · 1.36 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
63
package main
import (
"fmt"
"io/ioutil"
"github.com/rs/zerolog/log"
"gopkg.in/yaml.v2"
)
const ConfigFile = "config.yaml"
const LogFolder = "log"
var GLogPath string
// Config represents configuration for applicaton
var Gconfig Config = DefaultConfig
// type Config struct {
// port string `yaml:"port"`
// }
type Config struct {
Port string `yaml:"port"`
Authkey string `yaml:"authkey"`
Authkey_expired_timeout int64 `yaml:"authkey_expired_timeout"`
}
var DefaultConfig = Config{
Port: "8086",
Authkey_expired_timeout: 30,
Authkey: "",
}
func (this Config) LoadConfig() error {
if !IsExistFile(ConfigFile) {
DefaultConfig.WriteConfig(ConfigFile)
}
data, err := ioutil.ReadFile(ConfigFile)
if err != nil {
log.Fatal().Msgf("Cannot read config file %s : %v", ConfigFile, err)
return err
}
err = yaml.Unmarshal(data, &this)
if err != nil {
log.Fatal().Msgf("Parse YAML file %s faild: %v", ConfigFile, err)
}
return nil
}
func (this Config) WriteConfig(path string) error {
CreateFileIfNotExist(path)
data, err := yaml.Marshal(&DefaultConfig)
fmt.Printf("%s", data)
if err != nil {
log.Fatal().Msg("config to yaml faild ")
}
err = ioutil.WriteFile("config.yaml", data, 0755)
if err != nil {
log.Fatal().Msgf("write to %s faild", "config.yaml")
}
return err
}