forked from jackwhelpton/fasthttp-routing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
49 lines (42 loc) · 1.24 KB
/
example_test.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
package routing_test
import (
"log"
"github.com/naharp/fasthttp-routing"
"github.com/naharp/fasthttp-routing/access"
"github.com/naharp/fasthttp-routing/content"
"github.com/naharp/fasthttp-routing/fault"
"github.com/naharp/fasthttp-routing/file"
"github.com/naharp/fasthttp-routing/slash"
"github.com/valyala/fasthttp"
)
func Example() {
router := routing.New()
router.Use(
// all these handlers are shared by every route
access.Logger(log.Printf),
slash.Remover(fasthttp.StatusMovedPermanently),
fault.Recovery(log.Printf),
)
// serve RESTful APIs
api := router.Group("/api")
api.Use(
// these handlers are shared by the routes in the api group only
content.TypeNegotiator(content.JSON, content.XML),
)
api.Get("/users", func(c *routing.Context) error {
return c.Write("user list")
})
api.Post("/users", func(c *routing.Context) error {
return c.Write("create a new user")
})
api.Put(`/users/<id:\d+>`, func(c *routing.Context) error {
return c.Write("update user " + c.Param("id"))
})
// serve index file
router.Get("/", file.Content("ui/index.html"))
// serve files under the "ui" subdirectory
router.Get("/*", file.Server(file.PathMap{
"/": "/ui/",
}))
fasthttp.ListenAndServe(":8080", router.HandleRequest)
}