forked from imgproxy/imgproxy
-
Notifications
You must be signed in to change notification settings - Fork 5
/
config.go
157 lines (124 loc) · 3.36 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
package main
import (
"flag"
"fmt"
"github.com/peterbourgon/diskv"
"log"
"net/url"
"os"
"strconv"
"strings"
)
func intEnvConfig(i *int, name string) {
if env, err := strconv.Atoi(os.Getenv(name)); err == nil {
*i = env
}
}
func megaIntEnvConfig(f *int, name string) {
if env, err := strconv.ParseFloat(os.Getenv(name), 64); err == nil {
*f = int(env * 1000000)
}
}
func urlEnvConfig(u **url.URL, name string) {
if env, present := os.LookupEnv(name); present {
if url, err := url.Parse(env); err == nil {
*u = url
}
}
}
func strEnvConfig(s *string, name string) {
if env := os.Getenv(name); len(env) > 0 {
*s = env
}
}
func strSliceEnvConfig(s *[]string, name string) {
if env := os.Getenv(name); len(env) > 0 {
*s = strings.Split(env, ",")
} else {
*s = make([]string, 0)
}
}
func boolEnvConfig(b *bool, name string) {
*b = false
if env, err := strconv.ParseBool(os.Getenv(name)); err == nil {
*b = env
}
}
type config struct {
Bind string
ReadTimeout int
WaitTimeout int
WriteTimeout int
DownloadTimeout int
TTL int
MaxDimension int
MaxResolution int
GZipCompression int
AllowOrigins []string
CacheRoot string
CacheSize int
ServerURL *url.URL
}
var conf = config{
Bind: ":8080",
ReadTimeout: 10,
WriteTimeout: 10,
DownloadTimeout: 5,
TTL: 3600,
MaxDimension: 2048,
GZipCompression: 5,
}
var farsparkCache *diskv.Diskv
func initCache() {
if conf.CacheSize > 0 {
farsparkCache = diskv.New(diskv.Options{
BasePath: conf.CacheRoot,
Transform: func(s string) []string { return []string{} },
CacheSizeMax: uint64(conf.CacheSize),
})
} else {
farsparkCache = nil
}
}
func init() {
flag.Parse()
if port := os.Getenv("PORT"); len(port) > 0 {
conf.Bind = fmt.Sprintf(":%s", port)
}
strEnvConfig(&conf.Bind, "FARSPARK_BIND")
intEnvConfig(&conf.ReadTimeout, "FARSPARK_READ_TIMEOUT")
intEnvConfig(&conf.WriteTimeout, "FARSPARK_WRITE_TIMEOUT")
intEnvConfig(&conf.DownloadTimeout, "FARSPARK_DOWNLOAD_TIMEOUT")
intEnvConfig(&conf.TTL, "FARSPARK_TTL")
intEnvConfig(&conf.MaxDimension, "FARSPARK_MAX_DIMENSION")
intEnvConfig(&conf.GZipCompression, "FARSPARK_GZIP_COMPRESSION")
strSliceEnvConfig(&conf.AllowOrigins, "FARSPARK_ALLOW_ORIGINS")
strEnvConfig(&conf.CacheRoot, "FARSPARK_CACHE_ROOT")
intEnvConfig(&conf.CacheSize, "FARSPARK_CACHE_SIZE")
urlEnvConfig(&conf.ServerURL, "FARSPARK_SERVER_URL")
if len(conf.Bind) == 0 {
log.Fatalln("Bind address is not defined")
}
if conf.ReadTimeout <= 0 {
log.Fatalf("Read timeout should be greater than 0, now - %d\n", conf.ReadTimeout)
}
if conf.WriteTimeout <= 0 {
log.Fatalf("Write timeout should be greater than 0, now - %d\n", conf.WriteTimeout)
}
if conf.DownloadTimeout <= 0 {
log.Fatalf("Download timeout should be greater than 0, now - %d\n", conf.DownloadTimeout)
}
if conf.TTL <= 0 {
log.Fatalf("TTL should be greater than 0, now - %d\n", conf.TTL)
}
if conf.MaxDimension <= 0 {
log.Fatalf("Max dimension should be greater than 0, now - %d\n", conf.MaxDimension)
}
if conf.GZipCompression < 0 {
log.Fatalf("GZip compression should be greater than or quual to 0, now - %d\n", conf.GZipCompression)
} else if conf.GZipCompression > 9 {
log.Fatalf("GZip compression can't be greater than 9, now - %d\n", conf.GZipCompression)
}
initDownloading()
initCache()
}