Giter VIP home page Giter VIP logo

Comments (5)

valyala avatar valyala commented on August 29, 2024

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

from fasthttp.

buaazp avatar buaazp commented on August 29, 2024

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

from fasthttp.

qwertmax avatar qwertmax commented on August 29, 2024

@buaazp thanks I'll take a look

from fasthttp.

valyala avatar valyala commented on August 29, 2024

See also the issue #9 .

from fasthttp.

Arnold1 avatar Arnold1 commented on August 29, 2024

@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)
       }
}

from fasthttp.

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.