-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
161 lines (148 loc) · 3.81 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package main
import (
"database/sql"
"flag"
"fmt"
"os"
"strconv"
"strings"
log "github.com/tengfei-xy/go-log"
"github.com/tengfei-xy/go-tools"
"gopkg.in/yaml.v3"
)
type appConfig struct {
Basic `yaml:"basic"`
Web `yaml:"web"`
SQL `yaml:"sql"`
db *sql.DB
}
type Basic struct {
ListenPort string `yaml:"listen"`
SavePath string `yaml:"savePath"`
ShareBaseLink string `yaml:"shareBaseLink"`
}
type Web struct {
FileMaxMB int64 `yaml:"fileMaxMB"`
SSLEnable bool `yaml:"sslEnable"`
SSLCERT string `yaml:"sslCERT"`
SSLKEY string `yaml:"sslKEY"`
}
type SQL struct {
APIEnable bool `yaml:"apiEnable"`
Username string `yaml:"username"`
Password string `yaml:"password"`
SYSFilename string `yaml:"sysFilename"`
}
type flagStruct struct {
config_file string
db_file string
version bool
}
func (app *appConfig) is_empty() {
if app.ListenPort == "" {
app.ListenPort = "0.0.0.0:25934"
log.Infof("监听端口使用强制参数:%s", app.ListenPort)
}
if app.SavePath == "" {
app.SavePath = "/data"
log.Infof("存储路径使用强制参数:%s", app.SavePath)
}
if app.ShareBaseLink == "" {
app.ShareBaseLink = "http://127.0.0.1:25934"
log.Infof("分享地址使用强制参数:%s", app.Basic.ShareBaseLink)
}
if app.FileMaxMB < 0 {
app.FileMaxMB = 100
log.Infof("最大文件使用强制参数:%d", app.FileMaxMB)
}
if app.SQL.SYSFilename == "" {
app.SQL.SYSFilename = "info.db"
log.Infof("数据库存储文件使用强制参数:%s", app.SQL.SYSFilename)
}
}
func init_flag() flagStruct {
var f flagStruct
flag.StringVar(&f.config_file, "c", "config.yaml", "打开配置文件")
flag.StringVar(&f.db_file, "d", "", "清空数据并导入SQL数据文件")
flag.BoolVar(&f.version, "v", false, "查看版本号")
flag.Parse()
return f
}
func init_config(flag flagStruct) {
if flag.version {
fmt.Println(version)
os.Exit(0)
}
log.Infof("读取配置文件")
l := []string{flag.config_file, "/data/config.yaml", "/config.yaml"}
for _, f := range l {
if !tools.FileExist(f) {
continue
}
log.Infof("配置文件: %s", flag.config_file)
yamlFile, err := os.ReadFile(flag.config_file)
if err != nil {
panic(err)
}
err = yaml.Unmarshal(yamlFile, &app)
if err != nil {
panic(err)
}
log.Infof("共享链接: %s", app.Basic.ShareBaseLink)
log.Infof("资源文件保存位置: %s", app.Basic.SavePath)
return
}
}
func init_env() {
if v := os.Getenv("SPSS_LISTEN"); v != "" {
log.Infof("SPSS_LISTEN=%s", v)
app.ListenPort = v
}
if v := os.Getenv("SPSS_SAVE_PATH"); v != "" {
log.Infof("SPSS_SAVE_PATH=%s", v)
app.Basic.SavePath = v
}
if v := os.Getenv("SPSS_SHARE_LINK"); v != "" {
log.Infof("SPSS_SHARE_LINK=%s", v)
app.Basic.ShareBaseLink = v
}
if v := os.Getenv("SPSS_WEB_FILE_MAX"); v != "" {
i, err := strconv.Atoi(v)
if err != nil {
app.Web.FileMaxMB = 100
log.Infof("最大文件使用强制参数:%d", app.FileMaxMB)
} else {
app.Web.FileMaxMB = int64(i)
}
log.Infof("SPSS_WEB_FILE_MAX=%s", v)
}
if v := os.Getenv("SPSS_WEB_SSL"); v == "true" || v == "TRUE" {
app.Web.SSLEnable = true
if v := os.Getenv("SPSS_WEB_SSL_CERT"); v != "" {
log.Infof("SPSS_WEB_SSL_CERT=%s", v)
app.Web.SSLCERT = v
}
if v := os.Getenv("SPSS_WEB_SSL_KEY"); v != "" {
log.Infof("SPSS_WEB_SSL_KEY=%s", v)
app.Web.SSLKEY = v
}
}
if v := os.Getenv("SPSS_DB_API"); v == "true" || v == "TRUE" {
log.Infof("SPSS_DB_API=%s", v)
app.SQL.APIEnable = true
}
if v := os.Getenv("SPSS_DB_AUTH"); v != "" {
if !strings.Contains(v, ":") {
log.Fatal("SPSS_DB_LOGIN变量格式错误")
}
l := strings.Split(v, ":")
app.SQL.APIEnable = true
app.SQL.Username = l[0]
app.SQL.Password = l[1]
log.Infof("SPSS_DB_AUTH=%s", v)
}
if v := os.Getenv("SPSS_DB_SAVE"); v != "" {
app.SQL.SYSFilename = v
}
app.is_empty()
}