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

missing Handle and HandleFunc #8

Closed
qwertmax opened this issue Nov 29, 2015 · 5 comments
Closed

missing Handle and HandleFunc #8

qwertmax opened this issue Nov 29, 2015 · 5 comments

Comments

@qwertmax
Copy link

are you going to add ?

http.Handle("/foo", fooHandler)

http.HandleFunc("/bar", func(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
})
@valyala
Copy link
Owner

valyala commented Nov 30, 2015

Requests' routing is orthogonal to http handling. It would be better if routing is implemented in separate packages. I already filed feature request for httprouter. httprouter is much better than the default routing from net/http.

As for default router from net/http, it can be substituted by a simple switch in most cases.

http.HandleFunc("/foo", fooHandler)
http.HandleFunc("/bar", barHandler)
http.HandleFunc("/baz", bazHandler)

is converted to

requestHandler := func(ctx *fasthttp.RequestHandler) {
    path := ctx.Path()
    switch string(path) {
    case "/foo":
        fooHandler(ctx)
    case "/bar":
        barHandler(ctx)
    case "/baz":
        bazHandler(ctx)
    }
}

@buaazp
Copy link
Contributor

buaazp commented Dec 16, 2015

There is a fasthttprouter forked from httprouter. Have a try. @qwertmax

@qwertmax
Copy link
Author

@buaazp thanks I'll take a look

@valyala
Copy link
Owner

valyala commented Dec 16, 2015

See also the issue #9 .

@Arnold1
Copy link

Arnold1 commented Apr 12, 2019

@valyala how can i convert r.HandleFunc("/getResponseRate", LogWrapper(HTTPErrorWrapper(dataProvider.StreamingResponse)) for fasthttp?
it seems the only way is to use github.com/buaazp/fasthttprouter. where is fasthttpserver if i use fasthttprouter? where can i define Concurrency, ReadBufferSize, MaxRequestBodySize, ReadTimeout, etc. when using fasthttprouter?

func main() {
    r := mux.NewRouter()
    var dataProvider getDataFunc
    dataProvider = data.GetData
    r.HandleFunc("/getResponse", LogWrapper(HTTPErrorWrapper(dataProvider.StreamingResponse))).Methods("POST")

    s := &http.Server{
	Addr:           ":8080",
	Handler:        r,
	ReadTimeout:    10 * time.Second,
	WriteTimeout:   10 * time.Second,
	MaxHeaderBytes: 1 << 20,
    }
    log.Fatal(s.ListenAndServe())
}

func (getDataFtor getDataFunc) StreamingResponse(w http.ResponseWriter, r *http.Request) error {
    // do some w.write() 
    return nil
}

type ErrorHandlerFunc func(w http.ResponseWriter, r *http.Request) error

func HTTPErrorWrapper(errorHandlerFunc ErrorHandlerFunc) http.HandlerFunc {
    return func(w http.ResponseWriter, r *http.Request) {
		err := errorHandlerFunc(w, r)
		if err != nil {
                    ....
                }
    }
}

func LogWrapper(f http.HandlerFunc) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "application/json")
		f(w, r)
       }
}

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

4 participants