1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

feat: serve swagger ui

fix: forbid to browse file system index
This commit is contained in:
Ferdinand Mütsch
2021-02-07 12:28:42 +01:00
parent 8fc39f23fa
commit bb1d6c048d
5 changed files with 462 additions and 347 deletions

32
middlewares/filetype.go Normal file
View File

@@ -0,0 +1,32 @@
package middlewares
import (
"net/http"
"strings"
)
type SuffixFilterMiddleware struct {
handler http.Handler
filterTypes []string
}
func NewFileTypeFilterMiddleware(filter []string) func(http.Handler) http.Handler {
return func(h http.Handler) http.Handler {
return &SuffixFilterMiddleware{
handler: h,
filterTypes: filter,
}
}
}
func (f *SuffixFilterMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
path := strings.ToLower(r.URL.Path)
for _, t := range f.filterTypes {
if strings.HasSuffix(path, strings.ToLower(t)) {
w.WriteHeader(http.StatusForbidden)
w.Write([]byte("403 forbidden"))
return
}
}
f.handler.ServeHTTP(w, r)
}