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