package main import ( "flag" "fmt" "log" "net/http" "os" "strings" ) var port = flag.Int("port", 8000, "port from which to serve") var hidden = flag.Bool("hidden", false, "allow serving hidden dirs") func logger(h http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Add("Access-Control-Allow-Origin", "*") log.Printf("%s: %s\n", r.RemoteAddr, r.URL) if !*hidden && strings.Contains(r.URL.Path, "/.") { http.Error(w, "hidden files and directories are not allowed", http.StatusUnauthorized) return } h.ServeHTTP(w, r) }) } func main() { flag.Parse() hostname, err := os.Hostname() if err != nil { log.Fatal("problem getting hostname:", err) } log.Printf("serving on: http://%s:%d/", hostname, *port) addr := fmt.Sprintf(":%d", *port) fh := http.FileServer(http.Dir("./")) if err := http.ListenAndServe(addr, logger(fh)); err != nil { log.Fatal(err) } }