buaazp / fasthttprouter

A high performance fasthttp request router that scales well
http://godoc.org/github.com/buaazp/fasthttprouter
BSD 3-Clause "New" or "Revised" License
872 stars 91 forks source link

Parameters as request instead of endpoint url #48

Open xleyba opened 5 years ago

xleyba commented 5 years ago

Hi

I have followed the examples and defined a router like this:

router.GET("/customer/account/detail/:accountId", myHandler.customerAccountDetailHandler)

And call to my service as http://locahost:9296/customer/account/detail/2

But I realised that I do not want to have the parameters as part of the endpoint , I rather prefer to use normal parameters by calling my service like this:

http://locahost:9296/customer/account/detail?accountId=2

Is it possible to be done with fasthttprouter? How?

Thanks in advance J

savsgio commented 5 years ago

Hi @xleyba,

To get value of query params in fasthttp (independently of router you are using) , use ctx.URI().QueryArgs().Peek("accountId").

savsgio commented 5 years ago

it's not necessary to add any extra functionalty to this router

xleyba commented 5 years ago

Hi Sergio

Thanks for your answer but I am affraid it is not working. I mean, if I define my route as router.GET("/customer/account/detail", myHandler.customerAccountDetailHandler)

and the try to call my service like:

http://locahost:9296/customer/account/detail?accountId=2

What a I get is a "not found" message. I assume due to the router is not recognising such route. Am I wrong?

savsgio commented 5 years ago

Yes, the router does not recognize the route. Could you share a bit part of your code, please!

xleyba commented 5 years ago

Hi

In the main func I have this:

// Used to pass a DB as a parameter
    myHandler := &MyHandler{
        myDB: db,
    }

    // Set a router mux
    router := fasthttprouter.New()
    router.GET("/customer/account/movements",
        myHandler.customerAccountMovementsHandler)

    // Use reuse port tool
    ln, err := reuseport.Listen("tcp4", viper.GetString("port"))
    if err != nil {
        log.Fatal().Msgf("error in reuseport listener: %s", err)
    }

    // Defining server
    srv := &fasthttp.Server{
        // https://stackoverflow.com/questions/29334407/creating-an-idle-timeout-in-go
        //WriteTimeout:                     time.Second * 60,
        ReadTimeout:                        time.Second * 1,
        //IdleTimeout:                      time.Second * 120,
        SleepWhenConcurrencyLimitsExceeded: time.Second * 5,
        Handler:                            router.Handler,
    }

    // Lanuch server in a thread
    go func() {
        fmt.Println("Starting server...")
        log.Info().Msg("Starting server...")
        if err := srv.Serve(ln); err != nil {
            log.Panic().Msgf("%s", err)
        }
    }()

And in my handler I have this:


// Retrieves all the customer account movements
func (h *MyHandler) customerAccountMovementsHandler(ctx *fasthttp.RequestCtx) {

    fmt.Printf("AccountId: %s, Sort: %s, Asc: %s",
        ctx.UserValue("accountId"),
        ctx.UserValue("sort"),
        ctx.UserValue("asc"))
}

Some people recommended me to fix it by stop using fasthttprouter anduse the default fasthttp handlers instead but I would like to be sure that it is imposible to fix it with the router...

Thanks in advance J

savsgio commented 5 years ago

I recommend you to use this router https://github.com/fasthttp/router, it's maintained by fasthttp team.

Try with it, and let my know if works.

savsgio commented 5 years ago

And modify your handler like this:


// Retrieves all the customer account movements
func (h *MyHandler) customerAccountMovementsHandler(ctx *fasthttp.RequestCtx) {
        args := ctx.URI().QueryArgs()
    fmt.Printf("AccountId: %s, Sort: %s, Asc: %s",
        args.Peek("accountId"),
        args.Peek("sort"),
        args.Peek("asc"))
}```
xleyba commented 5 years ago

Hi

Yes, it worked fine. Thanks! Should I expect a worst performance by using that instead of fasthttprouter?

J

savsgio commented 5 years ago

Nice!

Don't worry about performance.

The router has a improvements in performance respect to fasthttprouter.

You could see the benchmarks images in Readme.

xleyba commented 5 years ago

Ok, thanks a lot! :)