Please see my code below. As you can see it's a simple service with two endpoints:
- /test <-- basic auth protected using middleware
- /static <-- serves all the files in the ./static directory
Now I'm trying to add the basic authentication middleware pattern to the /static endpoint as well, but for some reason I can't figure out how. I'm having trouble converting the *route
(the outcome of r.PathPrefix) to something the middleware()
function understands. (I've also created a playground, but due to the external import, this won't work)
package main
import (
"encoding/base64"
"log"
"net/http"
"strings"
"github.com/gorilla/mux"
)
const (
username = "test"
password = "test"
)
func main() {
r := mux.NewRouter()
// add normal endpoint
r.HandleFunc("/test", middleWare(myHandler, basicAuth))
// add static
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("./static"))))
srv := &http.Server{
Handler: r,
Addr: "0.0.0.0:8080",
}
log.Print("listening on 0.0.0.0:8080")
log.Fatal(srv.ListenAndServe())
}
func myHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("yes!"))
return
}
func middleWare(h http.HandlerFunc, middleware ...func(http.HandlerFunc) http.HandlerFunc) http.HandlerFunc {
for _, m := range middleware {
h = m(h)
}
return h
}
func basicAuth(h http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
s := strings.SplitN(r.Header.Get("Authorization"), " ", 2)
if len(s) != 2 {
http.Error(w, "Not authorized", 401)
return
}
b, err := base64.StdEncoding.DecodeString(s[1])
if err != nil {
http.Error(w, err.Error(), 401)
return
}
pair := strings.SplitN(string(b), ":", 2)
if len(pair) != 2 {
http.Error(w, "Not authorized", 401)
return
}
if pair[0] != username || pair[1] != password {
http.Error(w, "Not authorized", 401)
return
}
h.ServeHTTP(w, r)
}
}