-
Notifications
You must be signed in to change notification settings - Fork 3
/
context.go
133 lines (104 loc) · 2.74 KB
/
context.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
package main
import (
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/kelseyhightower/envconfig"
)
// Context provides shared state among route handlers.
type Context struct {
Settings
Storage Storage
}
// Settings contains configuration options loaded from the environment.
type Settings struct {
InternalPort int
ExternalPort int
LogLevel string
LogColors bool
MongoURL string
InternalCACert string
InternalCert string
InternalKey string
ExternalCert string
ExternalKey string
}
// Load reads configuration settings from the environment and validates them.
func (c *Context) Load() error {
if err := envconfig.Process("AUTH", &c.Settings); err != nil {
return err
}
if c.InternalPort == 0 {
c.InternalPort = 9001
}
if c.ExternalPort == 0 {
c.ExternalPort = 9000
}
if c.LogLevel == "" {
c.LogLevel = "info"
}
if c.MongoURL == "" {
c.MongoURL = "mongo"
}
if c.InternalCACert == "" {
c.InternalCACert = "/certificates/ca.pem"
}
if c.InternalCert == "" {
c.InternalCert = "/certificates/auth-store-cert.pem"
}
if c.InternalKey == "" {
c.InternalKey = "/certificates/auth-store-key.pem"
}
if c.ExternalCert == "" {
c.ExternalCert = "/certificates/external-cert.pem"
}
if c.ExternalKey == "" {
c.ExternalKey = "/certificates/external-key.pem"
}
if _, err := log.ParseLevel(c.LogLevel); err != nil {
return err
}
return nil
}
// NewContext loads configuration from the environment and applies immediate, global settings.
func NewContext() (*Context, error) {
c := &Context{}
if err := c.Load(); err != nil {
return c, err
}
// Configure the logging level and formatter.
level, err := log.ParseLevel(c.LogLevel)
if err != nil {
return c, err
}
log.SetLevel(level)
log.SetFormatter(&log.TextFormatter{
ForceColors: c.LogColors,
})
// Summarize the loaded settings.
log.WithFields(log.Fields{
"internal port": c.InternalPort,
"external port": c.ExternalPort,
"logging level": c.LogLevel,
"log with color": c.LogColors,
"mongo URL": c.MongoURL,
"internal CA cert": c.InternalCACert,
"internal cert": c.InternalCert,
"internal key": c.InternalKey,
"external cert": c.ExternalCert,
"external key": c.ExternalKey,
}).Info("Initializing with loaded settings.")
// Connect to MongoDB
c.Storage, err = NewMongoStorage(c)
if err != nil {
return c, err
}
return c, nil
}
// InternalListenAddr generates an address to bind the private net/http server to.
func (c *Context) InternalListenAddr() string {
return fmt.Sprintf(":%d", c.InternalPort)
}
// ExternalListenAddr generates an address to bind the public net/http server to.
func (c *Context) ExternalListenAddr() string {
return fmt.Sprintf(":%d", c.ExternalPort)
}