-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
45 lines (37 loc) · 784 Bytes
/
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
package main
import (
"github.com/nidorx/chain"
"log"
"net/http"
)
func main() {
router := chain.New()
// Middleware
router.Use(func(ctx *chain.Context, next func() error) error {
println("first middleware")
return next()
})
router.Use("GET", "/*", func(ctx *chain.Context) {
println("second middleware")
})
// Handler
router.GET("/", func(ctx *chain.Context) {
ctx.Write([]byte("Hello World!"))
})
// Grouping
v1 := router.Group("/v1")
{
v1.GET("/users", func(ctx *chain.Context) {
ctx.Write([]byte("[001]"))
})
}
v2 := router.Group("/v2")
{
v2.GET("/users", func(ctx *chain.Context) {
ctx.Write([]byte("[002]"))
})
}
if err := http.ListenAndServe("localhost:8080", router); err != nil {
log.Fatalf("ListenAndServe: %v", err)
}
}