请在下面查看我的代码。如您所见,它是具有两个端点的简单服务:
现在,我也尝试将基本身份验证中间件模式也添加到/static端点,但是由于某些原因,我不知道如何操作。我无法将
*route
(r.PathPrefix的结果)转换为middleware()
函数可以理解的内容。 (我还创建了一个playground,但是由于外部导入,这将不起作用)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)
}
}
最佳答案
好吧,事实证明,我使用的是mux的古老版本,缺少use()
函数:)。
关于go - Gorilla Mux,静态文件服务器的中间件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50863974/