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

What is the recommended way to initialize the C.Env map? #11

Closed
mfzl opened this issue Apr 26, 2014 · 5 comments
Closed

What is the recommended way to initialize the C.Env map? #11

mfzl opened this issue Apr 26, 2014 · 5 comments

Comments

@mfzl
Copy link

mfzl commented Apr 26, 2014

Is it designed to check if the Env map is nil first before assigning values in middle-ware?

@mfzl
Copy link
Author

mfzl commented Apr 26, 2014

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"])
})

@zenazn
Copy link
Owner

zenazn commented Apr 26, 2014

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.

@mfzl
Copy link
Author

mfzl commented Apr 26, 2014

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

@zenazn zenazn closed this as completed in eb08516 Apr 26, 2014
@zenazn
Copy link
Owner

zenazn commented Apr 26, 2014

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)

@mfzl
Copy link
Author

mfzl commented Apr 29, 2014

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants