Giter Site home page Giter Site logo

Type chi.Middleware about chi HOT 6 CLOSED

go-chi avatar go-chi commented on May 5, 2024
Type chi.Middleware

from chi.

Comments (6)

GeertJohan avatar GeertJohan commented on May 5, 2024

I'd be happy to provide a PR if necessary.

from chi.

utrack avatar utrack commented on May 5, 2024

👍

from chi.

pkieltyka avatar pkieltyka commented on May 5, 2024

Hey @GeertJohan - a shorthand would be nice, but it would only save a bit of typing.. as so..

func NewSomethingCtx(stuff *Stuff)  chi.Middleware  {
    return chi.Middleware(func(next chi.Handler) chi.Handler {
        // stuff can be used here to set something up (should be done once, as mentioned in #10).
        return chi.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
            // stuff can also be used here, on each request.
            // actual middleware.
        })
    })
}

Im not convinced thats much better. Do you see another way? if so, it would be great to see a quick PR for the discussion.

from chi.

GeertJohan avatar GeertJohan commented on May 5, 2024

I liked it for documentation and readability. However, when trying to implement a quick example I found that it gets messy real quick. Existing func(next chi.Handler) chi.Handler must still need be supported and since router.Use(..) accepts interface{} there can't be any type conversion going on during the call, which means that internally both types must be handled. It's probably not worth the effort without unless doing a breaking change.

from chi.

GeertJohan avatar GeertJohan commented on May 5, 2024

Didn't mean to close, although this probably can be closed..?

from chi.

pkieltyka avatar pkieltyka commented on May 5, 2024

I'd like to keep the middleware signature as close to the standard http.Handler middlewares. I'm going to close this ticket, but if you find an elegant approach please submit a PR. Thanks for the feedback!

from chi.

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.