Skip to content

Commit 68f7825

Browse files
committed
mount information to index
1 parent 5233a6f commit 68f7825

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+6483
-272
lines changed

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ go 1.23.0
55
require (
66
github.com/bwmarrin/discordgo v0.28.1
77
github.com/distrobyte/multielo v0.0.5
8+
github.com/go-chi/chi/v5 v5.2.1
89
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
9-
github.com/justinas/alice v1.2.0
10+
github.com/google/uuid v1.6.0
1011
github.com/rs/zerolog v1.33.0
1112
gopkg.in/yaml.v3 v3.0.1
1213
layeh.com/gumble v0.0.0-20221205141517-d1df60a3cc14

go.sum

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
1818
github.com/dchote/go-openal v0.0.0-20171116030048-f4a9a141d372/go.mod h1:74z+CYu2/mx4N+mcIS/rsvfAxBPBV9uv8zRAnwyFkdI=
1919
github.com/distrobyte/multielo v0.0.5 h1:faENzmSoUb8gWaxGxnG6tzfvjeCkXviJK1spgB7pW/Y=
2020
github.com/distrobyte/multielo v0.0.5/go.mod h1:svv+AlrisaP7pUZDaOAhEfUp16LlrJIelr7CueKrr8M=
21+
github.com/go-chi/chi/v5 v5.2.1 h1:KOIHODQj58PmL80G2Eak4WdvUzjSJSm0vG72crDCqb8=
22+
github.com/go-chi/chi/v5 v5.2.1/go.mod h1:L2yAIGWB3H+phAw1NxKwWM+7eUH/lU8pOMm5hHcoops=
2123
github.com/go-fonts/dejavu v0.3.4 h1:Qqyx9IOs5CQFxyWTdvddeWzrX0VNwUAvbmAzL0fpjbc=
2224
github.com/go-fonts/dejavu v0.3.4/go.mod h1:D1z0DglIz+lmpeNYMYlxW4r22IhcdOYnt+R3PShU/Kg=
2325
github.com/go-fonts/latin-modern v0.3.3 h1:g2xNgI8yzdNzIVm+qvbMryB6yGPe0pSMss8QT3QwlJ0=
@@ -38,13 +40,13 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
3840
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
3941
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
4042
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
43+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
44+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
4145
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
4246
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
4347
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
4448
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
4549
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
46-
github.com/justinas/alice v1.2.0 h1:+MHSA/vccVCF4Uq37S42jwlkvI2Xzl7zTPCN5BnZNVo=
47-
github.com/justinas/alice v1.2.0/go.mod h1:fN5HRH/reO/zrUflLfTN43t3vXvKzvZIENsNEe7i7qA=
4850
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
4951
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
5052
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=

http/http.go

Lines changed: 61 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,67 @@ import (
66
"time"
77

88
"github.com/distrobyte/gerry/internal/config"
9-
"github.com/justinas/alice"
9+
"github.com/go-chi/chi/v5"
10+
"github.com/go-chi/chi/v5/middleware"
11+
"github.com/google/uuid"
12+
"github.com/rs/zerolog"
1013
"github.com/rs/zerolog/hlog"
1114
"github.com/rs/zerolog/log"
1215
)
1316

1417
func initHTTPServer() {
15-
c := alice.New()
16-
17-
c = c.Append(hlog.NewHandler(log.Logger.With().Str("component", "http").Logger()))
18-
19-
c = c.Append(hlog.AccessHandler(func(r *http.Request, status, size int, duration time.Duration) {
20-
hlog.FromRequest(r).Info().
21-
Str("method", r.Method).
22-
Stringer("url", r.URL).
23-
Str("Cf-Connecting-Ip", r.Header.Get("Cf-Connecting-Ip")).
24-
Int("status", status).
25-
Int("size", size).
26-
Dur("duration", duration).
27-
Msg("")
28-
}))
29-
c = c.Append(hlog.RemoteAddrHandler("ip"))
30-
c = c.Append(hlog.UserAgentHandler("user-agent"))
31-
c = c.Append(hlog.RequestIDHandler("req_id", "Request-Id"))
32-
c = c.Append(hlog.RefererHandler("referer"))
33-
34-
// create a new mux
35-
mux := http.NewServeMux()
36-
37-
mux.HandleFunc("GET /health", healthHandler)
38-
mux.HandleFunc("GET /karting", kartingHTMLHandler)
39-
mux.HandleFunc("GET /karting.svg", kartingSVGHandler)
40-
mux.HandleFunc("GET /karting.png", kartingPNGHandler)
41-
mux.HandleFunc("GET /karting.json", kartingJSONHandler)
42-
mux.HandleFunc("GET /*", notFoundHandler)
43-
mux.HandleFunc("/*", methodNotAllowedHandler)
44-
45-
http.Handle("/", c.Then(mux))
46-
47-
log.Info().Msg("http server initialized. listening on port " + fmt.Sprintf("%d", config.GetHTTPPort()))
18+
19+
r := chi.NewRouter()
20+
r.Use(requestIDMiddleware)
21+
r.Use(zerologMiddleware)
22+
23+
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
24+
// use the kartingHTML handler to serve the main page
25+
kartingHTMLHandler(w, r)
26+
})
27+
28+
r.Get("/health", healthHandler)
29+
r.Get("/karting", kartingHTMLHandler)
30+
r.Get("/karting.svg", kartingSVGHandler)
31+
r.Get("/karting.png", kartingPNGHandler)
32+
r.Get("/karting.json", kartingJSONHandler)
33+
r.NotFound(notFoundHandler)
34+
r.MethodNotAllowed(methodNotAllowedHandler)
35+
36+
log.Info().Msgf("Starting server on port %d", config.GetHTTPPort())
37+
log.Fatal().Err(http.ListenAndServe(fmt.Sprintf(":%d", config.GetHTTPPort()), r)).Msg("")
38+
log.Info().Msgf("Server started on port %d", config.GetHTTPPort())
39+
}
40+
41+
func requestIDMiddleware(next http.Handler) http.Handler {
42+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
43+
requestID := uuid.New().String()
44+
logger := log.With().Str("request_id", requestID).Logger()
45+
r = r.WithContext(logger.WithContext(r.Context()))
46+
w.Header().Set("X-Request-ID", requestID)
47+
next.ServeHTTP(w, r)
48+
})
49+
}
50+
51+
func zerologMiddleware(next http.Handler) http.Handler {
52+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
53+
start := time.Now()
54+
ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
55+
56+
logger := zerolog.Ctx(r.Context())
57+
defer func() {
58+
logger.Info().
59+
Str("method", r.Method).
60+
Str("path", r.URL.Path).
61+
Int("status", ww.Status()).
62+
Int("bytes", ww.BytesWritten()).
63+
Str("remote", r.RemoteAddr).
64+
Dur("duration", time.Since(start)).
65+
Msg("handled request")
66+
}()
67+
68+
next.ServeHTTP(ww, r)
69+
})
4870
}
4971

5072
func ServeHTTP() {
@@ -66,6 +88,11 @@ func healthHandler(w http.ResponseWriter, r *http.Request) {
6688
}
6789

6890
func kartingHTMLHandler(w http.ResponseWriter, r *http.Request) {
91+
if r.Method != http.MethodGet {
92+
methodNotAllowedHandler(w, r)
93+
return
94+
}
95+
6996
w.Header().Set("Content-Type", "text/html")
7097
htmlContent := `
7198
<!DOCTYPE html>
@@ -84,7 +111,6 @@ func kartingHTMLHandler(w http.ResponseWriter, r *http.Request) {
84111
_, err := w.Write([]byte(htmlContent))
85112
if err != nil {
86113
hlog.FromRequest(r).Error().Err(err).Msg("")
87-
return
88114
}
89115
}
90116

vendor/github.com/go-chi/chi/v5/.gitignore

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)