Skip to content

Commit 657f768

Browse files
committed
refactor(serverHandler): extract access log to log handler
This is to make sure log access request before processing pre-middleware.
1 parent 460326a commit 657f768

File tree

4 files changed

+28
-6
lines changed

4 files changed

+28
-6
lines changed

src/serverHandler/aliasHandler.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,6 @@ func (h *aliasHandler) preCheck(w http.ResponseWriter, r *http.Request, data *re
135135
}
136136

137137
func (h *aliasHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
138-
h.logRequest(r)
139-
140138
// hsts redirect
141139
if h.globalHsts && h.hsts(w, r) {
142140
return

src/serverHandler/log.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
"net/url"
88
)
99

10-
func (h *aliasHandler) logRequest(r *http.Request) {
11-
if !h.logger.CanLogAccess() {
10+
func logRequest(logger *serverLog.Logger, r *http.Request) {
11+
if !logger.CanLogAccess() {
1212
return
1313
}
1414

@@ -36,7 +36,7 @@ func (h *aliasHandler) logRequest(r *http.Request) {
3636
}
3737
buf = append(buf, uri...)
3838

39-
go h.logger.LogAccess(buf)
39+
go logger.LogAccess(buf)
4040
}
4141

4242
func (h *aliasHandler) logMutate(username, action, detail string, r *http.Request) {

src/serverHandler/logHandler.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package serverHandler
2+
3+
import (
4+
"mjpclab.dev/ghfs/src/serverLog"
5+
"net/http"
6+
)
7+
8+
type logHandler struct {
9+
logger *serverLog.Logger
10+
nextHandler http.Handler
11+
}
12+
13+
func (l logHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
14+
logRequest(l.logger, r)
15+
l.nextHandler.ServeHTTP(w, r)
16+
}
17+
18+
func newLogHandler(logger *serverLog.Logger, nextHandler http.Handler) http.Handler {
19+
return logHandler{
20+
logger: logger,
21+
nextHandler: nextHandler,
22+
}
23+
}

src/serverHandler/vhostHandler.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ func NewVhostHandler(
104104
}
105105

106106
muxHandler := newMultiplexHandler(p, ap)
107-
pathTransformHandler := newPathTransformHandler(p.PrefixUrls, muxHandler)
107+
logHandler := newLogHandler(logger, muxHandler)
108+
pathTransformHandler := newPathTransformHandler(p.PrefixUrls, logHandler)
108109

109110
return pathTransformHandler, nil
110111
}

0 commit comments

Comments
 (0)