-
Notifications
You must be signed in to change notification settings - Fork 1
/
fastfs.go
159 lines (143 loc) · 3.46 KB
/
fastfs.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package staticfs
import (
"bytes"
"errors"
"fmt"
"html/template"
"io"
"net/http"
"github.com/dkotik/htadaptor"
)
type FastFile struct {
contentType string
contents []byte
}
func NewFastFileFromTemplate(t *template.Template, data any) (*FastFile, error) {
b := &bytes.Buffer{}
if err := t.Execute(b, data); err != nil {
return nil, err
}
return &FastFile{
contentType: http.DetectContentType(b.Bytes()),
contents: b.Bytes(),
}, nil
}
func (f *FastFile) HandleError(
w http.ResponseWriter,
r *http.Request,
err error,
) error {
f.ServeHTTP(w, r)
return nil
}
func (f *FastFile) ServeHTTP(
w http.ResponseWriter,
r *http.Request,
) {
w.Header().Set("content-type", f.contentType)
_, _ = io.Copy(w, bytes.NewReader(f.contents))
}
type fastFileSystem struct {
Index map[string]*FastFile
Fallthrough http.Handler
}
func (f *fastFileSystem) ServeHTTP(w http.ResponseWriter, r *http.Request) {
p := r.URL.Path
file, ok := f.Index[p]
if !ok {
f.Fallthrough.ServeHTTP(w, r)
return
}
file.ServeHTTP(w, r)
}
// NewFastFileSystemFile serves a single file from memory without logging.
func NewFastFileSystemFile(contents []byte) *FastFile {
return &FastFile{
contentType: http.DetectContentType(contents),
contents: contents,
}
}
type fastFileSystemOptions struct {
Index map[string]*FastFile
Fallthrough http.Handler
}
type FastFileSystemOption func(*fastFileSystemOptions) error
func NewFastFileSystem(withOptions ...FastFileSystemOption) (_ http.Handler, err error) {
o := &fastFileSystemOptions{
Index: make(map[string]*FastFile),
}
for _, option := range append(
withOptions,
WithDefaultFastFileSystemFallthrough(),
func(o *fastFileSystemOptions) (err error) {
if len(o.Index) < 1 {
return errors.New("provide at least one file")
}
return nil
},
) {
if err = option(o); err != nil {
return nil, fmt.Errorf("unable to initialize fast file system: %w", err)
}
}
return &fastFileSystem{
Index: o.Index,
Fallthrough: o.Fallthrough,
}, nil
}
func WithFastFileSystemFile(
path string,
contents []byte,
) FastFileSystemOption {
return func(o *fastFileSystemOptions) error {
if path == "" {
return errors.New("cannot use an empty path")
}
if len(contents) < 1 {
return fmt.Errorf("cannot use empty file contents: %s", path)
}
if _, ok := o.Index[path]; ok {
return fmt.Errorf("file path is already set: %s", path)
}
b := make([]byte, len(contents))
copy(b, contents)
o.Index[path] = &FastFile{
contentType: http.DetectContentType(contents),
contents: b,
}
return nil
}
}
func WithFastFileSystemFallthrough(h http.Handler) FastFileSystemOption {
return func(o *fastFileSystemOptions) error {
if h == nil {
return errors.New("cannot use a <nil> handler")
}
if o.Fallthrough != nil {
return errors.New("fall through handler is already set")
}
o.Fallthrough = h
return nil
}
}
func WithDefaultFastFileSystemFallthrough() FastFileSystemOption {
return func(o *fastFileSystemOptions) (err error) {
if o.Fallthrough != nil {
return nil
}
b := &bytes.Buffer{}
if err = (&htadaptor.ErrorMessage{
StatusCode: http.StatusNotFound,
Title: http.StatusText(http.StatusNotFound),
Message: "Requested file does not exist.",
}).Render(b); err != nil {
return fmt.Errorf("unable to render error message: %w", err)
}
return WithFastFileSystemFallthrough(
&FastFile{
contentType: "text/html",
contents: b.Bytes(),
},
)(o)
}
}