forked from vouch/vouch-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
198 lines (162 loc) · 5.88 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
Copyright 2020 The Vouch Proxy Authors.
Use of this source code is governed by The MIT License (MIT) that
can be found in the LICENSE file. Software distributed under The
MIT License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
OR CONDITIONS OF ANY KIND, either express or implied.
*/
package main
// Vouch Proxy
// github.com/vouch/vouch-proxy
/*
Hello Developer! Thanks for looking at the code!
Before submitting PRs, please see the README...
https://github.com/vouch/vouch-proxy#submitting-a-pull-request-for-a-new-feature
*/
import (
"errors"
"flag"
"log"
"net"
"net/http"
"os"
"path/filepath"
"strconv"
"time"
// "net/http/pprof"
"github.com/gorilla/mux"
"go.uber.org/zap"
"github.com/vouch/vouch-proxy/handlers"
"github.com/vouch/vouch-proxy/pkg/cfg"
"github.com/vouch/vouch-proxy/pkg/cookie"
"github.com/vouch/vouch-proxy/pkg/domains"
"github.com/vouch/vouch-proxy/pkg/healthcheck"
"github.com/vouch/vouch-proxy/pkg/jwtmanager"
"github.com/vouch/vouch-proxy/pkg/responses"
"github.com/vouch/vouch-proxy/pkg/timelog"
)
// version and semver get overwritten by build with
// go build -i -v -ldflags="-X main.version=$(git describe --always --long) -X main.semver=v$(git semver get)"
var (
version = "undefined"
builddt = "undefined"
host = "undefined"
semver = "undefined"
branch = "undefined"
staticDir = "/static/"
logger *zap.SugaredLogger
fastlog *zap.Logger
help = flag.Bool("help", false, "show usage")
// doProfile = flag.Bool("profile", false, "run profiler at /debug/pprof")
)
// fwdToZapWriter allows us to use the zap.Logger as our http.Server ErrorLog
// see https://stackoverflow.com/questions/52294334/net-http-set-custom-logger
type fwdToZapWriter struct {
logger *zap.Logger
}
func (fw *fwdToZapWriter) Write(p []byte) (n int, err error) {
fw.logger.Error(string(p))
return len(p), nil
}
// configure() is essentially init()
// for most other projects you would think of this as init()
// this epic issue related to the flag.parse change of behavior for go 1.13 explains some of what's going on here
// https://github.com/golang/go/issues/31859
// essentially, flag.parse() must be called in vouch-proxy's main() and *not* in init()
// this has a cascading effect on the zap logger since the log level can be set on the command line
// configure() explicitly calls package configure functions (domains.Configure() etc) mostly to set the logger
// without this setup testing and logging are screwed up
func configure() {
flag.Parse()
if *help {
flag.PrintDefaults()
os.Exit(1)
}
cfg.Configure()
healthcheck.CheckAndExitIfIsHealthCheck()
logger = cfg.Logging.Logger
fastlog = cfg.Logging.FastLogger
if err := cfg.ValidateConfiguration(); err != nil {
logger.Fatal(err)
}
domains.Configure()
jwtmanager.Configure()
cookie.Configure()
responses.Configure()
handlers.Configure()
timelog.Configure()
}
func main() {
configure()
var listen = cfg.Cfg.Listen + ":" + strconv.Itoa(cfg.Cfg.Port)
checkTCPPortAvailable(listen)
logger.Infow("starting "+cfg.Branding.FullName,
// "semver": semver,
"version", version,
"buildtime", builddt,
"buildhost", host,
"branch", branch,
"semver", semver,
"listen", listen,
"oauth.provider", cfg.GenOAuth.Provider)
muxR := mux.NewRouter()
authH := http.HandlerFunc(handlers.ValidateRequestHandler)
muxR.HandleFunc("/validate", timelog.TimeLog(jwtmanager.JWTCacheHandler(authH)))
muxR.HandleFunc("/_external-auth-{id}", timelog.TimeLog(jwtmanager.JWTCacheHandler(authH)))
loginH := http.HandlerFunc(handlers.LoginHandler)
muxR.HandleFunc("/login", timelog.TimeLog(loginH))
logoutH := http.HandlerFunc(handlers.LogoutHandler)
muxR.HandleFunc("/logout", timelog.TimeLog(logoutH))
callH := http.HandlerFunc(handlers.CallbackHandler)
muxR.HandleFunc("/auth", timelog.TimeLog(callH))
healthH := http.HandlerFunc(handlers.HealthcheckHandler)
muxR.HandleFunc("/healthcheck", timelog.TimeLog(healthH))
// setup static
sPath, err := filepath.Abs(cfg.RootDir + staticDir)
if fastlog.Core().Enabled(zap.DebugLevel) {
if err != nil {
logger.Errorf("couldn't find static assets at %s", sPath)
}
logger.Debugf("serving static files from %s", sPath)
}
// https://golangcode.com/serve-static-assets-using-the-mux-router/
muxR.PathPrefix(staticDir).Handler(http.StripPrefix(staticDir, http.FileServer(http.Dir(sPath))))
//
// if *doProfile {
// addProfilingHandlers(muxR)
// }
srv := &http.Server{
Handler: muxR,
Addr: listen,
// Good practice: enforce timeouts for servers you create!
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
ErrorLog: log.New(&fwdToZapWriter{fastlog}, "", 0),
}
logger.Fatal(srv.ListenAndServe())
}
func checkTCPPortAvailable(listen string) {
logger.Debug("checking availability of tcp port: " + listen)
conn, err := net.Listen("tcp", listen)
if err != nil {
logger.Error(err)
logger.Fatal(errors.New(listen + " is not available (is " + cfg.Branding.FullName + " already running?)"))
}
if err = conn.Close(); err != nil {
logger.Error(err)
}
}
// if you'd like to enable profiling uncomment these
// func addProfilingHandlers(muxR *mux.Router) {
// // https://stackoverflow.com/questions/47452471/pprof-profile-with-julienschmidtrouter-and-benchmarks-not-profiling-handler
// logger.Debugf("profiling routes added at http://%s:%d/debug/pprof/", cfg.Cfg.Listen, cfg.Cfg.Port)
// muxR.HandleFunc("/debug/pprof/", pprof.Index)
// muxR.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
// muxR.HandleFunc("/debug/pprof/profile", pprof.Profile)
// muxR.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
// muxR.HandleFunc("/debug/pprof/trace", pprof.Trace)
// muxR.Handle("/debug/pprof/goroutine", pprof.Handler("goroutine"))
// muxR.Handle("/debug/pprof/heap", pprof.Handler("heap"))
// muxR.Handle("/debug/pprof/threadcreate", pprof.Handler("threadcreate"))
// muxR.Handle("/debug/pprof/block", pprof.Handler("block"))
// }