-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.go
More file actions
49 lines (42 loc) · 1.07 KB
/
handler.go
File metadata and controls
49 lines (42 loc) · 1.07 KB
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
package wo
import (
"io/fs"
"net/http"
)
func WrapMiddleware[T Resolver](m func(http.Handler) http.Handler) func(T) error {
return func(e T) (err error) {
m(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
e.SetRequest(r)
switch resp := w.(type) {
case *Response:
e.SetResponse(resp)
default:
e.SetResponse(NewResponse(w))
}
err = e.Next()
})).ServeHTTP(e.Response(), e.Request())
return
}
}
func WrapHandler[T Resolver](h http.Handler) func(T) error {
return func(e T) error {
h.ServeHTTP(e.Response(), e.Request())
return nil
}
}
func FileFS[T interface{ FileFS(fs.FS, string) error }](fsys fs.FS, filename string) func(T) error {
if fsys == nil {
panic("FileFS: the provided fs.FS argument is nil")
}
return func(e T) error {
return e.FileFS(fsys, filename)
}
}
func StaticFS[T interface{ StaticFS(fs.FS, bool) error }](fsys fs.FS, indexFallback bool) func(T) error {
if fsys == nil {
panic("StaticFS: the provided fs.FS argument is nil")
}
return func(e T) error {
return e.StaticFS(fsys, indexFallback)
}
}