Coder Social home page Coder Social logo

Add WrapHandler to the main docs about docs HOT 9 CLOSED

gobuffalo avatar gobuffalo commented on July 28, 2024 1
Add WrapHandler to the main docs

from docs.

Comments (9)

dankinder avatar dankinder commented on July 28, 2024

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.

markbates avatar markbates commented on July 28, 2024

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.

dankinder avatar dankinder commented on July 28, 2024

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.

markbates avatar markbates commented on July 28, 2024

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.

dankinder avatar dankinder commented on July 28, 2024

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.

markbates avatar markbates commented on July 28, 2024

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.

dankinder avatar dankinder commented on July 28, 2024

Right on, thanks!

from docs.

dreampuf avatar dreampuf commented on July 28, 2024

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.

UberMeatShield avatar UberMeatShield commented on July 28, 2024

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)

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.