-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
137 lines (119 loc) · 3.53 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
package main
import (
"encoding/json"
"github.com/sirupsen/logrus"
"os"
"strconv"
"strings"
"time"
)
var Conf = NewConfig("./config/config.json")
type Config struct {
//Host string `json:"host"`
//Port int `json:"port"`
//DBUser string `json:"db_user"`
//DBPass string `json:"db_pass"`
//DBName string `json:"db_name"`
Redis struct {
Host string `json:"host"`
Port string `json:"port"`
}
MySQL struct {
GormDNS string `json:"gorm_dns"`
}
//MySQLRootPassword string `json:"mysql_root_password"`
//MySQLDatabase string `json:"mysql_database"`
//MySQLUser string `json:"mysql_user"`
//MySQLUserPassword string `json:"mysql_user_password"`
SMTP struct {
Switch string `json:"switch"`
Host string `json:"host"`
Port string `json:"port"`
Sender string `json:"sender"`
SenderPassword string `json:"email_sender_password"`
SendTimePoint1 string `json:"send_time_point_1"`
SendTimePoint2 string `json:"send_time_point_2"`
P1 time.Time
P2 time.Time
}
//日志存放路径
LogPath string `json:"log_path"`
//管理员用户
Admins []string `json:"admins"`
}
func NewConfig(filePath string) *Config {
var config Config
data, err := os.ReadFile(filePath)
if err != nil {
logrus.Fatal(err)
}
err = json.Unmarshal(data, &config)
if err != nil {
logrus.Fatal(err)
}
config.overrideWithEnvVars()
return &config
}
func (conf *Config) overrideWithEnvVars() {
now := time.Now()
if value, exists := os.LookupEnv("EMAIL_SWITCH"); exists {
conf.SMTP.Switch = value
}
if value, exists := os.LookupEnv("REDIS_HOST"); exists {
conf.Redis.Host = value
}
if value, exists := os.LookupEnv("REDIS_PORT"); exists {
conf.Redis.Port = value
}
if value, exists := os.LookupEnv("GORM_DNS"); exists {
conf.MySQL.GormDNS = value
}
if value, exists := os.LookupEnv("EMAIL_SENDER"); exists {
conf.SMTP.Sender = value
}
if value, exists := os.LookupEnv("EMAIL_SENDER"); exists {
conf.SMTP.Sender = value
}
if value, exists := os.LookupEnv("EMAIL_SENDER"); exists {
conf.SMTP.Sender = value
}
if value, exists := os.LookupEnv("EMAIL_SENDER_PASSWORD"); exists {
conf.SMTP.SenderPassword = value
}
if value, exists := os.LookupEnv("SEND_MAIL_TIME_POINT1"); exists {
hour, _ := strconv.Atoi(value)
conf.SMTP.P1 = time.Date(now.Year(), now.Month(), now.Day(), hour, 0, 0, 0, now.Location())
} else {
hour, _ := strconv.Atoi(conf.SMTP.SendTimePoint1)
conf.SMTP.P1 = time.Date(now.Year(), now.Month(), now.Day(), hour, 0, 0, 0, now.Location())
}
if value, exists := os.LookupEnv("SEND_MAIL_TIME_POINT2"); exists {
hour, _ := strconv.Atoi(value)
conf.SMTP.P2 = time.Date(now.Year(), now.Month(), now.Day(), hour, 0, 0, 0, now.Location())
} else {
hour, _ := strconv.Atoi(conf.SMTP.SendTimePoint2)
conf.SMTP.P2 = time.Date(now.Year(), now.Month(), now.Day(), hour, 0, 0, 0, now.Location())
}
if value, exists := os.LookupEnv("LOG_PATH"); exists {
conf.LogPath = value
}
if value, exists := os.LookupEnv("ADMINS"); exists {
admins := strings.Split(value, ";")
conf.Admins = admins
}
}
/*
func main() {
// 加载配置文件
config, err := loadConfigFromFile("config.json")
if err != nil {
log.Fatalf("Error loading config file: %v", err)
}
// 覆盖配置文件中的值(如果环境变量存在)
overrideWithEnvVars(config)
// 使用最终的配置
fmt.Printf("App Name: %s\n", config.AppName)
fmt.Printf("Server Host: %s\n", config.Host)
fmt.Printf("Database User: %s\n", config.DBUser)
}
*/