A collection of useful Go HTTP middleware functions.
go get github.com/mwalto7/httpware
Need HTTP Basic Auth for your routes? Simply wrap your handlers with httpware.BasicAuth
:
package main
import (
"fmt"
"net/http"
"github.com/mwalto7/httpware"
)
func main() {
authenticate := httpware.BasicAuth(httpware.BasicAuthOptions{
Realm: "My super secure path.",
AuthFunc: func(username, password string, _ *http.Request) bool {
// Don't do this in production.
return username == "user" && password == "pass"
},
})
mux := http.NewServeMux()
mux.Handle("/foo", authenticate(handleFoo()))
_ = http.ListenAndServe(":8080", mux)
}
func handleFoo() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
_, _ = fmt.Fprintln(w, "You're authenticated 🙂")
}
}