Giter VIP home page Giter VIP logo

Comments (3)

savsgio avatar savsgio commented on June 3, 2024

Hi @Manghud,

These feature is easy to implement, wrapping your handler.

So, some frameworks give you this functionality on easy way like Atreugo, Gramework and others.

Thanks!

from router.

 avatar commented on June 3, 2024

@savsgio I'm not sure this is possible to implement by wrapping the handler if you want to add middleware before the routes but after the router's built-in redirection/HandleMethodNotAllowed middleware. For example, if I wrap the handler like this:

r := router.New()
r.RedirectTrailingSlash = true
r.RedirectFixedPath = true
r.HandleMethodNotAllowed = true

r.GET("/hello", helloHandler)

fasthttp.ListenAndServe(":8080", func(ctx *fasthttp.RequestContext) {
    // my middleware here
    r.Handler(ctx)
})

The order of execution for a request to /hello is this:

  1. my middleware wrapper function
  2. router's RedirectTrailingSlash, RedirectFixedPath, and HandleMethodNotAllowed logic
  3. helloHandler

What I would like is the ability to run them in this order:

  1. router's RedirectTrailingSlash, RedirectFixedPath, and HandleMethodNotAllowed logic
  2. my middleware wrapper function
  3. helloHandler

If I understand correctly, this is only possible with a Use method or something similar, like @Manghud described, right? This is important to me because my middleware in this use case that runs on every route needs to hit the database (load a session, for example). I do not want to hit the database for simple redirects and method-not-allowed responses.

Additionally, without a Use method on Groups, I don't think it is possible to apply middleware specifically to a group.

from router.

savsgio avatar savsgio commented on June 3, 2024

Hi @winduptoy,

Sorry for delayed answer.

If you want thats behaviour, you must wrap the helloHandler and not execute the middleware before the r.Handler.

So if you need a simple API to use middlewares, you could use some of frameworks built on top of fasthttp.

from router.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.