-
Notifications
You must be signed in to change notification settings - Fork 325
/
main.go
157 lines (128 loc) · 4.26 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
package main
// vouch
// github.com/vouch/vouch-proxy
import (
"errors"
"flag"
"log"
"net"
"net/http"
"os"
"path/filepath"
"strconv"
"time"
"github.com/vouch/vouch-proxy/pkg/healthcheck"
"github.com/vouch/vouch-proxy/pkg/response"
"github.com/vouch/vouch-proxy/pkg/cookie"
"github.com/vouch/vouch-proxy/pkg/jwtmanager"
"github.com/vouch/vouch-proxy/pkg/domains"
"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/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")
)
// 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()
cfg.TestConfiguration()
logger = cfg.Logging.Logger
fastlog = cfg.Logging.FastLogger
domains.Configure()
jwtmanager.Configure()
cookie.Configure()
handlers.Configure()
timelog.Configure()
response.Configure()
}
func main() {
configure()
var listen = cfg.Cfg.Listen + ":" + strconv.Itoa(cfg.Cfg.Port)
checkTCPPortAvailable(listen)
logger.Infow("starting "+cfg.Branding.CcName,
// "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(authH))
muxR.HandleFunc("/_external-auth-{id}", timelog.TimeLog(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))))
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.CcName + " already running?)"))
}
if err = conn.Close(); err != nil {
logger.Error(err)
}
}