问题
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