Search code examples
web-servicesgogorillamux

404 Error while using gorilla/mux golang library


I am fairly new to golang and am finding myself frustrated with a simple file service program. I am suspecting that there is something wrong with my file prefix/ directory in the handler for my router r. I have tried many different formats for the directory. the html file i would like serviced is $HOME/Documents/TEST/Login on my system. Below is my code, note the {address} replaces the ip address.

    package main

    import (
       "log"
       "github.com/gorilla/mux"
       "net/http"
       "time"
     )

   func main() {
   r := mux.NewRouter()
   r.PathPrefix("/Login/").Handler(http.StripPrefix("/Login/", 
   http.FileServer(http.Dir("$HOME/Documents/TEST/Login"))))

   srv := &http.Server{
       Handler:      r,
       Addr:         "{address}:9999",
       WriteTimeout: 600 * time.Second,
       ReadTimeout:  600 * time.Second,
   }

   log.Fatal(srv.ListenAndServe())
   }

Solution

  • Go won't interpret $HOME. Use an explicit path such as /home/username/Documents/TEST/Login/.