This repository was archived by the owner on May 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathserver.go
114 lines (96 loc) · 2.87 KB
/
server.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
package main
import (
"fmt"
"net/http"
"os"
"strings"
"github.com/cloudfoundry-community/go-cfenv"
"github.com/gorilla/context"
"github.com/gorilla/csrf"
"github.com/govau/cf-common/env"
"github.com/yvasiyarov/gorelic"
"github.com/18F/cg-dashboard/controllers"
"github.com/18F/cg-dashboard/controllers/pprof"
"github.com/18F/cg-dashboard/helpers"
)
const (
defaultPort = "9999"
defaultUPSName = "dashboard-ups"
envUPSNames = "UPS_NAMES"
upsNamesEnvDelimiter = ":"
)
func main() {
// Start the server up.
var port string
if port = os.Getenv("PORT"); len(port) == 0 {
port = defaultPort
}
fmt.Println("using port: " + port)
// Try to load the user-provided-service
// for backup of certain environment variables.
cfEnv, err := cfenv.Current()
if err != nil || cfEnv == nil {
fmt.Println("Warning: No Cloud Foundry Environment found")
}
startApp(port, cfEnv)
}
func startMonitoring(license string) {
agent := gorelic.NewAgent()
agent.Verbose = true
agent.CollectHTTPStat = true
agent.NewrelicLicense = license
agent.NewrelicName = "Cloudgov Deck"
if err := agent.Run(); err != nil {
fmt.Println(err.Error())
}
}
func startApp(port string, app *cfenv.App) {
var envVars *env.VarSet
if upsNames := os.Getenv(envUPSNames); upsNames != "" && app != nil {
envVars = makeUPSEnvVarSet(app, upsNames)
} else {
envVars = makeDefaultEnvVarSet(app)
}
router, settings, err := controllers.InitApp(envVars)
if err != nil {
fmt.Println(err.Error())
// Terminate the program with a non-zero value number.
// Need this for testing purposes.
os.Exit(1)
}
if settings.PProfEnabled {
pprof.InitPProfRouter(router)
}
nrLicense := envVars.String(helpers.NewRelicLicenseEnvVar, "")
if nrLicense != "" {
fmt.Println("starting monitoring...")
startMonitoring(nrLicense)
}
fmt.Println("starting app now...")
// TODO add better timeout message. By default it will just say "Timeout"
protect := csrf.Protect(settings.CSRFKey, csrf.Secure(settings.SecureCookies))
http.ListenAndServe(":"+port, protect(
http.TimeoutHandler(context.ClearHandler(router), helpers.TimeoutConstant, ""),
))
}
// makeDefaultEnvVarSet makes an env var set using the hard-coded UPS named
// defaultUPSName followed by the OS.
func makeDefaultEnvVarSet(app *cfenv.App) *env.VarSet {
opts := []env.VarSetOpt{}
if app != nil {
opts = append(opts, env.WithUPSLookup(app, defaultUPSName))
}
opts = append(opts, env.WithOSLookup())
return env.NewVarSet(opts...)
}
// makeUPSEnvVarSet makes an env var set from UPS names in the delimited
// environment variable upsNames.
func makeUPSEnvVarSet(app *cfenv.App, upsNames string) *env.VarSet {
opts := []env.VarSetOpt{env.WithOSLookup()}
for _, name := range strings.Split(upsNames, upsNamesEnvDelimiter) {
if name = strings.TrimSpace(name); name != "" {
opts = append(opts, env.WithUPSLookup(app, name))
}
}
return env.NewVarSet(opts...)
}