Hi I am trying to create a server in Go Lang which serves files and HTTP Requests at the same time.
I want /upload path to accept post requests and
/files path to serve static files in the fpath
I tried with the following code but i get a 404 error
func main() {
fpath, _ := filepath.Abs(filepath.Dir(os.Args[0]))
fpath+="/public"
fmt.Println(fpath)
http.HandleFunc("/upload",uploadFunc)
http.HandleFunc("/files",http.FileServer(http.Dir(fpath)))
panic(http.ListenAndServe(":8080", nil))
}
http.FileServeris trying to serve routes like "/files/foo" (and thus looking for "./public/files/foo") instead of stripping the "/files" prefix off. I tried this myself and it still didn't work, so there's probably something else wrong, but you'll definitely need to do that anyway.http.Handle("/files/", http.StripPrefix("/files/", http.FileServer(http.Dir(fpath))))instead ofHandleFunc(). Please note the trailing slashes on"/files/".