-
Notifications
You must be signed in to change notification settings - Fork 1
/
middleware.go
45 lines (42 loc) · 1.06 KB
/
middleware.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package nchi
import (
"net/http"
"github.com/muir/nvelope"
)
func translateMiddleware(raw []interface{}) []interface{} {
n := make([]interface{}, 0, len(raw))
hms := make([]func(http.Handler) http.Handler, 0, len(raw))
hfs := make([]func(http.HandlerFunc) http.HandlerFunc, 0, len(raw))
for i := 0; i < len(raw); i++ {
if h, ok := raw[i].(func(http.Handler) http.Handler); ok {
hms = append(hms, h)
var j int
for j = i + 1; j < len(raw); j++ {
h, ok := raw[j].(func(http.Handler) http.Handler)
if !ok {
break
}
hms = append(hms, h)
}
n = append(n, nvelope.MiddlewareHandlerBaseWriter(hms...))
i = j - 1
hms = hms[:0]
} else if h, ok := raw[i].(func(http.HandlerFunc) http.HandlerFunc); ok {
hfs = append(hfs, h)
var j int
for j = i + 1; j < len(raw); j++ {
h, ok := raw[j].(func(http.HandlerFunc) http.HandlerFunc)
if !ok {
break
}
hfs = append(hfs, h)
}
n = append(n, nvelope.MiddlewareBaseWriter(hfs...))
i = j - 1
hfs = hfs[:0]
} else {
n = append(n, raw[i])
}
}
return n
}