An HTTP Basic Auth middleware for Go
import (
"net/http"
"github.com/yspro/httpbasicauth"
)
// credentials
creds := httpbasicauth.SimpleCredentialMap{"u$eR": "$ecret"}
middleware := httpbasicauth.Handle(creds, "Restricted Zone")
yourhandler := http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "Hello World")
},
)
http.Handle("/secret", middleware(yourhandler))
err := http.ListenAndServe(":8080", nil)
if err != nil {
panic(err)
}