-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfile_handler.go
65 lines (52 loc) · 1.3 KB
/
file_handler.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
package plaud
import (
"fmt"
"net/http"
"strings"
)
type FileHandler struct {
handler http.Handler
path string
middlewares []MiddleWareFunc
}
func NewFileHandler(dir http.FileSystem, path string) *FileHandler {
if path == "" {
path = "/"
}
return &FileHandler{
handler: http.FileServer(dir),
path: path,
}
}
func (h *FileHandler) GetRoute() string {
if h.path != "/" {
return fmt.Sprintf("%s/", h.path)
}
return h.path
}
//nolint:unused // TODO: Should be used in a future commit
func (h *FileHandler) applyMiddleware(w http.ResponseWriter, r *http.Request) *Error {
for _, middleware := range h.middlewares {
if err := middleware(w, r); err != nil {
return err
}
}
return nil
}
func (h *FileHandler) GetHandleFunc() func(http.ResponseWriter, *http.Request) {
return nil
}
func (h *FileHandler) GetHandler() http.Handler {
return h.handler
}
func (h *FileHandler) stackMiddleware(middleware []MiddleWareFunc) {
h.middlewares = append(middleware, h.middlewares...)
}
// registers a set of all middlewares
// adds the middlewares in order
func (h *FileHandler) Use(middlewares ...MiddleWareFunc) {
h.middlewares = append(h.middlewares, middlewares...)
}
func (h *FileHandler) Prepend(path string) {
h.path = fmt.Sprintf("%s%s", path, strings.TrimRight(h.path, "/"))
}