-
Notifications
You must be signed in to change notification settings - Fork 0
/
static.go
46 lines (38 loc) · 1.33 KB
/
static.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
// Package static provides a handler for serving static assets from an in-memory
// map.
package static
import (
"net/http"
"strings"
"time"
)
// use start time as a conservative bound for last-modified
var lastMod = time.Now()
type Handler struct {
Assets map[string]string
// Index is the name of an entry in Assets that should be used if the request
// path is empty (equivalent to requesting "/"). This is analogous to index
// documents commonly used in webservers. If Index is empty, it will be
// ignored.
Index string
// Default is the name of an entry in Assets that should be used if the
// the requested path does not exist in Assets. This is useful for
// delivering a common document (usually a frontend application script) that
// handles URL-based state on the client side. If Default is empty, it will be
// ignored.
Default string
}
func (h Handler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
output, ok := h.Assets[r.URL.Path]
if !ok && r.URL.Path == "" && h.Index != "" {
output = h.Assets[h.Index]
} else if !ok && h.Default != "" {
output = h.Assets[h.Default]
} else if !ok {
http.NotFound(rw, r)
return
}
// Some autogenerated documentation uses frames, e.g. Javadoc
rw.Header().Set("X-Frame-Options", "SAMEORIGIN")
http.ServeContent(rw, r, r.URL.Path, lastMod, strings.NewReader(output))
}