Giter Site home page Giter Site logo

http.Handler vs http.HandlerFunc about chi HOT 7 CLOSED

go-chi avatar go-chi commented on May 5, 2024 2
http.Handler vs http.HandlerFunc

from chi.

Comments (7)

nubunto avatar nubunto commented on May 5, 2024 6

Since http.HandlerFunc implements http.Handler, shouldn't all the method functions take an http.Handler instead? This is probably a stretch WRT compatibility, but it would make more sense to structure chi routers around interfaces, rather than around functions.

from chi.

pkieltyka avatar pkieltyka commented on May 5, 2024 1

@didip yea, I'd rather not do that.. I want to keep Router interface simpler/smaller.

from chi.

mrcpvn avatar mrcpvn commented on May 5, 2024

Hi,
as a workaround you can set up the route like this:

chi.NewRouter().Get(http.HandlerFunc(yourHandler.ServeHTTP))

from chi.

pkieltyka avatar pkieltyka commented on May 5, 2024

hey @didip - yea you can do:chi.NewRouter().Get(yourHander.ServeHTTP)

from chi.

didip avatar didip commented on May 5, 2024

Oh, TIL, they can be casted back and forth. This works too: handler.(http.HandlerFunc).

Thanks for taking the time!

But that said (I am just opening a discussion here), what do you guys think about having GetFunc and Get as new API? It seems cleaner.

from chi.

dvelitchkov avatar dvelitchkov commented on May 5, 2024

This seems like a giant mistake - to tie the methods' first parameter to http.HandlerFunc and not http.Handler. You want the parameter to be an interface type, not a specific implementation of that interface. Same reason why fmt.Fprint* take an io.Writer, not os.Stdout. Any chance this can be fixed in v5? v6?

from chi.

pkieltyka avatar pkieltyka commented on May 5, 2024

@dvelitchkov I hear you, but the common case is to attach a func SomeHandler(w http.ResponseWriter, r *http.Request) { .. } method to an endpoint, if I change the parameter to a http.Handler it will require everyone to write, r.Get("/path", http.HandlerFunc(SomeHandler)) which I don't think is better. It's been decided for syntactic sugar reasons this is the more common case.

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.