package main import ( "flag" "log" "net/http" ) var ( username = "mtumizi" password = "nen0gumu" ) func main() { port := flag.String("p", "8100", "port to serve on") directory := flag.String("d", ".", "the directory of static file to host") flag.Parse() // basic auth http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { user, pass, ok := r.BasicAuth() if !ok || user != username || pass != password { w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`) http.Error(w, "Unauthorized.", 401) return } http.FileServer(http.Dir(*directory)).ServeHTTP(w, r) }) log.Printf("Serving %s on HTTP port: %s\n", *directory, *port) log.Fatal(http.ListenAndServe(":"+*port, nil)) }