From 7eb9bd7e29ebdddfbfa280975e58f3e92346c257 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20G=C4=85tkowski?= Date: Mon, 8 Aug 2022 01:05:13 +0200 Subject: [PATCH] Migrate to go:embed from go-bindata --- .github/workflows/go.yml | 4 ---- .gitignore | 1 - Dockerfile.magneticow | 1 - Makefile | 6 +----- cmd/magneticow/handlers.go | 12 ++++++++---- cmd/magneticow/main.go | 6 +++--- 6 files changed, 12 insertions(+), 18 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 139af38..49818d8 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -16,10 +16,6 @@ jobs: - name: Check out code into the Go module directory uses: actions/checkout@v2 - - name: Install go-bindata - run: | - go get -u -v github.com/kevinburke/go-bindata/... - - name: Build run: | make magneticod diff --git a/.gitignore b/.gitignore index 1589a7f..b6cef6b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,6 @@ vendor/ .idea/ Gopkg.lock -cmd/magneticow/bindata.go go.sum diff --git a/Dockerfile.magneticow b/Dockerfile.magneticow index 8e3499e..c5b191d 100644 --- a/Dockerfile.magneticow +++ b/Dockerfile.magneticow @@ -3,7 +3,6 @@ WORKDIR /magnetico RUN export PATH=$PATH:/go/bin RUN apk add --no-cache build-base curl git -RUN go install -a -v github.com/go-bindata/go-bindata/...@latest ADD ./Makefile /magnetico/ ADD ./pkg /magnetico/pkg diff --git a/Makefile b/Makefile index 9278e34..ae703f4 100644 --- a/Makefile +++ b/Makefile @@ -6,11 +6,7 @@ magneticod: go install --tags fts5 "-ldflags=-s -w -X main.compiledOn=`date -u +%Y-%m-%dT%H:%M:%SZ`" ./cmd/magneticod magneticow: - # TODO: minify files! - # https://github.com/kevinburke/go-bindata - go-bindata -pkg "main" -o="cmd/magneticow/bindata.go" -prefix="cmd/magneticow/data/" cmd/magneticow/data/... - # Prepend the linter instruction to the beginning of the file - sed -i '1s;^;//lint:file-ignore * Ignore file altogether\n;' cmd/magneticow/bindata.go + # TODO: minify /data/* files! go install --tags fts5 "-ldflags=-s -w -X main.compiledOn=`date -u +%Y-%m-%dT%H:%M:%SZ`" ./cmd/magneticow .PHONY: docker diff --git a/cmd/magneticow/handlers.go b/cmd/magneticow/handlers.go index 8fcddf7..a02416d 100644 --- a/cmd/magneticow/handlers.go +++ b/cmd/magneticow/handlers.go @@ -1,6 +1,7 @@ package main import ( + "embed" "net/http" "strings" "time" @@ -10,6 +11,9 @@ import ( "github.com/boramalper/magnetico/pkg/persistence" ) +//go:embed data +var content embed.FS + // DONE func rootHandler(w http.ResponseWriter, r *http.Request) { nTorrents, err := database.GetNumberOfTorrents() @@ -26,7 +30,7 @@ func rootHandler(w http.ResponseWriter, r *http.Request) { } func torrentsHandler(w http.ResponseWriter, r *http.Request) { - data := mustAsset("templates/torrents.html") + data, _ := content.ReadFile("data/templates/torrents.html") w.Header().Set("Content-Type", "text/html; charset=utf-8") // Cache static resources for a day w.Header().Set("Cache-Control", "max-age=86400") @@ -34,7 +38,7 @@ func torrentsHandler(w http.ResponseWriter, r *http.Request) { } func torrentsInfohashHandler(w http.ResponseWriter, r *http.Request) { - data := mustAsset("templates/torrent.html") + data, _ := content.ReadFile("data/templates/torrent.html") w.Header().Set("Content-Type", "text/html; charset=utf-8") // Cache static resources for a day w.Header().Set("Cache-Control", "max-age=86400") @@ -42,7 +46,7 @@ func torrentsInfohashHandler(w http.ResponseWriter, r *http.Request) { } func statisticsHandler(w http.ResponseWriter, r *http.Request) { - data := mustAsset("templates/statistics.html") + data, _ := content.ReadFile("data/templates/statistics.html") w.Header().Set("Content-Type", "text/html; charset=utf-8") // Cache static resources for a day w.Header().Set("Cache-Control", "max-age=86400") @@ -98,7 +102,7 @@ func feedHandler(w http.ResponseWriter, r *http.Request) { } func staticHandler(w http.ResponseWriter, r *http.Request) { - data, err := Asset(r.URL.Path[1:]) + data, err := content.ReadFile("data/" + r.URL.Path[1:]) if err != nil { http.NotFound(w, r) return diff --git a/cmd/magneticow/main.go b/cmd/magneticow/main.go index 63aa44f..191fad8 100644 --- a/cmd/magneticow/main.go +++ b/cmd/magneticow/main.go @@ -176,8 +176,8 @@ func main() { } templates = make(map[string]*template.Template) - templates["feed"] = template.Must(template.New("feed").Funcs(templateFunctions).Parse(string(mustAsset("templates/feed.xml")))) - templates["homepage"] = template.Must(template.New("homepage").Funcs(templateFunctions).Parse(string(mustAsset("templates/homepage.html")))) + templates["feed"] = template.Must(template.New("feed").Funcs(templateFunctions).Parse(string(mustAsset("data/templates/feed.xml")))) + templates["homepage"] = template.Must(template.New("homepage").Funcs(templateFunctions).Parse(string(mustAsset("data/templates/homepage.html")))) database, err = persistence.MakeDatabase(opts.Database, logger) if err != nil { @@ -201,7 +201,7 @@ func respondError(w http.ResponseWriter, statusCode int, format string, a ...int } func mustAsset(name string) []byte { - data, err := Asset(name) + data, err := content.ReadFile(name) if err != nil { zap.L().Panic("Could NOT access the requested resource! THIS IS A BUG, PLEASE REPORT", zap.String("name", name), zap.Error(err))