-
Notifications
You must be signed in to change notification settings - Fork 0
/
static.go
53 lines (42 loc) · 1.14 KB
/
static.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package kid
import (
"net/http"
"os"
)
// FS is Kid's http.FileSystem implementation to disable directory listing.
type FS struct {
http.FileSystem
}
// File is Kid's http.File implementation to disable directory listing.
type File struct {
http.File
}
// Readdir overrides http.File's default implementation.
//
// It disables directory listing.
func (f File) Readdir(count int) ([]os.FileInfo, error) {
return nil, nil
}
// Open overrides http.FileSystem's default implementation to disable directory listing.
func (fs FS) Open(name string) (http.File, error) {
f, err := fs.FileSystem.Open(name)
if err != nil {
return nil, err
}
return File{f}, err
}
// newFileServer returns new file server.
func newFileServer(urlPath string, fs http.FileSystem) http.Handler {
panicIfNil(fs, "file system cannot be nil")
urlPath = cleanPath(urlPath, false)
urlPath = appendSlash(urlPath)
fileServer := http.StripPrefix(urlPath, http.FileServer(fs))
return fileServer
}
// appendSlash appends slash to a path if the path is not end with slash.
func appendSlash(path string) string {
if path[len(path)-1] != '/' {
path = path + "/"
}
return path
}