diff --git a/local_file.go b/local_file.go index a3028cb..d092602 100644 --- a/local_file.go +++ b/local_file.go @@ -11,12 +11,36 @@ import ( const INDEX = "index.html" +// Named boolean values for the indexes parameter of LocalFile, so calls read +// clearly at the call site instead of using a bare true/false. For example: +// +// router.Use(static.Serve("/", static.LocalFile("/www", static.NoListDirectory))) +const ( + // ListDirectory enables directory listing: requesting a directory returns + // a listing of the files it contains. + ListDirectory = true + // NoListDirectory disables directory listing: requesting a directory is + // served only when it contains an index.html file (which is then returned). + NoListDirectory = false +) + type localFileSystem struct { http.FileSystem root string indexes bool } +// LocalFile serves files from the local directory rooted at root. +// +// The indexes parameter controls how directories are handled: +// - when true (ListDirectory), directory listing is enabled and requesting a +// directory returns a listing of its files; +// - when false (NoListDirectory), directory listing is disabled and a +// directory is served only when it contains an index.html file, which is +// then returned. +// +// Note that an existing index.html is served in both cases; indexes only +// toggles the automatic directory listing. func LocalFile(root string, indexes bool) *localFileSystem { return &localFileSystem{ FileSystem: gin.Dir(root, indexes), diff --git a/local_file_test.go b/local_file_test.go index ef4fcf8..457e0d0 100644 --- a/local_file_test.go +++ b/local_file_test.go @@ -34,3 +34,47 @@ func TestLocalFile(t *testing.T) { w = PerformRequest(router, "GET", "/") assert.Contains(t, w.Body.String(), `