-
Notifications
You must be signed in to change notification settings - Fork 163
/
config.go
82 lines (68 loc) · 1.87 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
package main
import (
"fmt"
"os"
"reflect"
"strings"
)
// Configuration holds application configuration. Values will be pulled from
// environment variables, prefixed by keyPrefix. Default values can be added
// via tags.
type Configuration struct {
Listen string `config:"tcp://:8080"`
Host string `config:"localhost:8080"`
ExtOrigin string `config:""` // consider lfs-test-server may behind a reverse proxy
MetaDB string `config:"lfs.db"`
ContentPath string `config:"lfs-content"`
AdminUser string `config:""`
AdminPass string `config:""`
Cert string `config:""`
Key string `config:""`
Scheme string `config:"http"`
Public string `config:"public"`
UseTus string `config:"false"`
TusHost string `config:"localhost:1080"`
}
func (c *Configuration) IsHTTPS() bool {
return strings.Contains(Config.Scheme, "https")
}
func (c *Configuration) IsPublic() bool {
switch Config.Public {
case "1", "true", "TRUE":
return true
}
return false
}
func (c *Configuration) IsUsingTus() bool {
switch Config.UseTus {
case "1", "true", "TRUE":
return true
}
return false
}
// Config is the global app configuration
var Config = &Configuration{}
const keyPrefix = "LFS"
func init() {
te := reflect.TypeOf(Config).Elem()
ve := reflect.ValueOf(Config).Elem()
for i := 0; i < te.NumField(); i++ {
sf := te.Field(i)
name := sf.Name
field := ve.FieldByName(name)
envVar := strings.ToUpper(fmt.Sprintf("%s_%s", keyPrefix, name))
env := os.Getenv(envVar)
tag := sf.Tag.Get("config")
if env == "" && tag != "" {
env = tag
}
field.SetString(env)
}
if port := os.Getenv("PORT"); port != "" {
// If $PORT is set, override LFS_LISTEN. This is useful for deploying to Heroku.
Config.Listen = "tcp://:" + port
}
if Config.ExtOrigin == "" {
Config.ExtOrigin = fmt.Sprintf("%s://%s", Config.Scheme, Config.Host)
}
}