0

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())
   }
Clarko
  • 103
  • 5

1 Answers1

0

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

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
  • seems like that fixed the 404 error, I also had to change the "/Login/" prefix to "/" – Clarko Apr 21 '17 at 18:13
  • If you don't like absolute paths, use `currentUser.HomeDir` to get the home path. See more at: https://golang.org/pkg/os/user/#User – lowatt Apr 22 '17 at 00:04