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:
34
utils/filesystem.go
Normal file
34
utils/filesystem.go
Normal file
@ -0,0 +1,34 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// https://www.alexedwards.net/blog/disable-http-fileserver-directory-listings
|
||||
|
||||
type NeuteredFileSystem struct {
|
||||
Fs http.FileSystem
|
||||
}
|
||||
|
||||
func (nfs NeuteredFileSystem) Open(path string) (http.File, error) {
|
||||
f, err := nfs.Fs.Open(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
s, err := f.Stat()
|
||||
if s.IsDir() {
|
||||
index := filepath.Join(path, "index.html")
|
||||
if _, err := nfs.Fs.Open(index); err != nil {
|
||||
closeErr := f.Close()
|
||||
if closeErr != nil {
|
||||
return nil, closeErr
|
||||
}
|
||||
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return f, nil
|
||||
}
|
Reference in New Issue
Block a user