I have a Golang
Shell with a FileServer function that I want to be able to change the src directory.
Here is what I have:
func Server() {
wdir, _ := os.Getwd()
m := http.NewServeMux()
fs := http.FileServer(http.Dir(wdir))
m.Handle("/", http.StripPrefix("/", fs))
s := http.Server{Addr: ":8000", Handler: m}
m.HandleFunc("/shutdown", func(w http.ResponseWriter, r *http.Request) {
s.Shutdown(context.Background())
})
if err := s.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatal(err)
}
log.Printf("Finished")
}
I have it this way so that if I get to shutdown
, the FileServer closes.
But no matter from what path I launch Server()
, the path remains always the same.
So if from the shell I cd
into another folder and launch Server()
, I will still get the files from the directory where the executable is.
What I want is that if I run the shell from /home/user/Downloads/shell
and inside the shell I cd
into /opt
, the files to show in the server are the ones in /opt
, not the ones in /home/user/Downloads/shell