-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
main.go
94 lines (82 loc) · 2.97 KB
/
main.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Package main will explore the helpers for middleware(s) that Muxie has to offer,
// but they are totally optional, you can still use your favourite pattern
// to wrap route handlers.
//
// Example of usage of an external net/http common middleware:
//
// import "github.com/rs/cors"
//
// mux := muxie.New()
// mux.Use(cors.Default().Handler)
//
//
// To wrap a specific route or even if for some reason you want to wrap the entire router
// use the `Pre(middlewares...).For(mainHandler)` as :
//
// wrapped := muxie.Pre(cors.Default().Handler, ...).For(mux)
// http.ListenAndServe(..., wrapped)
package main
import (
"log"
"net/http"
"github.com/kataras/muxie"
)
func main() {
mux := muxie.NewMux()
// Globally, will be inherited by all sub muxes as well unless `Of(...).Unlink()` called.
mux.Use(myGlobalMiddleware)
// Per Route.
mux.Handle("/", muxie.Pre(myFirstRouteMiddleware, mySecondRouteMiddleware).ForFunc(myMainRouteHandler))
// Per Group.
inheritor := mux.Of("/inheritor")
inheritor.Use(myMiddlewareForSubmux)
inheritor.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
log.Println("execute: my inheritor's main index route's handler")
w.Write([]byte("Hello from /inheritor\n"))
})
// Per Group, without its parents' middlewares.
// Unlink will clear all middlewares for this sub mux.
orphan := mux.Of("/orphan").Unlink()
orphan.Use(myMiddlewareForSubmux)
orphan.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
log.Println("execute: orphan's main index route's handler")
w.Write([]byte("Hello from /orphan\n"))
})
// Open your web browser or any other HTTP Client
// and navigate through the below endpoinds, one by one,
// and check the console output of your webserver.
//
// http://localhost:8080
// http://localhost:8080/inheritor
// http://localhost:8080/orphan
log.Println("Server started at http://localhost:8080")
http.ListenAndServe(":8080", mux)
}
func myGlobalMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Println("execute: my global and first of all middleware for all following mux' routes and sub muxes")
next.ServeHTTP(w, r)
})
}
func myMiddlewareForSubmux(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Println("execute: my submux' routes middleware")
next.ServeHTTP(w, r)
})
}
func myFirstRouteMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Println("execute: my first specific route's middleware")
next.ServeHTTP(w, r)
})
}
func mySecondRouteMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Println("execute: my second specific route's middleware")
next.ServeHTTP(w, r)
})
}
func myMainRouteHandler(w http.ResponseWriter, r *http.Request) {
log.Println("execute: my main route's handler")
w.Write([]byte("Hello World!\n"))
}