-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
280 lines (256 loc) · 8.3 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package kimg
import (
"io/ioutil"
"log"
"os"
"strconv"
"strings"
yaml "gopkg.in/yaml.v3"
)
// KimgConfig is configuration of kimg.
type KimgConfig struct {
Httpd struct {
Bind string `yaml:"bind,omitempty"`
URL string `yaml:"url,omitempty"`
Headers map[string]string `yaml:"headers,omitempty"`
Etag bool `yaml:"etag,omitempty"`
MaxAge int `yaml:"maxAge,omitempty"`
FormName string `yaml:"formName,omitempty"`
MaxSize int64 `yaml:"maxSize,omitempty"`
EnableWeb bool `yaml:"enableWeb,omitempty"`
} `yaml:"httpd,omitempty"`
Image struct {
Format string `yaml:"format,omitempty"`
Quality int `yaml:"quality,omitempty"`
AllowedTypes []string `yaml:"allowedTypes,omitempty"`
} `yaml:"image,omitempty"`
Logger struct {
Mode string `yaml:"mode,omitempty"`
Level string `yaml:"level,omitempty"`
File string `yaml:"file,omitempty"`
} `yaml:"logger,omitempty"`
Cache struct {
Mode string `yaml:"mode,omitempty"`
MaxSize int `yaml:"maxSize,omitempty"`
Memcache struct {
URL string `yaml:"url,omitempty"`
} `yaml:"memcache,omitempty"`
Redis struct {
URL string `yaml:"url,omitempty"`
} `yaml:"redis,omitempty"`
Memory struct {
Capacity int64 `yaml:"capacity,omitempty"`
} `yaml:"memory,omitempty"`
} `yaml:"cache,omitempty"`
Storage struct {
Mode string `yaml:"mode,omitempty"`
SaveNew bool `yaml:"saveNew,omitempty"`
File struct {
Root string `yaml:"root,omitempty"`
} `yaml:"file,omitempty"`
Minio struct {
Endpoint string `yaml:"endpoint,omitempty"`
AccessKeyID string `yaml:"accessKeyId,omitempty"`
SecretAccessKey string `yaml:"secretAccessKey,omitempty"`
Bucket string `yaml:"bucket,omitempty"`
UseSSL bool `yaml:"useSSL,omitempty"`
} `yaml:"minio,omitempty"`
} `yaml:"storage,omitempty"`
Watermark struct {
Enable bool `yaml:"enable,omitempty"`
Gravity string `yaml:"gravity,omitempty"`
X int `yaml:"x,omitempty"`
Y int `yaml:"y,omitempty"`
Rotate int `yaml:"rotate,omitempty"`
Opacity int `yaml:"opacity,omitempty"`
Text struct {
Content string `yaml:"content,omitempty"`
FontName string `yaml:"fontName,omitempty"`
FontSize int `yaml:"fontSize,omitempty"`
FontColor string `yaml:"fontColor,omitempty"`
StrokeColor string `yaml:"strokeColor,omitempty"`
StrokeWidth int `yaml:"strokeWidth,omitempty"`
} `yaml:"text,omitempty"`
Logo struct {
File string `yaml:"file,omitempty"`
W int `yaml:"w,omitempty"`
H int `yaml:"h,omitempty"`
} `yaml:"logo,omitempty"`
} `yaml:"watermark,omitempty"`
}
// NewKimgConfig create a config instance from config file.
func NewKimgConfig(configFile string) (*KimgConfig, error) {
var cfg KimgConfig
cfg.Httpd.Bind = "0.0.0.0:80"
cfg.Httpd.URL = "/"
cfg.Httpd.Headers = map[string]string{"Server": "kimg"}
cfg.Httpd.Etag = true
cfg.Httpd.MaxAge = 90 * 24 * 3600
cfg.Httpd.FormName = "file"
cfg.Httpd.MaxSize = 100 * 1024 * 1024
cfg.Httpd.EnableWeb = true
cfg.Image.Format = "jpeg"
cfg.Image.Quality = 75
cfg.Image.AllowedTypes = []string{"jpeg", "jpg", "png", "gif", "webp"}
cfg.Logger.Mode = "console"
cfg.Logger.Level = "debug"
cfg.Logger.File = "kimg.log"
cfg.Cache.Mode = "memory"
cfg.Cache.MaxSize = 1 * 1024 * 1024
cfg.Cache.Memcache.URL = "127.0.0.1:11211"
cfg.Cache.Redis.URL = "127.0.0.1:6379"
cfg.Cache.Memory.Capacity = 100 * 1024 * 1024
cfg.Storage.Mode = "file"
cfg.Storage.SaveNew = true
cfg.Storage.File.Root = "kimgs"
data, err := ioutil.ReadFile(configFile)
if err != nil {
log.Printf("[WARN] %s\n", err)
log.Println("[INFO] configuration [default] used")
} else {
log.Printf("[INFO] configuration [%s] used\n", configFile)
if err := yaml.Unmarshal(data, &cfg); err != nil {
log.Printf("[ERROR] %s\n", err)
return nil, err
}
}
// httpd env
if env, ok := os.LookupEnv("KIMG_HTTPD_BIND"); ok {
cfg.Httpd.Bind = env
}
if env, ok := os.LookupEnv("KIMG_HTTPD_URL"); ok {
cfg.Httpd.URL = env
}
if env, ok := os.LookupEnv("KIMG_HTTPD_HEADERS"); ok {
arr := strings.Split(env, ",")
for _, v := range arr {
s := strings.Split(v, ":")
if len(s) == 2 {
cfg.Httpd.Headers[s[0]] = s[1]
}
}
}
if env, ok := os.LookupEnv("KIMG_HTTPD_ETAG"); ok {
cfg.Httpd.Etag, _ = strconv.ParseBool(env)
}
if env, ok := os.LookupEnv("KIMG_HTTPD_MAX_AGE"); ok {
cfg.Httpd.MaxAge, _ = strconv.Atoi(env)
}
if env, ok := os.LookupEnv("KIMG_HTTPD_FORM_NAME"); ok {
cfg.Httpd.FormName = env
}
if env, ok := os.LookupEnv("KIMG_HTTPD_MAX_SIZE"); ok {
cfg.Httpd.MaxSize, _ = strconv.ParseInt(env, 0, 64)
}
if env, ok := os.LookupEnv("KIMG_HTTPD_ENABLE_WEB"); ok {
cfg.Httpd.EnableWeb, _ = strconv.ParseBool(env)
}
// image env
if env, ok := os.LookupEnv("KIMG_IMAGE_FORMAT"); ok {
cfg.Image.Format = env
}
if env, ok := os.LookupEnv("KIMG_IMAGE_QUALITY"); ok {
cfg.Image.Quality, _ = strconv.Atoi(env)
}
if env, ok := os.LookupEnv("KIMG_IMAGE_ALLOWED_TYPES"); ok {
cfg.Image.AllowedTypes = strings.Split(env, ",")
}
// logger env
if env, ok := os.LookupEnv("KIMG_LOGGER_MODE"); ok {
cfg.Logger.Mode = env
}
if env, ok := os.LookupEnv("KIMG_LOGGER_LEVEL"); ok {
cfg.Logger.Level = env
}
if env, ok := os.LookupEnv("KIMG_LOGGER_FILE"); ok {
cfg.Logger.File = env
}
// cache env
if env, ok := os.LookupEnv("KIMG_CACHE_MODE"); ok {
cfg.Cache.Mode = env
}
if env, ok := os.LookupEnv("KIMG_CACHE_MAX_SIZE"); ok {
cfg.Cache.MaxSize, _ = strconv.Atoi(env)
}
if env, ok := os.LookupEnv("KIMG_CACHE_MEMCACHE_URL"); ok {
cfg.Cache.Memcache.URL = env
}
if env, ok := os.LookupEnv("KIMG_CACHE_REDIS_URL"); ok {
cfg.Cache.Redis.URL = env
}
if env, ok := os.LookupEnv("KIMG_CACHE_MEMORY_CAPACITY"); ok {
cfg.Cache.Memory.Capacity, _ = strconv.ParseInt(env, 0, 64)
}
// storage env
if env, ok := os.LookupEnv("KIMG_STORAGE_MODE"); ok {
cfg.Storage.Mode = env
}
if env, ok := os.LookupEnv("KIMG_STORAGE_SAVE_NEW"); ok {
cfg.Storage.SaveNew, _ = strconv.ParseBool(env)
}
if env, ok := os.LookupEnv("KIMG_STORAGE_FILE_ROOT"); ok {
cfg.Storage.File.Root = env
}
if env, ok := os.LookupEnv("KIMG_STORAGE_MINIO_ENDPOINT"); ok {
cfg.Storage.Minio.Endpoint = env
}
if env, ok := os.LookupEnv("KIMG_STORAGE_MINIO_ACCESSKEYID"); ok {
cfg.Storage.Minio.AccessKeyID = env
}
if env, ok := os.LookupEnv("KIMG_STORAGE_MINIO_SECRETACCESSKEY"); ok {
cfg.Storage.Minio.SecretAccessKey = env
}
if env, ok := os.LookupEnv("KIMG_STORAGE_MINIO_BUCKET"); ok {
cfg.Storage.Minio.Bucket = env
}
if env, ok := os.LookupEnv("KIMG_STORAGE_MINIO_USESSL"); ok {
cfg.Storage.Minio.UseSSL, _ = strconv.ParseBool(env)
}
// watermark env
if env, ok := os.LookupEnv("KIMG_WATERMARK_ENABLE"); ok {
cfg.Watermark.Enable, _ = strconv.ParseBool(env)
}
if env, ok := os.LookupEnv("KIMG_WATERMARK_GRAVITY"); ok {
cfg.Watermark.Gravity = env
}
if env, ok := os.LookupEnv("KIMG_WATERMARK_X"); ok {
cfg.Watermark.X, _ = strconv.Atoi(env)
}
if env, ok := os.LookupEnv("KIMG_WATERMARK_Y"); ok {
cfg.Watermark.Y, _ = strconv.Atoi(env)
}
if env, ok := os.LookupEnv("KIMG_WATERMARK_ROTATE"); ok {
cfg.Watermark.Rotate, _ = strconv.Atoi(env)
}
if env, ok := os.LookupEnv("KIMG_WATERMARK_OPACITY"); ok {
cfg.Watermark.Opacity, _ = strconv.Atoi(env)
}
if env, ok := os.LookupEnv("KIMG_WATERMARK_TEXT_CONTENT"); ok {
cfg.Watermark.Text.Content = env
}
if env, ok := os.LookupEnv("KIMG_WATERMARK_TEXT_FONT_NAME"); ok {
cfg.Watermark.Text.FontName = env
}
if env, ok := os.LookupEnv("KIMG_WATERMARK_TEXT_FONT_SIZE"); ok {
cfg.Watermark.Text.FontSize, _ = strconv.Atoi(env)
}
if env, ok := os.LookupEnv("KIMG_WATERMARK_TEXT_FONT_COLOR"); ok {
cfg.Watermark.Text.FontColor = env
}
if env, ok := os.LookupEnv("KIMG_WATERMARK_TEXT_STROKE_COLOR"); ok {
cfg.Watermark.Text.StrokeColor = env
}
if env, ok := os.LookupEnv("KIMG_WATERMARK_TEXT_STROKE_WIDTH"); ok {
cfg.Watermark.Text.StrokeWidth, _ = strconv.Atoi(env)
}
if env, ok := os.LookupEnv("KIMG_WATERMARK_LOGO_FILE"); ok {
cfg.Watermark.Logo.File = env
}
if env, ok := os.LookupEnv("KIMG_WATERMARK_LOGO_W"); ok {
cfg.Watermark.Logo.W, _ = strconv.Atoi(env)
}
if env, ok := os.LookupEnv("KIMG_WATERMARK_LOGO_H"); ok {
cfg.Watermark.Logo.H, _ = strconv.Atoi(env)
}
return &cfg, nil
}