/
t_example_test.go
85 lines (70 loc) · 2.5 KB
/
t_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
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
package rout_test
import (
"io"
"net/http"
"github.com/mitranim/rout"
)
type (
Rew = http.ResponseWriter
Req = *http.Request
Res = *http.Response
Han = http.Handler
)
func ExampleRou_Route() {
handleRequest(nil, nil)
}
// Top-level request handler.
func handleRequest(rew Rew, req Req) {
// Errors are handled ONLY in app code. There are no surprises.
err := rout.MakeRou(rew, req).Route(routes)
// Replace this with custom error handling.
rout.WriteErr(rew, err)
}
/*
This is executed for every request.
Unknown paths cause the router to return error 404. Unknown methods on known
paths cause the router to return error 405. The error is handled by YOUR code,
which is an important advantage; see `handleRequest` above.
*/
func routes(rou rout.Rou) {
rou.Pat(`/`).Get().Han(pageIndex)
rou.Pat(`/articles`).Get().Han(pageArticles)
rou.Pat(`/articles/{}`).Get().ParamHan(pageArticle)
rou.Sta(`/api`).Sub(routesApi)
rou.Get().Handler(fileServer)
}
var fileServer = http.FileServer(http.Dir(`public`))
// This is executed for every request that gets routed to it.
func routesApi(rou rout.Rou) {
/**
Enable CORS only for this route. This would usually involve middleware.
With `rout`, you just call A before B.
*/
allowCors(rou.Rew.Header())
rou.Sta(`/api/articles`).Sub(routesApiArticles)
}
// This is executed for every request that gets routed to it.
func routesApiArticles(rou rout.Rou) {
rou.Pat(`/api/articles`).Methods(func(rou rout.Rou) {
rou.Get().Han(apiArticleFeed)
rou.Post().Han(apiArticleCreate)
})
rou.Pat(`/api/articles/{}`).Methods(func(rou rout.Rou) {
rou.Get().ParamHan(apiArticleGet)
rou.Patch().ParamHan(apiArticleUpdate)
rou.Delete().ParamHan(apiArticleDelete)
})
}
// Oversimplified for example's sake.
func allowCors(head http.Header) {}
func pageIndex(req Req) Han { return Str(`ok`) }
func pageArticles(req Req) Han { return Str(`ok`) }
func pageArticle(req Req, args []string) Han { return Str(`ok`) }
func apiArticleFeed(req Req) Han { return Str(`ok`) }
func apiArticleCreate(req Req) Han { return Str(`ok`) }
func apiArticleGet(req Req, args []string) Han { return Str(`ok`) }
func apiArticleUpdate(req Req, args []string) Han { return Str(`ok`) }
func apiArticleDelete(req Req, args []string) Han { return Str(`ok`) }
type Str string
func (self Str) ServeHTTP(rew Rew, _ Req) { _, _ = io.WriteString(rew, string(self)) }
func (self Str) Ptr() *Str { return &self }