-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
49 lines (42 loc) · 1.03 KB
/
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
package handler
/* *
* Copyright (c) 2023, @jhekau <mr.evgeny.u@gmail.com>
* 12 August 2023
*/
import (
"io"
"net/http"
"time"
"github.com/jhekau/favicon/pkg/core/models/logger"
)
const (
logP = `github.com/jhekau/favicon/pkg/core/http/v1/handler/handler.go`
logH1 = `H1: get file`
)
type Content interface {
ServeFile(urlPath string) (content io.ReadSeekCloser, modtime time.Time, name string, exists bool, err error)
}
type Handler struct {
L logger.Logger
}
func (h *Handler) Handle(c ...Content) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet && r.Method != http.MethodHead {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
for _, c := range c {
content, modtime, name, exists, err := c.ServeFile(r.URL.Path)
if err != nil {
h.L.Error(logP, logH1, err)
w.WriteHeader(http.StatusInternalServerError)
return
}
if exists {
http.ServeContent(w, r, name, modtime, content)
return
}
}
w.WriteHeader(http.StatusNotFound)
}
}