Skip to content

Commit c966f15

Browse files
committed
feat(serverHandler): add middleware interface
1 parent c299418 commit c966f15

File tree

6 files changed

+60
-4
lines changed

6 files changed

+60
-4
lines changed

src/middleware/context.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package middleware
2+
3+
type Context struct {
4+
PrefixReqPath string
5+
VhostReqPath string
6+
AliasReqPath string
7+
AliasFsPath string
8+
AliasFsRoot string
9+
}

src/middleware/main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package middleware
2+
3+
import "net/http"
4+
5+
type Middleware func(w http.ResponseWriter, r *http.Request, context *Context) (processed bool)

src/param/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package param
22

33
import (
44
"crypto/tls"
5+
"mjpclab.dev/ghfs/src/middleware"
56
"mjpclab.dev/ghfs/src/serverError"
67
"mjpclab.dev/ghfs/src/util"
78
"os"
@@ -83,6 +84,8 @@ type Param struct {
8384

8485
AccessLog string
8586
ErrorLog string
87+
88+
Middlewares []middleware.Middleware
8689
}
8790

8891
type Params []*Param

src/serverHandler/aliasHandler.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package serverHandler
22

33
import (
4+
"mjpclab.dev/ghfs/src/middleware"
45
"mjpclab.dev/ghfs/src/param"
56
"mjpclab.dev/ghfs/src/serverLog"
67
"mjpclab.dev/ghfs/src/tpl"
@@ -106,6 +107,8 @@ type aliasHandler struct {
106107
contentVaryV1 string
107108
contentVary string
108109

110+
middlewares []middleware.Middleware
111+
109112
fileServer http.Handler
110113
}
111114

@@ -131,7 +134,7 @@ func (h *aliasHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
131134
}
132135

133136
// data
134-
data := h.getResponseData(r)
137+
data, fsPath := h.getResponseData(r)
135138
h.logErrors(data.errors)
136139
file := data.File
137140
if file != nil {
@@ -167,7 +170,7 @@ func (h *aliasHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
167170
return
168171
}
169172

170-
// regular flows
173+
// archive
171174
if len(r.URL.RawQuery) >= 3 {
172175
switch r.URL.RawQuery[:3] {
173176
case "tar":
@@ -182,6 +185,12 @@ func (h *aliasHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
182185
}
183186
}
184187

188+
// middlewares
189+
if h.middleware(w, r, data, fsPath) {
190+
return
191+
}
192+
193+
// final process
185194
item := data.Item
186195
if data.WantJson {
187196
h.json(w, r, data)
@@ -275,6 +284,8 @@ func newAliasHandler(
275284
pageVary: ap.pageVary,
276285
contentVaryV1: ap.contentVaryV1,
277286
contentVary: ap.contentVary,
287+
288+
middlewares: p.Middlewares,
278289
}
279290
return h
280291
}

src/serverHandler/middleware.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package serverHandler
2+
3+
import (
4+
"mjpclab.dev/ghfs/src/middleware"
5+
"net/http"
6+
)
7+
8+
func (h *aliasHandler) middleware(w http.ResponseWriter, r *http.Request, data *responseData, fsPath string) (processed bool) {
9+
if len(h.middlewares) == 0 {
10+
return false
11+
}
12+
13+
context := &middleware.Context{
14+
PrefixReqPath: data.prefixReqPath,
15+
VhostReqPath: data.rawReqPath,
16+
AliasReqPath: data.handlerReqPath,
17+
AliasFsPath: fsPath,
18+
AliasFsRoot: h.root,
19+
}
20+
21+
for i := range h.middlewares {
22+
if h.middlewares[i](w, r, context) {
23+
return true
24+
}
25+
}
26+
27+
return false
28+
}

src/serverHandler/responseData.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ func dereferenceSymbolLinks(reqFsPath string, subItems []os.FileInfo) (errs []er
291291
return
292292
}
293293

294-
func (h *aliasHandler) getResponseData(r *http.Request) *responseData {
294+
func (h *aliasHandler) getResponseData(r *http.Request) (data *responseData, fsPath string) {
295295
var errs []error
296296

297297
prefixReqPath := r.URL.RawPath
@@ -458,5 +458,5 @@ func (h *aliasHandler) getResponseData(r *http.Request) *responseData {
458458
Context: context,
459459

460460
NeedDirSlashRedirect: needDirSlashRedirect,
461-
}
461+
}, reqFsPath
462462
}

0 commit comments

Comments
 (0)