/
fileserver.go
67 lines (57 loc) · 1.44 KB
/
fileserver.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
// SPDX-License-Identifier: MIT
package filesystem
import (
"bytes"
"errors"
"io/fs"
"log"
"net/http"
"path"
"path/filepath"
"github.com/caixw/blogit/v2/internal/vars"
)
// FileServer 以 fsys 为根目录作为静态文件服务
//
// erro 在出错时日志的输出通道,可以为空,表示输出到 log.Default();
func FileServer(fsys fs.FS, erro *log.Logger) http.Handler {
if erro == nil {
erro = log.Default()
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
p := r.URL.Path
if p != "" && p[0] == '/' {
p = p[1:]
}
if p == "" || p[len(p)-1] == '/' {
p += vars.IndexFilename
}
STAT:
stat, err := fs.Stat(fsys, p)
if err != nil {
printError(erro, err, w)
return
}
if stat.IsDir() {
p = path.Join(p, vars.IndexFilename)
goto STAT
}
data, err := fs.ReadFile(fsys, p)
if err != nil {
printError(erro, err, w)
return
}
buf := bytes.NewReader(data)
http.ServeContent(w, r, filepath.Base(p), stat.ModTime(), buf)
})
}
func printError(erro *log.Logger, err error, w http.ResponseWriter) {
switch {
case errors.Is(err, fs.ErrPermission):
http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
case errors.Is(err, fs.ErrNotExist):
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
case err != nil:
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
erro.Println(err)
}
}