feat: new (optional) index file list for file system serve

This commit is contained in:
Martin Riedl 2024-07-02 01:14:17 +02:00
parent aa0a11f3c5
commit a6d67cf60a
Signed by: martinr92
GPG key ID: FB68DA65516A804C
2 changed files with 35 additions and 5 deletions

19
fs.go
View file

@ -33,6 +33,7 @@ type FS struct {
UseLocalFolder bool UseLocalFolder bool
LocalFolderPrefix string LocalFolderPrefix string
HeaderHandler HeaderHandler HeaderHandler HeaderHandler
IndexFilenames []string
} }
// NewFS creates a new instance of the http file system used for serving static files. // NewFS creates a new instance of the http file system used for serving static files.
@ -51,13 +52,13 @@ func (fs *FS) ServeHTTP(w http.ResponseWriter, r *http.Request, info RoutingInfo
} }
// search for file in static file system // search for file in static file system
file, filePath, err := fs.findFile(url) file, err := fs.findFile(url)
if err != nil { if err != nil {
http.NotFound(w, r) http.NotFound(w, r)
return return
} }
// check, if the file is a folder // check, if the file is a folder
file, ok := fs.checkFolder(file, filePath) file, ok := fs.checkFolder(file, url)
if !ok { if !ok {
http.NotFound(w, r) http.NotFound(w, r)
return return
@ -68,7 +69,7 @@ func (fs *FS) ServeHTTP(w http.ResponseWriter, r *http.Request, info RoutingInfo
fs.serve(w, r, info, file) fs.serve(w, r, info, file)
} }
func (fs *FS) findFile(url string) (fs.File, string, error) { func (fs *FS) findFile(url string) (fs.File, error) {
// build file path // build file path
filePath := path.Join(fs.FolderPrefix, url) filePath := path.Join(fs.FolderPrefix, url)
filePath = strings.TrimPrefix(filePath, "/") filePath = strings.TrimPrefix(filePath, "/")
@ -83,7 +84,7 @@ func (fs *FS) findFile(url string) (fs.File, string, error) {
file, err := os.Open(localFilePath) file, err := os.Open(localFilePath)
if err == nil { if err == nil {
return file, localFilePath, nil return file, nil
} }
} }
@ -92,7 +93,7 @@ func (fs *FS) findFile(url string) (fs.File, string, error) {
filePath = "." filePath = "."
} }
file, err := fs.StaticFiles.Open(filePath) file, err := fs.StaticFiles.Open(filePath)
return file, filePath, err return file, err
} }
func (fs *FS) checkFolder(file fs.File, url string) (fs.File, bool) { func (fs *FS) checkFolder(file fs.File, url string) (fs.File, bool) {
@ -107,6 +108,14 @@ func (fs *FS) checkFolder(file fs.File, url string) (fs.File, bool) {
// close folder handler (we don't need it anymore) // close folder handler (we don't need it anymore)
_ = file.Close() _ = file.Close()
// check for index files
for _, indexFileName := range fs.IndexFilenames {
newFullPath := path.Join(url, indexFileName)
if newFile, err := fs.findFile(newFullPath); err == nil {
return newFile, true
}
}
return file, false return file, false
} }

View file

@ -156,6 +156,27 @@ func TestFSFolderLocal(t *testing.T) {
} }
} }
func TestFSFolderIndex(t *testing.T) {
// create new FS
fs := NewFS(&staticFiles)
fs.IndexFilenames = []string{"README.md"}
// serve the request
tw := &testWriter{}
tr := &http.Request{
Method: http.MethodHead,
URL: &url.URL{
Path: "/",
},
}
fs.ServeHTTP(tw, tr, RoutingInfo{})
// check data
if tw.statusCode != http.StatusOK {
t.Error("received invalid http status code", tw.statusCode)
}
}
func TestFSNotFound(t *testing.T) { func TestFSNotFound(t *testing.T) {
// create new FS // create new FS
fs := NewFS(&staticFiles) fs := NewFS(&staticFiles)