-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
63 lines (51 loc) · 1.54 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
package fileserver
import (
"github.com/roadrunner-server/errors"
)
type Config struct {
// Address to serve
Address string `mapstructure:"address"`
// CalculateEtag can be true/false and used to calculate etag for the static
CalculateEtag bool `mapstructure:"calculate_etag"`
// Weak etag `W/`
Weak bool `mapstructure:"weak"`
// per-root configuration
Configuration []*Cfg `mapstructure:"serve"`
// StreamRequestBody ...
StreamRequestBody bool `mapstructure:"stream_request_body"`
}
type Cfg struct {
// Prefix HTTP
Prefix string `mapstructure:"prefix"`
// Dir contains name of directory to control access to.
// Default - "."
Root string `mapstructure:"root"`
BytesRange bool `mapstructure:"bytes_range"`
Compress bool `mapstructure:"compress"`
CacheDuration int `mapstructure:"cache_duration"`
MaxAge int `mapstructure:"max_age"`
}
func (c *Config) Valid() error {
const op = errors.Op("static_validation")
if c.Address == "" {
return errors.E(op, errors.Str("empty address"))
}
if c.Configuration == nil {
return errors.E(op, errors.Str("no configuration to serve"))
}
for i := 0; i < len(c.Configuration); i++ {
if c.Configuration[i].Prefix == "" {
return errors.E(op, errors.Str("empty prefix"))
}
if c.Configuration[i].Prefix[0] != '/' {
return errors.E(op, errors.Str("prefix must begin with a forward slash"))
}
if c.Configuration[i].Root == "" {
c.Configuration[i].Root = "."
}
if c.Configuration[i].CacheDuration == 0 {
c.Configuration[i].CacheDuration = 10
}
}
return nil
}