forked from vouch/vouch-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
108 lines (88 loc) · 2.91 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
package main
// vouch
// github.com/vouch/vouch-proxy
import (
"log"
"net/http"
"path/filepath"
"strconv"
"time"
"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"
tran "github.com/vouch/vouch-proxy/pkg/transciever"
)
// 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 = cfg.Cfg.Logger
fastlog = cfg.Cfg.FastLogger
)
// 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
}
func main() {
var listen = cfg.Cfg.Listen + ":" + strconv.Itoa(cfg.Cfg.Port)
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 logger.Desugar().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 cfg.Cfg.WebApp {
logger.Info("enabling websocket")
tran.ExplicitInit()
muxR.Handle("/ws", tran.WS)
}
// socketio := tran.NewServer()
// muxR.Handle("/socket.io/", cors.AllowAll(socketio))
// http.Handle("/socket.io/", tran.Server)
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),
}
log.Fatal(srv.ListenAndServe())
}