-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
171 lines (151 loc) · 4.68 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
package main
import (
"time"
xconfig "github.com/alexferl/golib/config"
xhttp "github.com/alexferl/golib/http/config"
xlog "github.com/alexferl/golib/log"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
// Config holds all configuration for our program
type Config struct {
Config *xconfig.Config
Http *xhttp.Config
Logging *xlog.Config
FileDownloadTimeout time.Duration
FileUploadTimeout time.Duration
MaxFileSize int64
TmpPath string
Vips *Vips
Storage *Storage
}
// Vips holds vips specific configuration
type Vips struct {
ConcurrencyLevel int
MaxCacheMem int
MaxCacheSize int
MaxCacheFiles int
}
type Storage struct {
Type string
Filesystem *Filesystem
GCloud *GCloud
Linode *Linode
S3 *S3
}
type Filesystem struct {
Path string
}
type GCloud struct {
ProjectId string
Bucket string
StorageClass string
Location string
}
type Linode struct {
Bucket string
Region string
}
type S3 struct {
Bucket string
StorageClass string
Region string
}
// NewConfig creates a Config instance
func NewConfig() *Config {
return &Config{
Config: xconfig.New("air"),
Http: xhttp.DefaultConfig,
Logging: xlog.DefaultConfig,
FileUploadTimeout: time.Second * 60,
FileDownloadTimeout: time.Second * 60,
MaxFileSize: 10,
TmpPath: "",
Vips: &Vips{
ConcurrencyLevel: 1,
MaxCacheMem: 100 * 1024 * 1024, // 100MB
MaxCacheSize: 500,
MaxCacheFiles: 100,
},
Storage: &Storage{
Type: "filesystem",
Filesystem: &Filesystem{
Path: "/tmp",
},
GCloud: &GCloud{
ProjectId: "",
Bucket: "",
Location: "US",
StorageClass: "STANDARD",
},
Linode: &Linode{
Bucket: "",
Region: "",
},
S3: &S3{
Bucket: "",
Region: "",
StorageClass: "STANDARD",
},
},
}
}
// addFlags adds all the flags from the command line
func (c *Config) addFlags(fs *pflag.FlagSet) {
fs.DurationVar(&c.FileDownloadTimeout, "file-download-timeout", c.FileDownloadTimeout,
"File download timeout")
fs.DurationVar(&c.FileUploadTimeout, "file-upload-timeout", c.FileUploadTimeout, "File upload timeout")
fs.Int64Var(&c.MaxFileSize, "max-file-size", c.MaxFileSize, "Max file size for uploads in megabytes")
fs.StringVar(&c.TmpPath, "temp-path", c.TmpPath,
"Temporary files path")
// Vips
fs.IntVar(&c.Vips.ConcurrencyLevel, "vips-concurrency-level", c.Vips.ConcurrencyLevel,
"vips concurrency level")
fs.IntVar(&c.Vips.MaxCacheMem, "vips-max-cache-mem", c.Vips.MaxCacheMem,
"Maximum amount of memory in bytes vips is allowed to use for caching")
fs.IntVar(&c.Vips.MaxCacheSize, "vips-max-cache-size", c.Vips.MaxCacheSize,
"Maximum amount of objects vips is allowed to keep in cache")
fs.IntVar(&c.Vips.MaxCacheFiles, "vips-max-cache-files", c.Vips.MaxCacheFiles,
"Maximum amount of files vips is allowed to keep in cache")
// Storage
fs.StringVar(&c.Storage.Type, "storage-type", c.Storage.Type,
"Storage type to use for assets (filesystem, gcloud, linode, s3)")
// Filesystem
fs.StringVar(&c.Storage.Filesystem.Path, "filesystem-path", c.Storage.Filesystem.Path,
"Filesystem storage path")
// GCloud
fs.StringVar(&c.Storage.GCloud.ProjectId, "gcloud-project-id", c.Storage.GCloud.ProjectId,
"Google Cloud project id")
fs.StringVar(&c.Storage.GCloud.Bucket, "gcloud-bucket", c.Storage.GCloud.Bucket,
"Google Cloud storage bucket")
fs.StringVar(&c.Storage.GCloud.StorageClass, "gcloud-storage-class", c.Storage.GCloud.StorageClass,
"Google Cloud bucket storage class")
fs.StringVar(&c.Storage.GCloud.Location, "gcloud-location", c.Storage.GCloud.Location,
"Google Cloud bucket location")
// Linode
fs.StringVar(&c.Storage.Linode.Bucket, "linode-bucket", c.Storage.Linode.Bucket,
"Linode storage bucket")
fs.StringVar(&c.Storage.Linode.Region, "linode-region", c.Storage.Linode.Region, "Linode region")
// S3
fs.StringVar(&c.Storage.S3.Bucket, "s3-bucket", c.Storage.S3.Bucket, "AWS S3 storage bucket")
fs.StringVar(&c.Storage.S3.Region, "s3-region", c.Storage.S3.Region, "AWS S3 region")
fs.StringVar(&c.Storage.S3.StorageClass, "s3-storage-class", c.Storage.S3.StorageClass,
"AWS S3 storage class")
}
func (c *Config) BindFlags() {
c.addFlags(pflag.CommandLine)
c.Logging.BindFlags(pflag.CommandLine)
c.Http.BindFlags(pflag.CommandLine)
err := c.Config.BindFlags()
if err != nil {
panic(err)
}
err = xlog.New(&xlog.Config{
LogLevel: viper.GetString("log-level"),
LogOutput: viper.GetString("log-output"),
LogWriter: viper.GetString("log-writer"),
})
if err != nil {
panic(err)
}
}