-
Notifications
You must be signed in to change notification settings - Fork 59
/
main.go
158 lines (128 loc) · 4.71 KB
/
main.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
Copyright (C) 2016-2017 dapperdox.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"log"
"net"
"net/http"
"os"
"sync"
"time"
"github.com/dapperdox/dapperdox/config"
"github.com/dapperdox/dapperdox/handlers/guides"
"github.com/dapperdox/dapperdox/handlers/home"
"github.com/dapperdox/dapperdox/handlers/reference"
"github.com/dapperdox/dapperdox/handlers/specs"
"github.com/dapperdox/dapperdox/handlers/static"
"github.com/dapperdox/dapperdox/handlers/timeout"
"github.com/dapperdox/dapperdox/logger"
"github.com/dapperdox/dapperdox/network"
"github.com/dapperdox/dapperdox/proxy"
"github.com/dapperdox/dapperdox/render"
"github.com/dapperdox/dapperdox/spec"
"github.com/gorilla/pat"
"github.com/justinas/alice"
"github.com/justinas/nosurf"
)
var VERSION string = "1.2.1"
var tlsEnabled bool
// ---------------------------------------------------------------------------
func main() {
tlsEnabled = false
log.Printf("DapperDox server version %s starting\n", VERSION)
os.Setenv("GOFIGURE_ENV_ARRAY", "1") // Enable gofigure array parsing of env vars
cfg, err := config.Get()
if err != nil {
log.Fatalf("error configuring app: %s", err)
}
// logging before this point must rely on setting LOGLEVEL env var
if l, err := logger.LevelFromString(cfg.LogLevel); err == nil {
logger.DefaultLevel = l
} else {
logger.Errorf(nil, "error setting log level: %s", err)
os.Exit(1)
}
router := pat.New()
chain := alice.New(logger.Handler /*, context.ClearHandler*/, timeoutHandler, withCsrf, injectHeaders).Then(router)
logger.Infof(nil, "listening on %s", cfg.BindAddr)
listener, err := net.Listen("tcp", cfg.BindAddr)
if err != nil {
logger.Errorf(nil, "%s", err)
os.Exit(1)
}
var wg sync.WaitGroup
var sg sync.WaitGroup
sg.Add(1)
go func() {
logger.Traceln(nil, "Listen for and serve swagger spec requests for start up")
wg.Add(1)
sg.Done()
http.Serve(listener, chain)
logger.Traceln(nil, "Finished service swagger specs for start up")
wg.Done()
}()
sg.Wait()
// Register the spec routes (Listener and server must be up and running by now)
specs.Register(router)
spec.LoadStatusCodes()
err = spec.LoadSpecifications(cfg.BindAddr, true)
if err != nil {
logger.Errorf(nil, "Load specification error: %s", err)
os.Exit(1)
}
render.Register()
reference.Register(router)
guides.Register(router)
static.Register(router) // TODO - Static content should be capable of being CDN hosted
home.Register(router)
proxy.Register(router)
listener.Close() // Stop serving specs
wg.Wait() // wait for go routine serving specs to terminate
listener, err = network.GetListener(&tlsEnabled)
if err != nil {
logger.Errorf(nil, "Error listening on %s: %s", cfg.BindAddr, err)
os.Exit(1)
}
http.Serve(listener, chain)
}
// ---------------------------------------------------------------------------
func withCsrf(h http.Handler) http.Handler {
csrfHandler := nosurf.New(h)
csrfHandler.SetFailureHandler(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
rsn := nosurf.Reason(req).Error()
logger.Warnf(req, "failed csrf validation: %s", rsn)
render.HTML(w, http.StatusBadRequest, "error", map[string]interface{}{"error": rsn})
}))
return csrfHandler
}
// ---------------------------------------------------------------------------
func timeoutHandler(h http.Handler) http.Handler {
return timeout.Handler(h, 1*time.Second, http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
logger.Warnln(req, "request timed out")
render.HTML(w, http.StatusRequestTimeout, "error", map[string]interface{}{"error": "Request timed out"})
}))
}
// ---------------------------------------------------------------------------
// Handle additional headers such as strict transport security for TLS, and
// giving the Server name.
func injectHeaders(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Server", "DapperDox "+VERSION)
if tlsEnabled {
w.Header().Add("Strict-Transport-Security", "max-age=63072000; includeSubDomains")
}
h.ServeHTTP(w, r)
})
}
// ---------------------------------------------------------------------------