gofasthttp

Serving multiple directories is not working


Is there any way to serve multiple directory using fasthttp framework? I wrote the below code for the same purpose. But, this code is not working as I expected. When I access localhost:8080/path1, It throws error and warnings,

Cannot open requested path

2017/10/13 16:57:01 0.977 #0000000100000001 - 127.0.0.1:8080<->127.0.0.1:48870 - GET http://localhost:8080/path1 - cannot open file "/home/test/path1": open /home/test/path1/path1: no such file or directory

I don't know how this url(/home/test/path1) redirects to (/home/test/path1/path1). What is wrong with the below code ?

requestHandler := func(ctx *fasthttp.RequestCtx) {
        var fs fasthttp.FS
        switch string(ctx.Path()) {
        case "/path1":
            fs = fasthttp.FS{
                Root:       "/home/test/path1",
                IndexNames: []string{"index.html"},
            }
        case "/path2":
            fs = fasthttp.FS{
                Root:       "/home/test/path2",
                IndexNames: []string{"index.html"},
            }
        }
        fsHandler := fs.NewRequestHandler()
        fsHandler(ctx)
    }

    if err := fasthttp.ListenAndServe(":8080", requestHandler); err != nil {
        fmt.Println("error in ListenAndServe: %s", err)
    }

Solution

  • Nothing is wrong, works exactly as you wrote it: Root of webserver: /home/test/path1. You request http://bla/path1. This in turn translates into: http://bla/ -> /home/path/path1/index.html. http://bla/path1 -> /home/path/path1/path1/index.html.

    Ow yeah, in case of serving 2 directories - yes you can, just as any other normal HTTP server would, they just need to have the same Root. Otherwise look into virtual hosts support.