This serve all files inside the folder flag, as well as serving index.html at the root.
Usage
//port default values is 8500
//folder defaults to the current directory
go run main.go
//your case, dont forget the last slash
go run main.go -folder static/
//dont
go run main.go -folder ./
Code
package main
import (
"flag"
"fmt"
"net/http"
"os"
"strconv"
"strings"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/kr/fs"
)
func main() {
mux := mux.NewRouter()
var port int
var folder string
flag.IntVar(&port, "port", 8500, "help message for port")
flag.StringVar(&folder, "folder", "", "help message for folder")
flag.Parse()
walker := fs.Walk("./" + folder)
for walker.Step() {
var www string
if err := walker.Err(); err != nil {
fmt.Fprintln(os.Stderr, "eroooooo")
continue
}
www = walker.Path()
if info, err := os.Stat(www); err == nil && !info.IsDir() {
mux.HandleFunc("/"+strings.Replace(www, folder, "", -1), func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, www)
})
}
}
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, folder+"index.html")
})
http.ListenAndServe(":"+strconv.Itoa(port), handlers.LoggingHandler(os.Stdout, mux))
}