common/embed: replace all go:embed use by an embedded archive

Some of the files were quite big:

- asns.csv ~ 3 MB
- index.js ~ 1.5 MB
- *.svg ~ 2 MB

Use a ZIP archive to put them all and embed it. This reduce the binary
size from 89 MB to 82 MB. 🤯

This also pulls some code modernization (use of http.ServeFileFS).
This commit is contained in:
Vincent Bernat
2025-09-02 23:30:20 +02:00
parent d102e5f20e
commit b1d6382585
13 changed files with 107 additions and 47 deletions

View File

@@ -4,31 +4,20 @@
package console
import (
"embed"
"net/http"
"time"
)
//go:embed data/frontend
var embeddedAssets embed.FS
func (c *Component) defaultHandlerFunc(w http.ResponseWriter, req *http.Request) {
assets := c.embedOrLiveFS(embeddedAssets, "data/frontend")
f, err := http.FS(assets).Open("index.html")
if err != nil {
http.Error(w, "Application not found.", http.StatusInternalServerError)
return
}
http.ServeContent(w, req, "index.html", time.Time{}, f)
f.Close()
assets := c.embedOrLiveFS("data/frontend")
http.ServeFileFS(w, req, assets, "index.html")
}
func (c *Component) staticAssetsHandlerFunc(w http.ResponseWriter, req *http.Request) {
assets := c.embedOrLiveFS(embeddedAssets, "data/frontend/assets")
assets := c.embedOrLiveFS("data/frontend/assets")
http.FileServer(http.FS(assets)).ServeHTTP(w, req)
}
func (c *Component) docAssetsHandlerFunc(w http.ResponseWriter, req *http.Request) {
docs := c.embedOrLiveFS(embeddedDocs, "data/docs")
docs := c.embedOrLiveFS("data/docs")
http.FileServer(http.FS(docs)).ServeHTTP(w, req)
}