-
Notifications
You must be signed in to change notification settings - Fork 50
/
serv_statics.go
50 lines (41 loc) · 965 Bytes
/
serv_statics.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
package main
import (
"bytes"
"io"
"io/ioutil"
"net/http"
"time"
)
// Mod time will be init, every time, you start the server,
// Becasuse there is not an easy way of determining the timestamp of compilation
// or I don't know it.
var ModTime = time.Now()
type ServeStaticFromBinary struct {
MountPoint string
DataDir string // data/ with end slash
}
func (s *ServeStaticFromBinary) ServeHTTP(w http.ResponseWriter, r *http.Request) {
file := r.URL.Path[len(s.MountPoint):]
// requires go-bindata assets generation
data, err := Asset(s.DataDir + file)
if err != nil {
http.NotFound(w, r)
return
}
t := ModTime
if debug {
t = time.Now()
}
http.ServeContent(w, r, file, t, NewAssetDownload(data))
}
// AssetDownload for implementing reader, and closer
type AssetDownload struct {
*bytes.Reader
io.Closer
}
func NewAssetDownload(a []byte) *AssetDownload {
return &AssetDownload{
bytes.NewReader(a),
ioutil.NopCloser(nil),
}
}