Skip to content

Instantly share code, notes, and snippets.

@benzerbett
Created December 2, 2022 11:05
Show Gist options
  • Select an option

  • Save benzerbett/31b850e6cd90b1d0ecb54f643c3c0ac7 to your computer and use it in GitHub Desktop.

Select an option

Save benzerbett/31b850e6cd90b1d0ecb54f643c3c0ac7 to your computer and use it in GitHub Desktop.
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))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment