Skip to content

Commit 4598cca

Browse files
committed
feat(middleware): add extra fields to Context
- Added `NeedAuth` to Context - Added `AuthUserName` to Context - Added `AuthSuccess` to Context - Added `RestrictAccess` to Context - Added `AllowAccess` to Context - Added `Logger` to Context
1 parent 4e5d91d commit 4598cca

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

src/middleware/context.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package middleware
22

3-
import "os"
3+
import (
4+
"mjpclab.dev/ghfs/src/serverLog"
5+
"os"
6+
)
47

58
type Context struct {
69
PrefixReqPath string
@@ -9,8 +12,17 @@ type Context struct {
912
AliasFsPath string
1013
AliasFsRoot string
1114

15+
NeedAuth bool
16+
AuthUserName string
17+
AuthSuccess bool
18+
19+
RestrictAccess bool
20+
AllowAccess bool
21+
22+
Status int
23+
1224
Item os.FileInfo
1325
SubItems []os.FileInfo
1426

15-
Status int
27+
Logger *serverLog.Logger
1628
}

src/serverHandler/middleware.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,19 @@ func (h *aliasHandler) postMiddleware(w http.ResponseWriter, r *http.Request, da
1717
AliasFsPath: fsPath,
1818
AliasFsRoot: h.root,
1919

20+
NeedAuth: data.NeedAuth,
21+
AuthUserName: data.AuthUserName,
22+
AuthSuccess: data.AuthSuccess,
23+
24+
RestrictAccess: data.RestrictAccess,
25+
AllowAccess: data.AllowAccess,
26+
27+
Status: data.Status,
28+
2029
Item: data.Item,
2130
SubItems: data.SubItems,
2231

23-
Status: data.Status,
32+
Logger: h.logger,
2433
}
2534

2635
for i := range h.postMiddlewares {

src/serverHandler/multiplexHandler.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package serverHandler
33
import (
44
"mjpclab.dev/ghfs/src/middleware"
55
"mjpclab.dev/ghfs/src/param"
6+
"mjpclab.dev/ghfs/src/serverLog"
67
"net/http"
78
)
89

@@ -12,6 +13,7 @@ type aliasWithHandler struct {
1213
}
1314

1415
type multiplexHandler struct {
16+
logger *serverLog.Logger
1517
preMiddlewares []middleware.Middleware
1618
aliasWithHandlers []aliasWithHandler
1719
}
@@ -21,6 +23,7 @@ func (mux multiplexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
2123
middlewareContext := &middleware.Context{
2224
PrefixReqPath: r.URL.RawPath, // init by pathTransformHandler
2325
VhostReqPath: r.URL.Path,
26+
Logger: mux.logger,
2427
}
2528
for i := range mux.preMiddlewares {
2629
processResult := mux.preMiddlewares[i](w, r, middlewareContext)
@@ -68,6 +71,7 @@ func newMultiplexHandler(
6871
}
6972

7073
return multiplexHandler{
74+
logger: ap.logger,
7175
preMiddlewares: p.PreMiddlewares,
7276
aliasWithHandlers: aliasWithHandlers,
7377
}

0 commit comments

Comments
 (0)