Only create assets map on demand

This commit is contained in:
Jakob Borg 2014-09-02 13:07:33 +02:00
parent 1a6ac4aeb1
commit e9c7970ea4
4 changed files with 65 additions and 47 deletions

View File

@ -4,4 +4,20 @@
package auto_test package auto_test
// Empty test file to generate 0% coverage rather than no coverage import (
"bytes"
"testing"
"github.com/syncthing/syncthing/auto"
)
func TestAssets(t *testing.T) {
assets := auto.Assets()
idx, ok := assets["index.html"]
if !ok {
t.Fatal("No index.html in compiled in assets")
}
if !bytes.Contains(idx, []byte("<html")) {
t.Fatal("No html in index.html")
}
}

File diff suppressed because one or more lines are too long

View File

@ -27,17 +27,17 @@ import (
"io/ioutil" "io/ioutil"
) )
var Assets = make(map[string][]byte) func Assets() map[string][]byte {
var assets = make(map[string][]byte, {{.assets | len}})
func init() {
var bs []byte var bs []byte
var gr *gzip.Reader var gr *gzip.Reader
{{range $asset := .assets}} {{range $asset := .assets}}
bs, _ = base64.StdEncoding.DecodeString("{{$asset.Data}}") bs, _ = base64.StdEncoding.DecodeString("{{$asset.Data}}")
gr, _ = gzip.NewReader(bytes.NewBuffer(bs)) gr, _ = gzip.NewReader(bytes.NewBuffer(bs))
bs, _ = ioutil.ReadAll(gr) bs, _ = ioutil.ReadAll(gr)
Assets["{{$asset.Name}}"] = bs assets["{{$asset.Name}}"] = bs
{{end}} {{end}}
return assets
} }
`)) `))

View File

@ -652,6 +652,8 @@ func validAPIKey(k string) bool {
} }
func embeddedStatic(assetDir string) http.Handler { func embeddedStatic(assetDir string) http.Handler {
assets := auto.Assets()
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
file := r.URL.Path file := r.URL.Path
@ -672,7 +674,7 @@ func embeddedStatic(assetDir string) http.Handler {
} }
} }
bs, ok := auto.Assets[file] bs, ok := assets[file]
if !ok { if !ok {
http.NotFound(w, r) http.NotFound(w, r)
return return