1
0
Fork 0

Use embed package for binary assets instead of generated files

This commit is contained in:
Frédéric Guillot 2021-02-16 23:36:17 -08:00 committed by fguillot
parent 5d65a85bdb
commit 0de80c2ff0
6 changed files with 53 additions and 56 deletions

View file

@ -8,7 +8,6 @@ package main
import (
"crypto/sha256"
"encoding/base64"
"fmt"
"os"
"path"
@ -153,21 +152,6 @@ func generateCSSBundle(bundleFile string, themes map[string][]string) {
bundle.Write(bundleFile)
}
func generateBinaryBundle(bundleFile string, srcFiles []string) {
bundle := NewBundle("static", "Binaries", "ui/static")
for _, srcFile := range srcFiles {
data := readFile(srcFile)
filename := basename(srcFile)
encodedData := base64.StdEncoding.EncodeToString(data)
bundle.Files[filename] = string(encodedData)
bundle.Checksums[filename] = checksum(data)
}
bundle.Write(bundleFile)
}
func generateBundle(bundleFile, pkg, mapName string, srcFiles []string) {
bundle := NewBundle(pkg, mapName, pkg)
@ -212,8 +196,6 @@ func main() {
"system_sans_serif": []string{"ui/static/css/system.css", "ui/static/css/sans_serif.css", "ui/static/css/common.css"},
})
generateBinaryBundle("ui/static/bin.go", glob("ui/static/bin/*"))
generateBundle("template/views.go", "template", "templateViewsMap", glob("template/html/*.html"))
generateBundle("template/common.go", "template", "templateCommonMap", glob("template/html/common/*.html"))
}

View file

@ -6,7 +6,6 @@ package main // import "miniflux.app"
//go:generate go run generate.go
//go:generate gofmt -s -w ui/static/css.go
//go:generate gofmt -s -w ui/static/bin.go
//go:generate gofmt -s -w ui/static/js.go
//go:generate gofmt -s -w template/views.go
//go:generate gofmt -s -w template/common.go

File diff suppressed because one or more lines are too long

47
ui/static/static.go Normal file
View file

@ -0,0 +1,47 @@
// Copyright 2021 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package static // import "miniflux.app/ui/static"
import (
"crypto/sha256"
"embed"
"fmt"
)
//go:embed bin/*
var binaryFiles embed.FS
var binaryFileChecksums map[string]string
func init() {
binaryFileChecksums = make(map[string]string)
dirEntries, err := binaryFiles.ReadDir("bin")
if err != nil {
panic(err)
}
for _, dirEntry := range dirEntries {
data, err := LoadBinaryFile(dirEntry.Name())
if err != nil {
panic(err)
}
binaryFileChecksums[dirEntry.Name()] = fmt.Sprintf("%x", sha256.Sum256(data))
}
}
// LoadBinaryFile loads an embed binary file.
func LoadBinaryFile(filename string) ([]byte, error) {
return binaryFiles.ReadFile(fmt.Sprintf(`bin/%s`, filename))
}
// GetBinaryFileChecksum returns a binary file checksum.
func GetBinaryFileChecksum(filename string) (string, error) {
if _, found := binaryFileChecksums[filename]; !found {
return "", fmt.Errorf(`static: unable to find checksum for %q`, filename)
}
return binaryFileChecksums[filename], nil
}

View file

@ -5,7 +5,6 @@
package ui // import "miniflux.app/ui"
import (
"encoding/base64"
"net/http"
"time"
@ -17,14 +16,14 @@ import (
func (h *handler) showAppIcon(w http.ResponseWriter, r *http.Request) {
filename := request.RouteStringParam(r, "filename")
etag, found := static.BinariesChecksums[filename]
if !found {
etag, err := static.GetBinaryFileChecksum(filename)
if err != nil {
html.NotFound(w, r)
return
}
response.New(w, r).WithCaching(etag, 72*time.Hour, func(b *response.Builder) {
blob, err := base64.StdEncoding.DecodeString(static.Binaries[filename])
blob, err := static.LoadBinaryFile(filename)
if err != nil {
html.ServerError(w, r, err)
return

View file

@ -5,7 +5,6 @@
package ui // import "miniflux.app/ui"
import (
"encoding/base64"
"net/http"
"time"
@ -15,14 +14,14 @@ import (
)
func (h *handler) showFavicon(w http.ResponseWriter, r *http.Request) {
etag, found := static.BinariesChecksums["favicon.ico"]
if !found {
etag, err := static.GetBinaryFileChecksum("favicon.ico")
if err != nil {
html.NotFound(w, r)
return
}
response.New(w, r).WithCaching(etag, 48*time.Hour, func(b *response.Builder) {
blob, err := base64.StdEncoding.DecodeString(static.Binaries["favicon.ico"])
blob, err := static.LoadBinaryFile("favicon.ico")
if err != nil {
html.ServerError(w, r, err)
return