forked from YaoApp/yao
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
145 lines (125 loc) · 2.94 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package config
import (
"errors"
"os"
"path/filepath"
"strings"
"github.com/caarlos0/env/v6"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/joho/godotenv"
"github.com/yaoapp/kun/exception"
"github.com/yaoapp/kun/log"
"github.com/yaoapp/yao/crypto"
)
// Conf 配置参数
var Conf Config
// LogOutput 日志输出
var LogOutput *os.File // 日志文件
// DSLExtensions the dsl file Extensions
var DSLExtensions = []string{".yao", ".json", ".jsonc"}
func init() {
filename, _ := filepath.Abs(filepath.Join(".", ".env"))
if _, err := os.Stat(filename); errors.Is(err, os.ErrNotExist) {
Conf = Load()
return
}
Conf = LoadFrom(filename)
if Conf.Mode == "production" {
Production()
} else if Conf.Mode == "development" {
Development()
}
}
// LoadFrom 从配置项中加载
func LoadFrom(envfile string) Config {
file, err := filepath.Abs(envfile)
if err != nil {
// log.Warn("Can't load env file. %s", err.Error())
return Load()
}
godotenv.Overload(file)
// if err != nil {
// // log.Warn("Can't load env file. %s", err.Error())
// }
return Load()
}
// Load 加载配置
func Load() Config {
cfg := Config{}
if err := env.Parse(&cfg); err != nil {
exception.New("Can't read config %s", 500, err.Error()).Throw()
}
cfg.Root, _ = filepath.Abs(cfg.Root)
// Studio Secret
if cfg.Studio.Secret == nil {
v, err := crypto.Hash(crypto.HashTypes["SHA256"], uuid.New().String())
if err != nil {
exception.New("Can't gengrate studio secret %s", 500, err.Error()).Throw()
}
cfg.Studio.Secret = []byte(strings.ToUpper(v))
cfg.Studio.Auto = true
}
return cfg
}
// Production 设定为生产环境
func Production() {
Conf.Mode = "production"
log.SetLevel(log.InfoLevel)
log.SetFormatter(log.TEXT)
if Conf.LogMode == "JSON" {
log.SetFormatter(log.JSON)
}
gin.SetMode(gin.ReleaseMode)
ReloadLog()
}
// Development 设定为开发环境
func Development() {
Conf.Mode = "development"
log.SetLevel(log.TraceLevel)
log.SetFormatter(log.TEXT)
if Conf.LogMode == "JSON" {
log.SetFormatter(log.JSON)
}
gin.SetMode(gin.DebugMode)
ReloadLog()
}
// ReloadLog 重新打开日志
func ReloadLog() {
CloseLog()
OpenLog()
}
// OpenLog 打开日志
func OpenLog() {
if Conf.Log != "" {
logfile, err := filepath.Abs(Conf.Log)
if err != nil {
log.With(log.F{"file": logfile}).Error(err.Error())
return
}
logpath := filepath.Dir(logfile)
if _, err := os.Stat(logpath); os.IsNotExist(err) {
if err := os.MkdirAll(logpath, os.ModePerm); err != nil {
log.With(log.F{"file": logfile}).Error(err.Error())
return
}
}
LogOutput, err = os.OpenFile(logfile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
log.With(log.F{"file": logfile}).Error(err.Error())
return
}
log.SetOutput(LogOutput)
gin.DefaultWriter = LogOutput
}
}
// CloseLog 关闭日志
func CloseLog() {
if LogOutput != nil {
err := LogOutput.Close()
if err != nil {
log.Error(err.Error())
return
}
}
}