-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathconfig.go
More file actions
170 lines (132 loc) · 2.98 KB
/
config.go
File metadata and controls
170 lines (132 loc) · 2.98 KB
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
162
163
164
165
166
167
168
169
170
package main
import (
"encoding/json"
"fmt"
"os"
"sync"
"github.com/astaxie/beego/logs"
)
type UserInfo struct {
UserName string
Password string
}
type TlsInfo struct {
CA string
Cert string
Key string
}
type Config struct {
ServerDir string
DeleteEnable bool
UploadEnable bool
AuthEnable bool
AuthUsers []UserInfo
ListenAddr string
ListenPort int64
Timeout int64
HttpsEnable bool
HttpsInfo TlsInfo
ZipEnable bool
AutoStartup bool
}
var configCache = Config{
ServerDir: "",
DeleteEnable: true,
UploadEnable: true,
AuthEnable: false,
AuthUsers: make([]UserInfo, 0),
ListenAddr: "0.0.0.0",
ListenPort: 9000,
Timeout: 0,
HttpsEnable: false,
HttpsInfo: TlsInfo{CA: "", Cert: "", Key: ""},
ZipEnable: false,
AutoStartup: false,
}
var configFilePath string
var configLock sync.Mutex
func configSyncToFile() error {
configLock.Lock()
defer configLock.Unlock()
value, err := json.MarshalIndent(configCache, "\t", " ")
if err != nil {
logs.Error("json marshal config fail, %s", err.Error())
return err
}
return os.WriteFile(configFilePath, value, 0664)
}
func ConfigGet() *Config {
return &configCache
}
func UserListSave(userList []UserInfo) error {
configCache.AuthUsers = userList
return configSyncToFile()
}
func UserEnableSave(flag bool) error {
configCache.AuthEnable = flag
return configSyncToFile()
}
func ServerDirSave(dir string) error {
configCache.ServerDir = dir
return configSyncToFile()
}
func DeleteEnableSave(flag bool) error {
configCache.DeleteEnable = flag
return configSyncToFile()
}
func UploadEnableSave(flag bool) error {
configCache.UploadEnable = flag
return configSyncToFile()
}
func ListenAddressSave(addr string) error {
configCache.ListenAddr = addr
return configSyncToFile()
}
func ListenPortSave(port int64) error {
configCache.ListenPort = port
return configSyncToFile()
}
func ListenTimeoutSave(port int64) error {
configCache.Timeout = port
return configSyncToFile()
}
func HttpsEnableSave(flag bool) error {
configCache.HttpsEnable = flag
return configSyncToFile()
}
func HttpsInfoSave(info TlsInfo) error {
configCache.HttpsInfo = info
return configSyncToFile()
}
func ZipEnableSave(flag bool) error {
configCache.ZipEnable = flag
return configSyncToFile()
}
func AutoStartupSave(flag bool) error {
configCache.AutoStartup = flag
return configSyncToFile()
}
func ConfigInit() error {
configFilePath = fmt.Sprintf("%s%c%s", ConfigDirGet(), os.PathSeparator, "config.json")
_, err := os.Stat(configFilePath)
if err != nil {
err = configSyncToFile()
if err != nil {
logs.Error("config sync to file fail, %s", err.Error())
return err
}
}
value, err := os.ReadFile(configFilePath)
if err != nil {
logs.Error("read config file from app data dir fail, %s", err.Error())
configSyncToFile()
return err
}
err = json.Unmarshal(value, &configCache)
if err != nil {
logs.Error("json unmarshal config fail, %s", err.Error())
configSyncToFile()
return err
}
return nil
}