fasthttp with gorilla/mux

Created on 10 Feb 2017  路  4Comments  路  Source: valyala/fasthttp

Is it possible to implement these two together? Or is there any router compatible with fasthttp that let me have subrouters?

I need to have a versioned API (for example mydomain.com/v1/ and mydomain.com/v2/) but I also want to have the improvement in performance that fasthttp gives.

All 4 comments

No need to advertise iris here it's already popular, I think this is what you're searching for.

Give a try to iris with fasthttp support.
It covers your needs for routes group too.

go get -u gopkg.in/kataras/iris.v5

its docs: https://docs-v5.iris-go.com/party.html

@GeekyPanda thanks for your answer but I'm trying to avoid using iris (I've read some ugly things about it, but anyways I might give it a go). I'll keep this issue open in case someone has other suggestions

We are the ones who choose what to believe and what not. For this reason I am not going to judge your preordained choice. The freedom to form an opinion based on those you can read on the internet is without question. I just wanted to help, good luck in whatever you select at the end!

@facundomedica have you read README? It mentions fasthttp-routing alongside with other routers.
I haven't used it myself, but it seems like it supports route groups.
Another thing you might want to look at is echo. It used to have integration with fasthttp, but they dropped it in v3. You can use v2 though. echo also supports route groups.
Last but not least, you CAN integrate gorilla/mux and fasthttp like so:

package main

import (
    "net/http"

    "github.com/gorilla/mux"
    "github.com/valyala/fasthttp"
    "github.com/valyala/fasthttp/fasthttpadaptor"
)

func main() {
    r := mux.NewRouter()
    r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        w.WriteHeader(http.StatusOK)
        w.Header().Set("Content-Type", "text/plain; charset=utf-8")
        w.Write([]byte("root"))
    })
    r.HandleFunc("/route-a", func(w http.ResponseWriter, r *http.Request) {
        w.WriteHeader(http.StatusOK)
        w.Header().Set("Content-Type", "text/plain; charset=utf-8")
        w.Write([]byte("route-a"))
    })
    r.HandleFunc("/route-b", func(w http.ResponseWriter, r *http.Request) {
        w.WriteHeader(http.StatusOK)
        w.Header().Set("Content-Type", "text/plain; charset=utf-8")
        w.Write([]byte("route-b"))
    })
    fasthttp.ListenAndServe("localhost:8080", fasthttpadaptor.NewFastHTTPHandler(r))
}

But you probably gonna get performance similar to that of a net/http + gorilla/mux combo. It's still pretty good, but don't take my word for it. See and test for yourself.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

unisqu picture unisqu  路  4Comments

kirillDanshin picture kirillDanshin  路  5Comments

LeMoussel picture LeMoussel  路  3Comments

horgh picture horgh  路  5Comments

shkreios picture shkreios  路  3Comments