-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
134 lines (116 loc) · 3.57 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
package config
import (
"log"
"net"
"os"
"path"
"time"
"github.com/kelseyhightower/envconfig"
"github.com/go-imsto/imsto/storage/imagio"
)
// Section ...
type Section struct { // example: {demo,file,Demo,/var/lib/imsto/,demo.imsto.org}
Name string `json:"name,omitempty"`
Engine string `json:"engine,omitempty"`
Label string `json:"label,omitempty"`
Root string `json:"root,omitempty"`
Host string `json:"host,omitempty"` // stage host
}
// Sizes ...
type Sizes = imagio.Sizes
// IPNet ...
type IPNet struct{ net.IPNet }
// Decode ...
func (z *IPNet) Decode(value string) error {
_, ipn, err := net.ParseCIDR(value)
if err != nil {
return err
}
*z = IPNet{*ipn}
return nil
}
// Config ...
type Config struct {
DatabaseDSN string `envconfig:"META_DSN"`
SentryDSN string `envconfig:"SENTRY_DSN"`
MaxFileSize uint32 `envconfig:"MAX_FILESIZE" default:"2097152"` // 2MB
MaxWidth uint32 `envconfig:"MAX_WIDTH" default:"1600"`
MaxHeight uint32 `envconfig:"MAX_HEIGHT" default:"1600"`
MinWidth uint32 `envconfig:"MIN_WIDTH" default:"50"`
MinHeight uint32 `envconfig:"MIN_HEIGHT" default:"50"`
MaxQuality uint8 `envconfig:"MAX_QUALITY" default:"88"`
CacheRoot string `envconfig:"CACHE_ROOT" default:"/opt/imsto/cache/"`
LocalRoot string `envconfig:"LOCAL_ROOT" default:"/var/lib/imsto/"`
StageHost string `envconfig:"STAGE_HOST"` // stage.example.org
WatermarkFile string `envconfig:"WATERMARK_FILE"` // /opt/imsto/watermark.png
WatermarkOpacity uint8 `envconfig:"WATERMARK_OPACITY" default:"30"`
SupportSizes Sizes `envconfig:"SUPPORT_SIZE" default:"60,120,256"`
Roofs []string `envconfig:"ROOFS" default:"demo"` // roof1,roof2
Engines map[string]string `envconfig:"ENGINES"` // [roof]engine
Prefixes map[string]string `envconfig:"PREFIXES"` // [roof]prefix
WhiteList []IPNet `envconfig:"WHITELIST"`
ReadTimeout time.Duration `envconfig:"READ_TIMEOUT" default:"10s"`
TiringListen string `envconfig:"TIRING_LISTEN" default:":8967"`
StageListen string `envconfig:"STAGE_LISTEN" default:":8968"`
RPCListen string `envconfig:"RPC_LISTEN" default:":8969"`
}
// vars
var (
Version = "dev"
Name = "imsto"
// Current ...
Current = new(Config)
)
// InDevelop ...
func InDevelop() bool {
return "dev" == Version
}
func init() {
if err := envconfig.Process(Name, Current); err != nil {
log.Printf("envconfig init ERR %s", err)
}
if len(Current.LocalRoot) < 2 {
homeDir, err := os.UserHomeDir()
if err != nil {
log.Printf("userHomeDir err %s", err)
return
}
Current.LocalRoot = path.Join(homeDir, Name)
}
if len(Current.CacheRoot) < 2 {
cacheDir, err := os.UserCacheDir()
if err != nil {
log.Printf("userCacheDir err %s", err)
return
}
Current.CacheRoot = path.Join(cacheDir, Name)
}
}
// HasSection ...
func HasSection(roof string) bool {
if _, ok := Current.Engines[roof]; ok {
return true
}
return false
}
// GetEngine ...
func GetEngine(roof string) string {
if v, ok := Current.Engines[roof]; ok {
return v
}
return ""
}
// GetPrefix ...
func GetPrefix(roof string) string {
if s, ok := Current.Prefixes[roof]; ok && len(s) > 0 {
return s
}
return roof
}
// EnvOr ...
func EnvOr(key, dft string) string {
if v, ok := os.LookupEnv(key); ok {
return v
}
return dft
}