-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfavicon.go
51 lines (41 loc) · 982 Bytes
/
favicon.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
package zebra
import (
"fmt"
"io/ioutil"
"net/http"
)
var maxAge int = 86400
func ServeFavicon(path string) Middleware {
return &favicon{path}
}
type favicon struct {
path string
}
func (f *favicon) Excute(cxt *Context) bool {
if "/favicon.ico" != cxt.Urlpath {
return true
}
if len(f.path) <= 0 {
f.path = "favicon.ico"
}
w := cxt.OriginalResponseWriter()
if cxt.Method != MethodGet && cxt.Method != MethodHead {
w.Header().Set("Allow", "GET, HEAD, OPTIONS")
if cxt.Method == MethodOptions {
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusMethodNotAllowed)
}
return false
}
if data, error := ioutil.ReadFile(f.path); error == nil {
w.Header().Set("Cache-Control", fmt.Sprintf("public, max-age=%d", maxAge))
w.Header().Set("Content-Type", "image/x-icon")
w.Write(data)
} else {
logger := cxt.Logger()
// TODO 好像没必要这样
logger.Panicf("找不到指定的favicon.ico:%s\n", f.path)
}
return false
}