-
Notifications
You must be signed in to change notification settings - Fork 4
/
phi.go
213 lines (192 loc) · 6.12 KB
/
phi.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Package phi is a small, idiomatic and composable router for building HTTP services.
//
// phi requires Go 1.7 or newer.
//
// Example:
// package main
//
// import (
// "log"
// "time"
//
// "github.com/fate-lovely/phi"
// "github.com/valyala/fasthttp"
// )
//
// func main() {
// r := phi.NewRouter()
//
// reqIDMW := func(next phi.HandlerFunc) phi.HandlerFunc {
// return func(ctx *fasthttp.RequestCtx) {
// next(ctx)
// ctx.WriteString("+reqid=1")
// }
// }
// r.Use(reqIDMW)
//
// r.Get("/", func(ctx *fasthttp.RequestCtx) {
// ctx.WriteString("index")
// })
// r.NotFound(func(ctx *fasthttp.RequestCtx) {
// ctx.WriteString("whoops, not found")
// ctx.SetStatusCode(404)
// })
// r.MethodNotAllowed(func(ctx *fasthttp.RequestCtx) {
// ctx.WriteString("whoops, bad method")
// ctx.SetStatusCode(405)
// })
//
// // tasks
// r.Group(func(r phi.Router) {
// mw := func(next phi.HandlerFunc) phi.HandlerFunc {
// return func(ctx *fasthttp.RequestCtx) {
// next(ctx)
// ctx.WriteString("+task")
// }
// }
// r.Use(mw)
//
// r.Get("/task", func(ctx *fasthttp.RequestCtx) {
// ctx.WriteString("task")
// })
// r.Post("/task", func(ctx *fasthttp.RequestCtx) {
// ctx.WriteString("new task")
// })
//
// caution := func(next phi.HandlerFunc) phi.HandlerFunc {
// return func(ctx *fasthttp.RequestCtx) {
// next(ctx)
// ctx.WriteString("+caution")
// }
// }
// r.With(caution).Delete("/task", func(ctx *fasthttp.RequestCtx) {
// ctx.WriteString("delete task")
// })
// })
//
// // cat
// r.Route("/cat", func(r phi.Router) {
// r.NotFound(func(ctx *fasthttp.RequestCtx) {
// ctx.WriteString("no such cat")
// ctx.SetStatusCode(404)
// })
// r.Use(func(next phi.HandlerFunc) phi.HandlerFunc {
// return func(ctx *fasthttp.RequestCtx) {
// next(ctx)
// ctx.WriteString("+cat")
// }
// })
// r.Get("/", func(ctx *fasthttp.RequestCtx) {
// ctx.WriteString("cat")
// })
// r.Patch("/", func(ctx *fasthttp.RequestCtx) {
// ctx.WriteString("patch cat")
// })
// })
//
// // user
// userRouter := phi.NewRouter()
// userRouter.NotFound(func(ctx *fasthttp.RequestCtx) {
// ctx.WriteString("no such user")
// ctx.SetStatusCode(404)
// })
// userRouter.Use(func(next phi.HandlerFunc) phi.HandlerFunc {
// return func(ctx *fasthttp.RequestCtx) {
// next(ctx)
// ctx.WriteString("+user")
// }
// })
// userRouter.Get("/", func(ctx *fasthttp.RequestCtx) {
// ctx.WriteString("user")
// })
// userRouter.Post("/", func(ctx *fasthttp.RequestCtx) {
// ctx.WriteString("new user")
// })
// r.Mount("/user", userRouter)
//
// server := &fasthttp.Server{
// Handler: r.ServeFastHTTP,
// ReadTimeout: 10 * time.Second,
// }
//
// log.Fatal(server.ListenAndServe(":7789"))
// }
//
// See github.com/fate-lovely/phi/examples/ for more in-depth examples.
//
package phi
import (
"github.com/valyala/fasthttp"
)
// Handler represents a fasthttp request handler,
// it has one method: ServeFastHTTP, which is equal to fasthttp.RequestHandler
type Handler interface {
ServeFastHTTP(ctx *fasthttp.RequestCtx)
}
// HandlerFunc type is an adapter to allow the use of
// ordinary functions as handlers.
type HandlerFunc func(ctx *fasthttp.RequestCtx)
// ServeFastHTTP calss fn(ctx)
func (fn HandlerFunc) ServeFastHTTP(ctx *fasthttp.RequestCtx) {
fn(ctx)
}
// Middleware represents phi middlewares, which accept a HandlerFunc and return a HandlerFunc
type Middleware func(HandlerFunc) HandlerFunc
// Middlewares type is a slice of standard middleware handlers with methods
// to compose middleware chains and phi.Handler's.
// type Middlewares []func(Handler) Handler
type Middlewares []Middleware
// NewRouter returns a new Mux object that implements the Router interface.
func NewRouter() *Mux {
return NewMux()
}
// Router consisting of the core routing methods used by phi's Mux,
type Router interface {
Handler
Routes
// Use appends one of more middlewares onto the Router stack.
Use(middlewares ...Middleware)
// With adds inline middlewares for an endpoint handler.
With(middlewares ...Middleware) Router
// Group adds a new inline-Router along the current routing
// path, with a fresh middleware stack for the inline-Router.
Group(fn func(r Router))
// Route mounts a sub-Router along a `pattern`` string.
Route(pattern string, fn func(r Router))
// Mount attaches another phi.Handler along ./pattern/*
Mount(pattern string, h Handler)
// Handle and HandleFunc adds routes for `pattern` that matches
// all HTTP methods.
Handle(pattern string, h HandlerFunc)
// Method and MethodFunc adds routes for `pattern` that matches
// the `method` HTTP method.
Method(method, pattern string, h HandlerFunc)
// HTTP-method routing along `pattern`
Connect(pattern string, h HandlerFunc)
Delete(pattern string, h HandlerFunc)
Get(pattern string, h HandlerFunc)
Head(pattern string, h HandlerFunc)
Options(pattern string, h HandlerFunc)
Patch(pattern string, h HandlerFunc)
Post(pattern string, h HandlerFunc)
Put(pattern string, h HandlerFunc)
Trace(pattern string, h HandlerFunc)
// NotFound defines a handler to respond whenever a route could
// not be found.
NotFound(h HandlerFunc)
// MethodNotAllowed defines a handler to respond whenever a method is
// not allowed.
MethodNotAllowed(h HandlerFunc)
}
// Routes interface adds two methods for router traversal, which is also
// used by the `docgen` subpackage to generation documentation for Routers.
type Routes interface {
// Routes returns the routing tree in an easily traversable structure.
Routes() []Route
// Middlewares returns the list of middlewares in use by the router.
Middlewares() Middlewares
// Match searches the routing tree for a handler that matches
// the method/path - similar to routing a http request, but without
// executing the handler thereafter.
Match(rctx *Context, method, path string) bool
}