-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_files.go
72 lines (59 loc) · 1.63 KB
/
handler_files.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package akhttpd
import (
"errors"
"net/http"
"os"
"path/filepath"
)
// spellchecker:words akhttpd
// handlePathOrFallback sends filepath to w with the provided content type
//
// When filepath does not exist (or is the empty string), returns fallbackBytes.
// Otherwise returns an error.
func handlePathOrFallback(w http.ResponseWriter, filepath string, fallbackBytes []byte, contentType string) error {
var bytes []byte
var err error = os.ErrNotExist
// read bytes and error from the provided file
if filepath != "" {
bytes, err = os.ReadFile(filepath)
}
// if we could not find the error, use fallback!
if errors.Is(err, os.ErrNotExist) {
bytes = fallbackBytes
err = nil
}
// other unknown error occurred; something went wrong!
if err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return err
}
// write
w.Header().Set("Content-Type", contentType)
_, err = w.Write(bytes)
return err
}
// ServeUnderscore returns an http.Handler that serves the provided filesystem path under the prefix '_'.
func (Handler) ServeUnderscore(path string) http.Handler {
return http.StripPrefix("/_/", http.FileServer(noIndexFileSystem{http.Dir(path)}))
}
type noIndexFileSystem struct {
http.FileSystem
}
func (fs noIndexFileSystem) Open(path string) (http.File, error) {
file, err := fs.FileSystem.Open(path)
if err != nil {
return nil, err
}
var stat os.FileInfo
if stat, err = file.Stat(); err != nil {
return nil, err
}
if stat.IsDir() {
index := filepath.Join(path, "index.html")
if _, err := fs.FileSystem.Open(index); err != nil {
file.Close()
return nil, err
}
}
return file, nil
}