Created
December 2, 2022 11:05
-
-
Save benzerbett/31b850e6cd90b1d0ecb54f643c3c0ac7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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