Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pull] master from fasthttp:master #24

Open
wants to merge 115 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
115 commits
Select commit Hold shift + click to select a range
4087974
Small performance improvement
May 23, 2020
429531f
Update README
May 24, 2020
0dbe144
Upgrade fasthttp to v1.13.1
May 26, 2020
3338ac2
radix.node does not check siblings when first child match part of pat…
zxfishhack May 28, 2020
e65183a
Fix HandleMethodNotAllowed (#35)
Jun 2, 2020
f8df9de
Upgrade fasthttp to v1.14.0
Jun 2, 2020
e65038f
Upgrade gotils, update .travis-ci.yml and some fixes
Jun 8, 2020
7966096
Remove unused gover dependency
Jun 8, 2020
9620a51
Fix tests
Jun 8, 2020
055457d
Fix typo
Jun 8, 2020
b8d2a66
Upgrade dependencies
Jun 8, 2020
298eced
Force build
Jun 8, 2020
f41abad
Add Router.Mutable to update the route handler
Jun 9, 2020
bbf7915
Code fixes and minimal refactor
Jun 9, 2020
e06e8fa
Fix docs
Jun 9, 2020
3e590a4
Fix typo
Jun 15, 2020
5c3bd69
fix children conflict (#36)
milesdong Jul 19, 2020
d5d344c
Upgrade fasthttp to v1.15.1 and gotils
Jul 19, 2020
b310583
Fix mixing static + wildcard segment causes hung request #37
Jul 23, 2020
6528f5c
Upgrade fasthttp to v1.16.0
Aug 17, 2020
d1bb449
Add support to Go 1.15
Aug 17, 2020
2d2fdf9
Remove support to Go 1.11
Aug 17, 2020
16f85c3
Save path params as a copy #38
Aug 24, 2020
f947c3f
Fix get optional paths #39
Aug 24, 2020
b0e5c87
Improve performance
Sep 3, 2020
893131d
Use of cleaned path just when redirect to the fixed path #40
Sep 3, 2020
fc8225f
Add missing shortcuts
Sep 3, 2020
8ef2dc8
Upgrade gotils
Oct 12, 2020
5c77f27
Upgrade fasthttp to v1.17.0
Nov 8, 2020
495ce1b
Upgrade fasthttp to v1.18.0
Dec 11, 2020
8c5e42b
Upgrade dependencies
Jan 4, 2021
5916155
Upgrade gotils
Jan 5, 2021
890eabd
Upgrade fasthttp to v1.20.0 and others dependencies
Feb 11, 2021
291db29
typo parameters (#45)
kokizzu Feb 16, 2021
59f71bc
Upgrade fasthttp to v1.21.0 and gotils
Feb 17, 2021
0f5bbd6
Add support to Go v1.16.X
Feb 17, 2021
f74be76
hotfix(wildpath): set user empty value (#47)
1046102779 Mar 1, 2021
9a4b4db
Upgrade dependencies
Mar 2, 2021
cc03ff3
Bugfix (#49)
kimkit Mar 26, 2021
030ebe2
Fix bad and overflow redirections #50
Apr 3, 2021
1aec7f3
Upgrade fasthttp to v1.23.0 and gotils
Apr 3, 2021
8d81872
Upgrade fasthttp to v1.24.0
Apr 29, 2021
a5b8adf
Upgrade fasthttp to v1.25.0 and gotils
May 20, 2021
e284e8a
Upgrade fasthttp to v1.26.0
Jun 1, 2021
270963d
Upgrade fasthttp to v1.27.0 and gotils
Jun 21, 2021
1b0379a
Deprecate Go 1.12, 1.13 and 1.14
Jun 21, 2021
f1dfae9
Add github actions
Jun 21, 2021
f986d09
Update README
Jun 21, 2021
4c88c85
Run build workflow only on linux
Jun 21, 2021
53538e1
Fix typo
Jun 21, 2021
d7e5bc6
Simplify cleanPath
Jun 21, 2021
71240b7
Fix user params for regex paths (#52)
sashayakovtseva Jul 3, 2021
65fe692
Upgrade fasthttp to v1.28.0
Jul 22, 2021
4112ed4
Add support to Go 1.17.x
Aug 26, 2021
ffafa7e
Upgrade fasthttp to v1.29.0
Aug 26, 2021
aa309d5
feat: upgrade gotils dependency
Sep 8, 2021
fce00ab
Upgrade fasthttp to v1.30.0
Sep 10, 2021
fcdff20
chore: upgrade gotils
Sep 21, 2021
375a747
refactor(radix): improve error handling
Sep 21, 2021
1bd439e
Update Router.Lookup documentation (#55)
MarkArts Oct 11, 2021
e8b4c1b
chore: upgrade fasthttp to v1.31.0
Oct 11, 2021
f051d94
feat: upgrade fasthttp to v1.32.0 and gotils
Jan 11, 2022
0b87848
feat: upgrade fasthttp to v1.33.0
Jan 31, 2022
c714a84
feat: upgrade dependencies
Mar 23, 2022
8d410a9
fix: windows tests
Mar 23, 2022
13d85eb
chore: add support to Go 1.18
Mar 23, 2022
cf89ce0
fix: windows tests
Mar 23, 2022
a7447ba
fix: upgrade gotils lib
Mar 23, 2022
e698d40
feat: upgrade fasthttp to v1.35.0 and gotils
Apr 6, 2022
b90eb2d
feat: upgrade fasthttp to v1.36.0
May 9, 2022
b9f4533
feat: upgrade fasthttp to v1.37.0 and gotils
Jun 3, 2022
9d462dc
chore(ci): upgrade checkout and setup-go actions to v3
Jun 3, 2022
d9e4b5b
feat: upgrade fasthttp to v1.38.0
savsgio Jul 26, 2022
7e20e48
chore: add support to go v1.19
savsgio Sep 8, 2022
978f189
feat: upgrade fasthttp to v1.40.0
savsgio Sep 8, 2022
71c8a0c
feat: upgrade fasthttp to v1.41.0
savsgio Oct 28, 2022
fec4124
chore: deprecated Go 1.15
savsgio Oct 28, 2022
405cb38
feat: upgrade dependencies
savsgio Nov 24, 2022
670b7ce
feat: upgrade fasthttp to v1.44.0
savsgio Jan 19, 2023
b5e63c0
fix path substring incorrectly matching to route (#64)
hawell Jan 24, 2023
91fd40b
fix(radix/node): tsr for childs that becomes a final path
savsgio Feb 27, 2023
dfc141c
chore(ci): add support to go 1.20
savsgio Feb 27, 2023
d231958
docs: fix typo
savsgio Feb 27, 2023
2d790ee
feat: upgrade dependencies
savsgio Feb 27, 2023
c3fcfb3
test: fix not found by method
savsgio Feb 28, 2023
8341673
feat(group): add path validations
savsgio Feb 28, 2023
d860ec4
feat: upgrade dependencies
savsgio Mar 15, 2023
770ad62
chore: deprecated go 1.16
savsgio Mar 15, 2023
0c9be70
feat: upgrade dependencies
savsgio May 3, 2023
432d334
chore: upgrade actions/setup-go to v4
savsgio May 3, 2023
727449d
fix: force build
savsgio May 5, 2023
b2b7c5e
fix: upgrade dependencies
savsgio Jul 18, 2023
b4571be
fix: upgrade dependencies
savsgio Sep 6, 2023
68803fa
fix: upgrade dependencies
savsgio Oct 16, 2023
7a258ed
chore: add support to go 1.21
savsgio Oct 17, 2023
01ed86e
fix: upgrade dependencies
savsgio Nov 17, 2023
8de30ef
chore: deprecate Go 1.17
savsgio Nov 17, 2023
452cf2b
fix: go tidy
savsgio Nov 17, 2023
baf54d2
fix: upgrade dependencies
savsgio Mar 3, 2024
41cc258
chore: upgrade to v4 actions checkout
savsgio Mar 3, 2024
2175953
fix: upgrade dependencies
savsgio Mar 4, 2024
895727b
chore: deprecate go 1.18
savsgio Mar 4, 2024
d275c03
chore: add support to go 1.22
savsgio Mar 4, 2024
407bc93
feat: add support to serve fs.FS
savsgio Mar 4, 2024
bff70dd
fix: upgrade dependencies
savsgio May 23, 2024
e572ed5
chore(ci/test): fix finish job
savsgio May 23, 2024
702b579
fix: upgrade dependencies
savsgio Jul 4, 2024
3bf0033
chore: deprecate go 1.19
savsgio Jul 4, 2024
66da80f
fix: upgrade dependencies
savsgio Jul 4, 2024
932b478
chore(ci): remove test job
savsgio Jul 4, 2024
8225a4b
chore(ci/test): improve job
savsgio Jul 4, 2024
134f6b4
chore(ci/test): add macos-14
savsgio Jul 4, 2024
12bab28
fix: upgrade dependencies
savsgio Dec 4, 2024
f72fcdc
chore(ci): remove support to 1.20 and add 1.23
savsgio Dec 4, 2024
a90f305
fix: upgrade dependencies
savsgio Jan 1, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Test
on: [push, pull_request]
jobs:
test:
strategy:
matrix:
go-version: [1.21.x, 1.22.x, 1.23.x]
os: [ubuntu-latest, macos-latest, windows-latest, macos-14]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}
- run: go version
- run: go test -v -cover -shuffle=on ./...
- run: go test -v -cover -shuffle=on -race ./...

- name: Send coverage
uses: shogo82148/actions-goveralls@v1
with:
flag-name: Go-${{ matrix.os }}-${{ matrix.go-version }}
parallel: true

finish:
needs: test
runs-on: ubuntu-latest
steps:
- uses: shogo82148/actions-goveralls@v1
with:
parallel-finished: true
23 changes: 16 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
.vscode/
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

*.swp
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
.coverprofile

coverage.out
examples/basic/basic
examples/hosts/hosts
examples/auth/auth
# Dependency directories (remove the comment below to include it)
vendor/

**/vendor/
# IDE
.vscode/
53 changes: 0 additions & 53 deletions .travis.yml

This file was deleted.

11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Router

[![Build Status](https://travis-ci.org/fasthttp/router.svg?branch=master)](https://travis-ci.org/fasthttp/router)
[![Test status](https://github.com/fasthttp/router/actions/workflows/test.yml/badge.svg?branch=master)](https://github.com/fasthttp/router/actions?workflow=test)
[![Coverage Status](https://coveralls.io/repos/fasthttp/router/badge.svg?branch=master&service=github)](https://coveralls.io/github/fasthttp/router?branch=master)
[![Go Report Card](https://goreportcard.com/badge/github.com/fasthttp/router)](https://goreportcard.com/report/github.com/fasthttp/router)
[![GoDev](https://img.shields.io/badge/go.dev-reference-007d9c?logo=go&logoColor=white)](https://pkg.go.dev/github.com/fasthttp/router)
Expand Down Expand Up @@ -132,7 +132,7 @@ If you need define an optional parameters, add `?` at the end of param name. `{n

If you need define a validation, you could use a custom regex for the paramater value, add `:<regex>` after the name. For example: `{name:[a-zA-Z]{5}}`.

**_Optional paramters and regex validation are compatibles, only add `?` between the name and the regex. For example: `{name?:[a-zA-Z]{5}}`._**
**_Optional parameters and regex validation are compatibles, only add `?` between the name and the regex. For example: `{name?:[a-zA-Z]{5}}`._**

### Catch-All parameters

Expand Down Expand Up @@ -168,7 +168,8 @@ Priority Path Handle

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.

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.
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.


For even better scalability, the child nodes on each tree level are ordered by priority, where the priority is just the number of handles registered in sub nodes (children, grandchildren, and so on..). This helps in two ways:

Expand Down Expand Up @@ -199,8 +200,8 @@ This package just provides a very efficient request router with a few extra feat

Have a look at these middleware examples:

- [Auth Middleware](examples/auth)
- [Multi Hosts Middleware](examples/hosts)
- [Auth Middleware](_examples/auth)
- [Multi Hosts Middleware](_examples/hosts)

## Chaining with the NotFound handler

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
13 changes: 10 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
module github.com/fasthttp/router

go 1.11
go 1.21

toolchain go1.23.4

require (
github.com/savsgio/gotils v0.0.0-20200413113635-8c468ce75cca
github.com/savsgio/gotils v0.0.0-20240704082632-aef3928b8a38
github.com/valyala/bytebufferpool v1.0.0
github.com/valyala/fasthttp v1.12.0
github.com/valyala/fasthttp v1.58.0
)

require (
github.com/andybalholm/brotli v1.1.1 // indirect
github.com/klauspost/compress v1.17.11 // indirect
)
22 changes: 10 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
github.com/klauspost/compress v1.10.4 h1:jFzIFaf586tquEB5EhzQG0HwGNSlgAJpG53G6Ss11wc=
github.com/klauspost/compress v1.10.4/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
github.com/savsgio/gotils v0.0.0-20200413113635-8c468ce75cca h1:Qe7Mtuhjkk38HVpRtvWdziZJcwG3Qup1mfyvyOrcnyM=
github.com/savsgio/gotils v0.0.0-20200413113635-8c468ce75cca/go.mod h1:TWNAOTaVzGOXq8RbEvHnhzA/A2sLZzgn0m6URjnukY8=
github.com/andybalholm/brotli v1.1.1 h1:PR2pgnyFznKEugtsUo0xLdDop5SKXd5Qf5ysW+7XdTA=
github.com/andybalholm/brotli v1.1.1/go.mod h1:05ib4cKhjx3OQYUY22hTVd34Bc8upXjOLL2rKwwZBoA=
github.com/klauspost/compress v1.17.11 h1:In6xLpyWOi1+C7tXUUWv2ot1QvBjxevKAaI6IXrJmUc=
github.com/klauspost/compress v1.17.11/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0=
github.com/savsgio/gotils v0.0.0-20240704082632-aef3928b8a38 h1:D0vL7YNisV2yqE55+q0lFuGse6U8lxlg7fYTctlT5Gc=
github.com/savsgio/gotils v0.0.0-20240704082632-aef3928b8a38/go.mod h1:sM7Mt7uEoCeFSCBM+qBrqvEo+/9vdmj19wzp3yzUhmg=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.12.0 h1:TsB9qkSeiMXB40ELWWSRMjlsE+8IkqXHcs01y2d9aw0=
github.com/valyala/fasthttp v1.12.0/go.mod h1:229t1eWu9UXTPmoUkbpN/fctKPBY4IJoFXQnxHGXy6E=
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
github.com/valyala/fasthttp v1.58.0 h1:GGB2dWxSbEprU9j0iMJHgdKYJVDyjrOwF9RE59PbRuE=
github.com/valyala/fasthttp v1.58.0/go.mod h1:SYXvHHaFp7QZHGKSHmoMipInhrI5StHrhDTYVEjK/Kw=
github.com/xyproto/randomstring v1.0.5 h1:YtlWPoRdgMu3NZtP45drfy1GKoojuR7hmRcnhZqKjWU=
github.com/xyproto/randomstring v1.0.5/go.mod h1:rgmS5DeNXLivK7YprL0pY+lTuhNQW3iGxZ18UQApw/E=
151 changes: 151 additions & 0 deletions group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package router

import (
"io/fs"

"github.com/valyala/fasthttp"
)

// Group returns a new group.
// Path auto-correction, including trailing slashes, is enabled by default.
func (g *Group) Group(path string) *Group {
validatePath(path)

if len(g.prefix) > 0 && path == "/" {
return g
}

return g.router.Group(g.prefix + path)
}

// GET is a shortcut for group.Handle(fasthttp.MethodGet, path, handler)
func (g *Group) GET(path string, handler fasthttp.RequestHandler) {
validatePath(path)

g.router.GET(g.prefix+path, handler)
}

// HEAD is a shortcut for group.Handle(fasthttp.MethodHead, path, handler)
func (g *Group) HEAD(path string, handler fasthttp.RequestHandler) {
validatePath(path)

g.router.HEAD(g.prefix+path, handler)
}

// POST is a shortcut for group.Handle(fasthttp.MethodPost, path, handler)
func (g *Group) POST(path string, handler fasthttp.RequestHandler) {
validatePath(path)

g.router.POST(g.prefix+path, handler)
}

// PUT is a shortcut for group.Handle(fasthttp.MethodPut, path, handler)
func (g *Group) PUT(path string, handler fasthttp.RequestHandler) {
validatePath(path)

g.router.PUT(g.prefix+path, handler)
}

// PATCH is a shortcut for group.Handle(fasthttp.MethodPatch, path, handler)
func (g *Group) PATCH(path string, handler fasthttp.RequestHandler) {
validatePath(path)

g.router.PATCH(g.prefix+path, handler)
}

// DELETE is a shortcut for group.Handle(fasthttp.MethodDelete, path, handler)
func (g *Group) DELETE(path string, handler fasthttp.RequestHandler) {
validatePath(path)

g.router.DELETE(g.prefix+path, handler)
}

// OPTIONS is a shortcut for group.Handle(fasthttp.MethodOptions, path, handler)
func (g *Group) CONNECT(path string, handler fasthttp.RequestHandler) {
validatePath(path)

g.router.CONNECT(g.prefix+path, handler)
}

// OPTIONS is a shortcut for group.Handle(fasthttp.MethodOptions, path, handler)
func (g *Group) OPTIONS(path string, handler fasthttp.RequestHandler) {
validatePath(path)

g.router.OPTIONS(g.prefix+path, handler)
}

// OPTIONS is a shortcut for group.Handle(fasthttp.MethodOptions, path, handler)
func (g *Group) TRACE(path string, handler fasthttp.RequestHandler) {
validatePath(path)

g.router.TRACE(g.prefix+path, handler)
}

// ANY is a shortcut for group.Handle(router.MethodWild, path, handler)
//
// WARNING: Use only for routes where the request method is not important
func (g *Group) ANY(path string, handler fasthttp.RequestHandler) {
validatePath(path)

g.router.ANY(g.prefix+path, handler)
}

// ServeFiles serves files from the given file system root path.
// The path must end with "/{filepath:*}", files are then served from the local
// path /defined/root/dir/{filepath:*}.
// For example if root is "/etc" and {filepath:*} is "passwd", the local file
// "/etc/passwd" would be served.
// Internally a fasthttp.FSHandler is used, therefore http.NotFound is used instead
// Use:
//
// router.ServeFiles("/src/{filepath:*}", "./")
func (g *Group) ServeFiles(path string, rootPath string) {
validatePath(path)

g.router.ServeFiles(g.prefix+path, rootPath)
}

// ServeFS serves files from the given file system.
// The path must end with "/{filepath:*}", files are then served from the local
// path /defined/root/dir/{filepath:*}.
// For example if root is "/etc" and {filepath:*} is "passwd", the local file
// "/etc/passwd" would be served.
// Internally a fasthttp.FSHandler is used, therefore http.NotFound is used instead
// Use:
//
// router.ServeFS("/src/{filepath:*}", myFilesystem)
func (g *Group) ServeFS(path string, filesystem fs.FS) {
validatePath(path)

g.router.ServeFS(g.prefix+path, filesystem)
}

// ServeFilesCustom serves files from the given file system settings.
// The path must end with "/{filepath:*}", files are then served from the local
// path /defined/root/dir/{filepath:*}.
// For example if root is "/etc" and {filepath:*} is "passwd", the local file
// "/etc/passwd" would be served.
// Internally a fasthttp.FSHandler is used, therefore http.NotFound is used instead
// of the Router's NotFound handler.
// Use:
//
// router.ServeFilesCustom("/src/{filepath:*}", *customFS)
func (g *Group) ServeFilesCustom(path string, fs *fasthttp.FS) {
validatePath(path)

g.router.ServeFilesCustom(g.prefix+path, fs)
}

// Handle registers a new request handler with the given path and method.
//
// For GET, POST, PUT, PATCH and DELETE requests the respective shortcut
// functions can be used.
//
// This function is intended for bulk loading and to allow the usage of less
// frequently used, non-standardized or custom methods (e.g. for internal
// communication with a proxy).
func (g *Group) Handle(method, path string, handler fasthttp.RequestHandler) {
validatePath(path)

g.router.Handle(method, g.prefix+path, handler)
}
Loading