-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
67 lines (53 loc) · 1.5 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
package config
import (
"fmt"
"io/ioutil"
"log"
"os"
"time"
"gopkg.in/yaml.v2"
)
// Config holds the service configuration
// necessary for endpoints to respond to requests
type Config struct {
Version string // server git hash
DeployTime time.Time
Env string `yaml:"env"`
Port string `yaml:"port"`
Debug bool `yaml:"debug"`
MongoDB struct {
Name string `yaml:"name"`
ConnectionString string `yaml:"connectionString"`
UsersCollectionName string `yaml:"usersCollectionName"`
ProjectsCollectionName string `yaml:"projectsCollectionName"`
PrivKeysCollectionName string `yaml:"privKeysCollectionName"`
PubKeysCollectionName string `yaml:"pubKeysCollectionName"`
} `yaml:"mongodb"`
Auth struct {
SigningKey string `yaml:"signingKey"`
} `yaml:"auth"`
}
// BuildConfig returns a populated config struct from a yaml file
func BuildConfig(filePath, version string) *Config {
config := configFromYaml(filePath)
// When running on Google App Engine, the PORT env
// variable is set by the runtime. If set, we will
// serve on the port specified there.
if port := os.Getenv("PORT"); port != "" {
config.Port = fmt.Sprintf(":%s", port)
}
config.DeployTime = time.Now()
config.Version = version
return config
}
func configFromYaml(filePath string) *Config {
config := &Config{}
yamlFile, err := ioutil.ReadFile(filePath)
if err != nil {
log.Fatal(err)
}
if err = yaml.Unmarshal(yamlFile, config); err != nil {
log.Fatal(err)
}
return config
}