Comments (9)
Actually misspoke here. Though WrapHandler is useful I was thinking of middleware, not handlers. Would be great if there were a good example of wrapping standard middleware to make it work with buffalo.
from docs.
Can you elaborate on what you mean by "standard middleware"? There is no concept of middleware in the standard library, only third party implementations.
from docs.
Yeah for sure. You're right that there is no standard middleware interface definition, but this pattern is quite common:
func myMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// do some stuff ...
next.ServeHTTP(w, r)
})
}
An example library that provides middleware with this pattern, that we used: https://github.com/NYTimes/gziphandler
I guess this is a question: is there an easy way to nab a middleware library like this and use it in buffalo?
from docs.
It is possible, it would look something like this:
app.Use(func(next buffalo.Handler) buffalo.Handler {
return func(c buffalo.Context) error {
return buffalo.WrapHandler(myMiddleware(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
next(c)
})))(c)
}
})
Not pretty, however, that could easily be made into a function that did all that. You feel like getting your hands dirty with a PR? :)
from docs.
Hm, that won't work will it?
For example middleware like NYTimes/gziphandler (https://github.com/NYTimes/gziphandler/blob/master/gzip.go#L240) works by returning a new ResponseWriter wrapping the provided one with its own. I think what you posted will cause the new wrapped ResponseWrite not to be used since the buffalo context will still the ResponseWriter that was passed in with it.
It may be possible to work around this flaw, if I'm right. May send a PR when I have time for one, especially if I'm putting it to use!
from docs.
This can now be done in v0.9.4
, https://blog.gobuffalo.io/buffalo-v0-9-4-released-5d2327a4742e, so I'm closing this issue. Feel free and re-open if you have problems.
from docs.
Right on, thanks!
from docs.
I'm not sure that have you test it out. But I found that even PreWare
can't play with the responsewriter replacing. It's just another middleware between http.Handler
and buffalo.Context
.
There is no way to replace the responsewriter, so you only can do the gzip writer in buffalo.Handler
.
from docs.
Posting here in case it might some day help somebody, one potentially abusive but seemingly working option is that you can just use the Response and Writer from the c buffalo.Context directly.
// Initialized like this
app.GET("/view/{dir_id}/{file_id}", ViewHandler)
// Used in a semi abusive fashion but lets you serve a file using the standard Response, Request format many libraries demo
func ViewHandler(c buffalo.Context) error {
dir_to_list := c.Param("dir_id")
file_id := c.Param("file_id")
fq_name = SomeAbsPathLookup(dir_id, file_id)
if fq_name != nil {
http.ServeFile(c.Response(), c.Request(), fq_name) // from net/http
return nil // I guess this compiles and doesn't complain in the logs!
} else {
return c.Render(404, r.JSON(invalidDirMsg(dir_to_list, file_id)))
}
}
from docs.
Related Issues (20)
- `buffalo new coke` fails HOT 1
- Incorrect URL for crdb in configurations
- Improve examples with PUT, POST and more
- I don't know how to generate a model fields with default value in doc.
- improvement by adding date on release notes page
- Update tables when table name is dynamic
- No z-index on documentation HOT 1
- Local models not found in initial buffalo-auth generation HOT 1
- brew install gobuffalo/tap/pop
- Cant install SODA CLI on Linux HOT 1
- Some elements activate mailer on the docs sidebar HOT 3
- Plush variables not render correctly HOT 1
- could it be possible we make this path transition seamlessly? HOT 4
- Docs: Events page is 404-ing HOT 6
- Incorrect link for install Buffalo CLI HOT 3
- Deployment documentation misses important new "buffalo build" options HOT 1
- Buffalo Plugins documentation is outdated HOT 1
- buffalo dev fails to run a newly generated project HOT 1
- feature: no where on doc mention how to insert.
- bug: Fix models uuid documentation 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 docs.