forked from git-lfs/lfs-test-server
/
config.go
51 lines (41 loc) · 1.13 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
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"`
MetaDB string `config:"lfs.db"`
ContentPath string `config:"lfs-content"`
AdminUser string `config:""`
AdminPass string `config:""`
}
// 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
}
}