Use embed package for binary assets instead of generated files
This commit is contained in:
parent
5d65a85bdb
commit
0de80c2ff0
6 changed files with 53 additions and 56 deletions
18
generate.go
18
generate.go
|
@ -8,7 +8,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"encoding/base64"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
@ -153,21 +152,6 @@ func generateCSSBundle(bundleFile string, themes map[string][]string) {
|
||||||
bundle.Write(bundleFile)
|
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) {
|
func generateBundle(bundleFile, pkg, mapName string, srcFiles []string) {
|
||||||
bundle := NewBundle(pkg, mapName, pkg)
|
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"},
|
"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/views.go", "template", "templateViewsMap", glob("template/html/*.html"))
|
||||||
generateBundle("template/common.go", "template", "templateCommonMap", glob("template/html/common/*.html"))
|
generateBundle("template/common.go", "template", "templateCommonMap", glob("template/html/common/*.html"))
|
||||||
}
|
}
|
||||||
|
|
1
main.go
1
main.go
|
@ -6,7 +6,6 @@ package main // import "miniflux.app"
|
||||||
|
|
||||||
//go:generate go run generate.go
|
//go:generate go run generate.go
|
||||||
//go:generate gofmt -s -w ui/static/css.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 ui/static/js.go
|
||||||
//go:generate gofmt -s -w template/views.go
|
//go:generate gofmt -s -w template/views.go
|
||||||
//go:generate gofmt -s -w template/common.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
47
ui/static/static.go
Normal 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
|
||||||
|
}
|
|
@ -5,7 +5,6 @@
|
||||||
package ui // import "miniflux.app/ui"
|
package ui // import "miniflux.app/ui"
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/base64"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -17,14 +16,14 @@ import (
|
||||||
|
|
||||||
func (h *handler) showAppIcon(w http.ResponseWriter, r *http.Request) {
|
func (h *handler) showAppIcon(w http.ResponseWriter, r *http.Request) {
|
||||||
filename := request.RouteStringParam(r, "filename")
|
filename := request.RouteStringParam(r, "filename")
|
||||||
etag, found := static.BinariesChecksums[filename]
|
etag, err := static.GetBinaryFileChecksum(filename)
|
||||||
if !found {
|
if err != nil {
|
||||||
html.NotFound(w, r)
|
html.NotFound(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
response.New(w, r).WithCaching(etag, 72*time.Hour, func(b *response.Builder) {
|
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 {
|
if err != nil {
|
||||||
html.ServerError(w, r, err)
|
html.ServerError(w, r, err)
|
||||||
return
|
return
|
||||||
|
|
|
@ -5,7 +5,6 @@
|
||||||
package ui // import "miniflux.app/ui"
|
package ui // import "miniflux.app/ui"
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/base64"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -15,14 +14,14 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func (h *handler) showFavicon(w http.ResponseWriter, r *http.Request) {
|
func (h *handler) showFavicon(w http.ResponseWriter, r *http.Request) {
|
||||||
etag, found := static.BinariesChecksums["favicon.ico"]
|
etag, err := static.GetBinaryFileChecksum("favicon.ico")
|
||||||
if !found {
|
if err != nil {
|
||||||
html.NotFound(w, r)
|
html.NotFound(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
response.New(w, r).WithCaching(etag, 48*time.Hour, func(b *response.Builder) {
|
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 {
|
if err != nil {
|
||||||
html.ServerError(w, r, err)
|
html.ServerError(w, r, err)
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in a new issue