You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
commit 9d75223df8f161a17a74a92fcff42a34bc4387bf
Author: Sergio Andres Virviescas Santana <[email protected]>
Date: Sun Mar 8 18:36:23 2020 +0100
Uddate README of examples
commit 6bfedb6a9cc4634a6f56be54b0785f8806a2e3cb
Author: Sergio Andres Virviescas Santana <[email protected]>
Date: Sun Mar 8 18:34:10 2020 +0100
Update README
commit a77a142578e14a6e7b129f0f1e6bd29e56ec1ee8
Author: Sergio Andres Virviescas Santana <[email protected]>
Date: Sun Mar 8 17:57:12 2020 +0100
Add docs and fixes
commit 45fb922f61ae0cb3b7f1095f43462c6ba20a53ff
Author: Sergio Andres Virviescas Santana <[email protected]>
Date: Sat Mar 7 20:53:40 2020 +0100
New radix tree with more flexibility without lose performance
Router is forked from [httprouter](https://github.com/julienschmidt/httprouter) which is a lightweight high performance HTTP request router
9
-
(also called *multiplexer* or just *mux* for short) for [fasthttp](https://github.com/valyala/fasthttp).
9
+
Router is a lightweight high performance HTTP request router (also called _multiplexer_ or just _mux_ for short) for [fasthttp](https://github.com/valyala/fasthttp).
10
10
11
11
This router is optimized for high performance and a small memory footprint. It scales well even with very long paths and a large number of routes. A compressing dynamic trie (radix tree) structure is used for efficient matching.
12
12
13
13
#### License Related
14
14
15
+
- This repo is based on [httprouter](https://github.com/julienschmidt/httprouter).
15
16
- The author of `httprouter`[@julienschmidt](https://github.com/julienschmidt) did almost all the hard work of this router.
16
-
- The author of `fasthttprouter`[@buaazp](https://github.com/buaazp) did all the hard work by porting `httprouter` to fasthttp.
17
-
- We ([@buaazp](https://github.com/buaazp) and fasthttp ecosystem team) respect the laws of open source. So LICENSE of `httprouter` is alway stay here: [HttpRouterLicense](HttpRouterLicense).
18
-
- What [@buaazp](https://github.com/buaazp) have done is just fit for `fasthttp`. I have no hope to build a huge but toxic go web framwork like [iris](https://github.com/kataras/iris).
19
-
-[@buaazp](https://github.com/buaazp) forked `fasthttp` repo is just because there is no router for `fasthttp` at that time. And `fasthttprouter` is the FIRST router for `fasthttp`.
20
-
-`fasthttprouter` has been used in huge production environments and processes hundreds of millions requests per day. It is fast and stable, so [@buaazp](https://github.com/buaazp) decide to release a stable version.
21
-
- This repo is based on `fasthttprouter` and maintained by official fasthttp ecosystem team. We trying to optimize it even more and check if it works well when we update something huge in fasthttp.
22
-
17
+
- We (fasthttp ecosystem team) respect the laws of open source. So LICENSE of `httprouter` is alway stay here: [HttpRouterLicense](HttpRouterLicense).
23
18
24
19
## Features
25
20
@@ -31,16 +26,16 @@ This router is optimized for high performance and a small memory footprint. It s
31
26
32
27
- Concurrency Test (allocations): In 30 ms processing time, the tets result for 100, 1000, 5000 clients is:
See below for technical details of the implementation.
39
34
40
35
**Only explicit matches:** With other routers, like [http.ServeMux](http://golang.org/pkg/net/http/#ServeMux),
41
36
a requested URL path could match multiple patterns. Therefore they have some
42
-
awkward pattern priority rules, like *longest match* or *first registered,
43
-
first matched*. By design of this router, a request can only match exactly one
37
+
awkward pattern priority rules, like _longest match_ or _first registered,
38
+
first matched_. By design of this router, a request can only match exactly one
44
39
or no route. As a result, there are also no unintended matches, which makes it
45
40
great for SEO and improves the user experience.
46
41
@@ -73,7 +68,7 @@ PanicHandler log what happened and deliver a nice error page.
73
68
RESTful APIs. Moreover it has builtin native support for [OPTIONS requests](http://zacstewart.com/2012/04/14/http-options-method.html)
74
69
and `405 Method Not Allowed` replies.
75
70
76
-
Of course you can also set **custom [NotFound](http://godoc.org/github.com/fasthttp/router#Router.NotFound) and [MethodNotAllowed](http://godoc.org/github.com/fasthttp/router#Router.MethodNotAllowed) handlers** and [**serve static files**](http://godoc.org/github.com/fasthttp/router#Router.ServeFiles).
71
+
Of course you can also set **custom [NotFound](http://godoc.org/github.com/fasthttp/router#Router.NotFound) and [MethodNotAllowed](http://godoc.org/github.com/fasthttp/router#Router.MethodNotAllowed) handlers** and [**serve static files**](http://godoc.org/github.com/fasthttp/router#Router.ServeFiles).
77
72
78
73
## Usage
79
74
@@ -111,7 +106,7 @@ func main() {
111
106
112
107
### Named parameters
113
108
114
-
As you can see, `:name` is a *named parameter*. The values are accessible via `RequestCtx.UserValues`. You can get the value of a parameter by using the `ctx.UserValue("name")`.
109
+
As you can see, `:name` is a _named parameter_. The values are accessible via `RequestCtx.UserValues`. You can get the value of a parameter by using the `ctx.UserValue("name")`.
115
110
116
111
Named parameters only match a single path segment:
117
112
@@ -127,11 +122,12 @@ Pattern: /user/:user
127
122
**Note:** Since this router has only explicit matches, you can not register static routes and parameters for the same path segment. For example you can not register the patterns `/user/new` and `/user/:user` for the same request method at the same time. The routing of different request methods is independent from each other.
128
123
129
124
#### Optional parameters
125
+
130
126
If you need define an optional parameters, add `?` at the end of param name. `:name?`
131
127
132
128
### Catch-All parameters
133
129
134
-
The second type are *catch-all* parameters and have the form `*name`.
130
+
The second type are _catch-all_ parameters and have the form `*name`.
135
131
Like the name suggests, they match everything.
136
132
Therefore they must always be at the **end** of the pattern:
137
133
@@ -145,7 +141,7 @@ Pattern: /src/*filepath
145
141
146
142
## How does it work?
147
143
148
-
The router relies on a tree structure which makes heavy use of *common prefixes*, it is basically a *compact*[*prefix tree*](https://en.wikipedia.org/wiki/Trie) (or just [*Radix tree*](https://en.wikipedia.org/wiki/Radix_tree)). Nodes with a common prefix also share a common parent. Here is a short example what the routing tree for the `GET` request method could look like:
144
+
The router relies on a tree structure which makes heavy use of _common prefixes_, it is basically a _compact_[_prefix tree_](https://en.wikipedia.org/wiki/Trie) (or just [_Radix tree_](https://en.wikipedia.org/wiki/Radix_tree)). Nodes with a common prefix also share a common parent. Here is a short example what the routing tree for the `GET` request method could look like:
149
145
150
146
```
151
147
Priority Path Handle
@@ -161,7 +157,7 @@ Priority Path Handle
161
157
1 └contact\ *<8>
162
158
```
163
159
164
-
Every `*<num>` represents the memory address of a handler function (a pointer). If you follow a path trough the tree from the root to the leaf, you get the complete route path, e.g `\blog\:post\`, where `:post` is just a placeholder ([*parameter*](#named-parameters)) for an actual post name. Unlike hash-maps, a tree structure also allows us to use dynamic parts like the `:post` parameter, since we actually match against the routing patterns instead of just comparing hashes. [As benchmarks show][benchmark], this works very well and efficient.
160
+
Every `*<num>` represents the memory address of a handler function (a pointer). If you follow a path trough the tree from the root to the leaf, you get the complete route path, e.g `\blog\:post\`, where `:post` is just a placeholder ([_parameter_](#named-parameters)) for an actual post name. Unlike hash-maps, a tree structure also allows us to use dynamic parts like the `:post` parameter, since we actually match against the routing patterns instead of just comparing hashes. [As benchmarks show][benchmark], this works very well and efficient.
165
161
166
162
Since URL paths have a hierarchical structure and make use only of a limited set of characters (byte values), it is very likely that there are a lot of common prefixes. This allows us to easily reduce the routing into ever smaller problems. Moreover the router manages a separate tree for every request method. For one thing it is more space efficient than holding a method->handle map in every single node, for another thing is also allows us to greatly reduce the routing problem before even starting the look-up in the prefix-tree.
167
163
@@ -188,7 +184,7 @@ Fasthttp works with [RequestHandler](https://godoc.org/github.com/valyala/fastht
188
184
189
185
Just try it out for yourself, the usage of Router is very straightforward. The package is compact and minimalistic, but also probably one of the easiest routers to set up.
190
186
191
-
## Where can I find Middleware *X*?
187
+
## Where can I find Middleware _X_?
192
188
193
189
This package just provides a very efficient request router with a few extra features. The router is just a [`fasthttp.RequestHandler`](https://godoc.org/github.com/valyala/fasthttp#RequestHandler), you can chain any `fasthttp.RequestHandler` compatible middleware before the router. Or you could [just write your own](https://justinas.org/writing-http-middleware-in-go/), it's very easy!
194
190
@@ -204,6 +200,7 @@ Have a look at these middleware examples:
204
200
You can use another [http.Handler](http://golang.org/pkg/net/http/#Handler), for example another router, to handle requests which could not be matched by this router by using the [Router.NotFound](http://godoc.org/github.com/fasthttp/router#Router.NotFound) handler. This allows chaining.
205
201
206
202
### Static files
203
+
207
204
The `NotFound` handler can for example be used to serve static files from the root path `/` (like an index.html file along with other assets):
Copy file name to clipboardExpand all lines: examples/auth/README.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
# Example of FastHttpRouter
1
+
# Example of Router
2
2
3
3
These examples show you the usage of `router`. You can easily build a web application with it. Or you can make your own midwares such as custom logger, metrics, or any one you want.
Copy file name to clipboardExpand all lines: examples/basic/README.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
# Example of FastHttpRouter
1
+
# Example of Router
2
2
3
3
These examples show you the usage of `router`. You can easily build a web application with it. Or you can make your own midwares such as custom logger, metrics, or any one you want.
Copy file name to clipboardExpand all lines: examples/hosts/README.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
# Example of FastHttpRouter
1
+
# Example of Router
2
2
3
3
These examples show you the usage of `router`. You can easily build a web application with it. Or you can make your own midwares such as custom logger, metrics, or any one you want.
0 commit comments