Git Product home page Git Product logo

Comments (5)

mfzl avatar mfzl commented on July 28, 2024

Just for an example, something like below fails, because Env is not initialized. Is this not how it was intended to use?

r := web.New()
r.Use(func(c *web.C, h http.Handler) http.Handler {
    c.Env["test"] = r.URL.Path
    return handler
})

r.Get("/", func(c web.C, w http.ResponseWriter, req *http.Request) {
    log.Println(c.Env["test"])
})

from goji.

zenazn avatar zenazn commented on July 28, 2024

Yeah, the current behavior is by design—you're supposed to check c.Env for nil before using it. It was originally a (misguided?) attempt to avoid the extra map allocation if it's not needed.

I think the right place to enforce that c.Env is non-nil is probably as a middleware at the top of the stack. I'd be happy to provide a reference implementation of this in goji's middleware stdlib if you like.

from goji.

mfzl avatar mfzl commented on July 28, 2024

I think explicit initialization goes well with the philosophy of the framework. Though I think it's worth mentioning it somewhere and have an example demonstrating the case.

👍 for middleware.

Thanks

from goji.

zenazn avatar zenazn commented on July 28, 2024

I pushed a standard middleware for this, along with a bit of documentation about the behavior. I probably owe you a bit more documentation than that, but that's going to go on the back burner for now (it probably belongs in a "how do you even middleware?" section of some document)

from goji.

mfzl avatar mfzl commented on July 28, 2024

After checking the middleware package I now see why I got it wrong in the first place. Anyway thanks for the EnvInit middleware.

from goji.

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.