Skip to content

Commit 3405ce4

Browse files
committed
feat(middleware): add process result state: middleware processed
Process results could be: - `GoNext` (aka `false`): middleware did not affect the request, or still need to process by rest middlewares. - `SkipRests`: middleware processed and should skip rest middlewares, but continue to process out of middleware. - `Processed` (aka `true`): request handled by middleware completely.
1 parent 06ea186 commit 3405ce4

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

src/middleware/main.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,12 @@ package middleware
22

33
import "net/http"
44

5-
type Middleware func(w http.ResponseWriter, r *http.Request, context *Context) (processed bool)
5+
type ProcessResult int
6+
7+
const (
8+
GoNext ProcessResult = iota
9+
SkipRests
10+
Processed
11+
)
12+
13+
type Middleware func(w http.ResponseWriter, r *http.Request, context *Context) (result ProcessResult)

src/serverHandler/middleware.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@ func (h *aliasHandler) postMiddleware(w http.ResponseWriter, r *http.Request, da
2424
}
2525

2626
for i := range h.postMiddlewares {
27-
if h.postMiddlewares[i](w, r, context) {
27+
result := h.postMiddlewares[i](w, r, context)
28+
if result == middleware.Processed {
2829
return true
30+
} else if result == middleware.SkipRests {
31+
break
2932
}
3033
}
3134

src/serverHandler/multiplexHandler.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ func (mux multiplexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
2323
VhostReqPath: r.URL.Path,
2424
}
2525
for i := range mux.preMiddlewares {
26-
processed := mux.preMiddlewares[i](w, r, middlewareContext)
27-
if processed {
26+
processResult := mux.preMiddlewares[i](w, r, middlewareContext)
27+
if processResult == middleware.Processed {
2828
return
29+
} else if processResult == middleware.SkipRests {
30+
break
2931
}
3032
}
3133
}

0 commit comments

Comments
 (0)