/
server.go
67 lines (61 loc) · 1.88 KB
/
server.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
64
65
66
67
//go:generate staticfiles -o hugo.go ../../../../hugo/public
package tmp
import (
"compress/gzip"
"io"
"net/http"
"strconv"
"strings"
"time"
"github.com/opentracing/opentracing-go"
)
// ServeHTTP serves a request, attempting to reply with an embedded file.
// It improves on the provided ServeHTTP by checking if an 'index.html'
// file exists.
func ServeHTTPWithIndexes(rw http.ResponseWriter, req *http.Request) {
// configure tracing
span, ctx := opentracing.StartSpanFromContext(req.Context(), "ServeHTTPWithIndexes")
req = req.WithContext(ctx)
defer span.Finish()
// now, serve the request
f, ok := staticFiles[strings.TrimPrefix(req.URL.Path, "/")]
if !ok {
f, ok = staticFiles[strings.TrimSuffix(strings.TrimPrefix(req.URL.Path, "/"), "/")+"/index.html"]
if !ok {
NotFound(rw, req)
return
}
}
header := rw.Header()
if f.hash != "" {
if hash := req.Header.Get("If-None-Match"); hash == f.hash {
rw.WriteHeader(http.StatusNotModified)
return
}
header.Set("ETag", f.hash)
}
if !f.mtime.IsZero() {
if t, err := time.Parse(http.TimeFormat, req.Header.Get("If-Modified-Since")); err == nil && f.mtime.Before(t.Add(1*time.Second)) {
rw.WriteHeader(http.StatusNotModified)
return
}
header.Set("Last-Modified", f.mtime.UTC().Format(http.TimeFormat))
}
header.Set("Content-Type", f.mime)
// Check if the asset is compressed in the binary
if f.size == 0 {
header.Set("Content-Length", strconv.Itoa(len(f.data)))
io.WriteString(rw, f.data)
} else {
if header.Get("Content-Encoding") == "" && strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") {
header.Set("Content-Encoding", "gzip")
header.Set("Content-Length", strconv.Itoa(len(f.data)))
io.WriteString(rw, f.data)
} else {
header.Set("Content-Length", strconv.Itoa(f.size))
reader, _ := gzip.NewReader(strings.NewReader(f.data))
io.Copy(rw, reader)
reader.Close()
}
}
}