-
Notifications
You must be signed in to change notification settings - Fork 402
/
cache.go
63 lines (55 loc) · 1.76 KB
/
cache.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
// Copyright (C) 2022 Storj Labs, Inc.
// See LICENSE for copying information.
package web
import (
"net/http"
"path"
"strings"
)
// CacheHandler is a middleware for caching static files for 1 year.
func CacheHandler(fn http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
header := w.Header()
// "mime" package, which http.FileServer uses, depends on Operating System
// configuration for mime-types. When a system has hardcoded mime-types to
// something else, they might serve ".js" as a "plain/text".
//
// Override any of that default behavior to ensure we get the correct types for
// common files.
if contentType, ok := CommonContentType(path.Ext(r.URL.Path)); ok {
header.Set("Content-Type", contentType)
}
header.Set("Cache-Control", "public, max-age=31536000")
header.Set("X-Content-Type-Options", "nosniff")
header.Set("Referrer-Policy", "same-origin")
fn.ServeHTTP(w, r)
})
}
// CommonContentType returns content-type for common extensions,
// ignoring OS settings.
func CommonContentType(ext string) (string, bool) {
ext = strings.ToLower(ext)
mime, ok := commonContentType[ext]
return mime, ok
}
var commonContentType = map[string]string{
".css": "text/css; charset=utf-8",
".gif": "image/gif",
".htm": "text/html; charset=utf-8",
".html": "text/html; charset=utf-8",
".jpeg": "image/jpeg",
".jpg": "image/jpeg",
".js": "application/javascript",
".mjs": "application/javascript",
".otf": "font/otf",
".pdf": "application/pdf",
".png": "image/png",
".svg": "image/svg+xml",
".ttf": "font/ttf",
".wasm": "application/wasm",
".webp": "image/webp",
".xml": "text/xml; charset=utf-8",
".sfnt": "font/sfnt",
".woff": "font/woff",
".woff2": "font/woff2",
}