Comments (11)
I will implement it soon.
Thanks for your advise!
from router.
I've just released v1.1.0 😉
Please, check: https://pkg.go.dev/github.com/fasthttp/router?tab=doc#Router.ANY
from router.
I'm working on it!
from router.
I think it would be nice to add a separate method to add a route that can accept any methods at the specified address, like
r.HandleAny("/route/path", func(ctx *fasthttp.RequestCtx) { ... })
from router.
Why do you need to handle any method from one handler?? When I say any method, i mean unknown methods. That could generate unexpected behaviours and maybe sometimes need to check if the http method is valid.
r.HandleAny("/route/path", func(ctx *fasthttp.RequestCtx) { ... })
This route should accept GET
, POST
, ... , UNICORN
, MYCUSTOMETHOD
, etc
The following request returns an status code and other things:
curl -X GET http://example.com/route/path
--> 200
curl -X POST http://example.com/route/path
--> 200
curl -X UNICORN http://example.com/route/path
--> This is valid??? Are you prepare to expect that?
Maybe if you need to authenticate the request you could wrap your handlers (like a middleware) and validate.
Or if you want to handle some methods for the same handler you could do:
validMethods := []string{"GET", "POST", "UNICORN"}
handler := func(ctx *fasthttp.RequestCtx) { ... }
for _, method := range validMethods {
router.Handle(method, "/some/path", handler)
}
I don't sure to add a wildcard method in the router, because like i said, it could generate unexpected behaviours and errors. So you must need to know what HTTP methods are expected, therefore a dirty code.
from router.
@savsgio The Server
structure in fasthttp
package contains the Handler
field, which does not validate methods by accepting any requests.
I expect that I will not have to juggle either router.Router{...}.Handler
or fasthttp.RequestHandler
in fasthttp.Server{ Handler: ... }
to accept the prescribed methods in some router.Router
routes along with routes where the method is not important to me.
from router.
So if if the method it's not important for you, the routes are important or always will you use a wild path (/path/{any:*}
)??
I ask you, because if it's true. Use fasthttp without the router is the best option. (In your case)
from router.
@savsgio The
Server
structure infasthttp
package contains theHandler
field, which does not validate methods by accepting any requests.I expect that I will not have to juggle either
router.Router{...}.Handler
orfasthttp.RequestHandler
infasthttp.Server{ Handler: ... }
to accept the prescribed methods in somerouter.Router
routes along with routes where the method is not important to me.
It's a good point for add the wild method
from router.
So if if the method it's not important for you, the routes are important or always will you use a wild path (
/path/{any:*}
)??I ask you, because if it's true. Use fasthttp without the router is the best option. (In your case)
For example, I want to use routes for Rest API and for websocket connection:
r := router.New()
r.GET("/users", getAllUsers)
r.GET("/users/{id:[0-9]+}", getSingleUser)
r.POST("/users", createUser)
r.DELETE("/users/{id:[0-9]+}", removeUser)
// NOTE: I don't care about methods, because WebSocket
r.HandleAny("/ws", handleWebSocketActions)
// or, if you just change behaviour for Handle function:
r.Handle("{method:*}", "/ws", handleWebSocketActions)
s := new(fasthttp.Server)
s.Handler = r.Handler
Why WebSocket handler use HandleAny
? Because if I use GET route for this, then I always get Switch Protocols
in logs.
from router.
Why WebSocket handler use
HandleAny
? Because if I use GET route for this, then I always getSwitch Protocols
in logs.
And why not? The websocket connection only works if method is GET, if not the returned code should be MethodNotAllowed
-> 405
from router.
And why not? The websocket connection only works if method is GET, if not the returned code should be
MethodNotAllowed
->405
Suppose I did not choose the most obvious example, but instead of WebSocket, you can take any other HTTP protocol that does not require specific methods or requires a range of them. In any case, the router
does not yet support the addition of routes without a rigid binding to methods.
from router.
Related Issues (20)
- Not removing superfluous path elements like //// HOT 5
- ServeFilesCustom responds for a long time when requesting a non existent file HOT 4
- Behavior change in 1.3.2 with double slashes when RedirectFixedPath is false HOT 4
- ServeFilesCustom calls PanicHandler if file does not exist HOT 2
- Consultation on route matching HOT 1
- Get Matched Route HOT 1
- Why this routing doesn't work? HOT 4
- Issue with catch-all parameter HOT 1
- How to integrate with Prometheus HOT 1
- Documentation for router.Lookup out of date HOT 2
- Group wide middlewares
- gotils is gpl HOT 2
- CORS allow ? HOT 1
- Path Route HOT 1
- how to serve files using embed? HOT 2
- incorrect regex match HOT 2
- path validation breaks group usage HOT 1
- panic: path must begin with '/' in path '' group.go:19 HOT 1
- Middleware for Group
- how to support path with and without trailing slash ? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from router.