-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.go
47 lines (39 loc) · 1.16 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
46
47
package gox
import (
"fmt"
"html/template"
"net/http"
m "github.com/narsus81/gox/internal/model"
)
func (g *Gox) loadChain() {
g.Chain = append(g.Chain, g.basicMiddleware)
g.Chain = append(g.Chain, g.rootMiddleware)
}
func chainingMiddleware(h http.Handler, mdw ...m.Middleware) http.Handler {
if len(mdw) < 1 {
return h
}
wrappedHandler := h
for i := len(mdw) - 1; i >= 0; i-- {
wrappedHandler = mdw[i](wrappedHandler)
}
return wrappedHandler
}
func (g *Gox) basicMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
route := g.patterns[r.Pattern]
fmt.Println("First middleware, the Pattern is: ", r.Pattern, " and the Route is: ", route.Name)
next.ServeHTTP(w, r)
})
}
func (g *Gox) rootMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
route := g.patterns[r.Pattern]
if route.Name == "root" {
fmt.Println("Root middleware, the Pattern is: ", r.Pattern, " and the Route is: ", route.Name)
templ := template.Must(template.ParseFiles(g.config.DefaultTmpl))
templ.Execute(w, g.config)
}
next.ServeHTTP(w, r)
})
}