Combining net/http and fasthttp

时光总嘲笑我的痴心妄想 提交于 2020-01-17 15:00:20

问题


I was looking for a fast framework for go and I stumbled upon fasthttp https://github.com/valyala/fasthttp which is according to the developer and the benchmark is 10x faster than Golang net/http package. I'm already familiar with gorilla toolkit and other net/http based frameworks like gin-gonic, goji, and gocraft.

My question is: Is it possible to mix net/http framework / toolkits with fasthttp for example I would like to use some gorilla packages with echo / iris (fasthttp frameworks)?


回答1:


I was checking the Iris framework and I saw in the documentation that it's possible use net/http with the framework.

https://kataras.gitbooks.io/iris/content/using-native-httphandler.html

The framework is using the following to convert native net/http handler to fasthttp handler.

https://github.com/valyala/fasthttp/tree/master/fasthttpadaptor




回答2:


I think that replace from a scratch file will be the best solution. Here an example that show differences between default net/http and fasthttp

Example for setup net/http default package. Basic not efficient approach

/* Global var */
var logcfg = initConfigurationData("test/")
var fileList = initLogFileData(logcfg)

func main() {
    channel := make(chan bool)
    /* Run coreEngine in background, parameter not necessary */
    go coreEngine(fileList, logcfg, channel)
    log.Println("Started!")
    handleRequests()
    <-channel
    log.Println("Finished!")
}

After, you can declare a 'wrapper' that contains all of your 'API' like the following 'handleRequest' method

func handleRequests() {
    // Map the endoint to the function
    http.HandleFunc("/", homePage)
    http.HandleFunc("/listAllFile", listAllFilesHTTP)
    http.HandleFunc("/getFile", getFileHTTP)
    log.Fatal(http.ListenAndServe(":8081", nil))
}

Here is the list the API, each one bind as the first argument of the HandleFunc and managed with the core functionalities provided by the method (example):

// Example of function for print a list of file
func listAllFilesHTTP(w http.ResponseWriter, r *http.Request) {
    tmp := make([]string, len(fileList))
    for i := 0; i < len(fileList); i++ {
        tmp[i] = fileList[i].name
    }
    fmt.Fprintf(w, strings.Join(tmp, "\n"))
    log.Println("Endpoint Hit: listFile")
}

From the other side, with fasthttp, you have to change your handleRequest method as following

func handleRequests() {
    m := func(ctx *fasthttp.RequestCtx) {
        switch string(ctx.Path()) {
        case "/":
            fastHomePage(ctx)
        case "/listAllFile":
            fastListAllFilesHTTP(ctx)
        case "/getFile":
            fastGetFileHTTP(ctx)
        default:
            ctx.Error("not found", fasthttp.StatusNotFound)
        }
    }
    fasthttp.ListenAndServe(":8081", m)
}

Than, the core function will be the following:
NOTE: You can pass other variable too other from the context to the method.

// NOTE: The signature have changed!
func fastListAllFilesHTTP(ctx *fasthttp.RequestCtx) {
    ctx.Response.Header.Set("GoLog-Viewer", "v0.1$/alpha")
    tmp := make([]string, len(fileList))
    for i := 0; i < len(fileList); i++ {
        tmp[i] = fileList[i].name
    }
    fmt.Fprintf(ctx, strings.Join(tmp, "\n"))
    log.Println("Endpoint Hit: listFile")
}


来源:https://stackoverflow.com/questions/38094739/combining-net-http-and-fasthttp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!