Migrate to go:embed from go-bindata

This commit is contained in:
Michał Gątkowski 2022-08-08 01:05:13 +02:00
parent bba53a8be6
commit 7eb9bd7e29
6 changed files with 12 additions and 18 deletions

View File

@ -16,10 +16,6 @@ jobs:
- name: Check out code into the Go module directory - name: Check out code into the Go module directory
uses: actions/checkout@v2 uses: actions/checkout@v2
- name: Install go-bindata
run: |
go get -u -v github.com/kevinburke/go-bindata/...
- name: Build - name: Build
run: | run: |
make magneticod make magneticod

1
.gitignore vendored
View File

@ -1,7 +1,6 @@
vendor/ vendor/
.idea/ .idea/
Gopkg.lock Gopkg.lock
cmd/magneticow/bindata.go
go.sum go.sum

View File

@ -3,7 +3,6 @@ WORKDIR /magnetico
RUN export PATH=$PATH:/go/bin RUN export PATH=$PATH:/go/bin
RUN apk add --no-cache build-base curl git 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 ./Makefile /magnetico/
ADD ./pkg /magnetico/pkg ADD ./pkg /magnetico/pkg

View File

@ -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 go install --tags fts5 "-ldflags=-s -w -X main.compiledOn=`date -u +%Y-%m-%dT%H:%M:%SZ`" ./cmd/magneticod
magneticow: magneticow:
# TODO: minify files! # TODO: minify /data/* 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
go install --tags fts5 "-ldflags=-s -w -X main.compiledOn=`date -u +%Y-%m-%dT%H:%M:%SZ`" ./cmd/magneticow go install --tags fts5 "-ldflags=-s -w -X main.compiledOn=`date -u +%Y-%m-%dT%H:%M:%SZ`" ./cmd/magneticow
.PHONY: docker .PHONY: docker

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"embed"
"net/http" "net/http"
"strings" "strings"
"time" "time"
@ -10,6 +11,9 @@ import (
"github.com/boramalper/magnetico/pkg/persistence" "github.com/boramalper/magnetico/pkg/persistence"
) )
//go:embed data
var content embed.FS
// DONE // DONE
func rootHandler(w http.ResponseWriter, r *http.Request) { func rootHandler(w http.ResponseWriter, r *http.Request) {
nTorrents, err := database.GetNumberOfTorrents() nTorrents, err := database.GetNumberOfTorrents()
@ -26,7 +30,7 @@ func rootHandler(w http.ResponseWriter, r *http.Request) {
} }
func torrentsHandler(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") w.Header().Set("Content-Type", "text/html; charset=utf-8")
// Cache static resources for a day // Cache static resources for a day
w.Header().Set("Cache-Control", "max-age=86400") 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) { 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") w.Header().Set("Content-Type", "text/html; charset=utf-8")
// Cache static resources for a day // Cache static resources for a day
w.Header().Set("Cache-Control", "max-age=86400") 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) { 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") w.Header().Set("Content-Type", "text/html; charset=utf-8")
// Cache static resources for a day // Cache static resources for a day
w.Header().Set("Cache-Control", "max-age=86400") 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) { 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 { if err != nil {
http.NotFound(w, r) http.NotFound(w, r)
return return

View File

@ -176,8 +176,8 @@ func main() {
} }
templates = make(map[string]*template.Template) templates = make(map[string]*template.Template)
templates["feed"] = template.Must(template.New("feed").Funcs(templateFunctions).Parse(string(mustAsset("templates/feed.xml")))) 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("templates/homepage.html")))) templates["homepage"] = template.Must(template.New("homepage").Funcs(templateFunctions).Parse(string(mustAsset("data/templates/homepage.html"))))
database, err = persistence.MakeDatabase(opts.Database, logger) database, err = persistence.MakeDatabase(opts.Database, logger)
if err != nil { if err != nil {
@ -201,7 +201,7 @@ func respondError(w http.ResponseWriter, statusCode int, format string, a ...int
} }
func mustAsset(name string) []byte { func mustAsset(name string) []byte {
data, err := Asset(name) data, err := content.ReadFile(name)
if err != nil { if err != nil {
zap.L().Panic("Could NOT access the requested resource! THIS IS A BUG, PLEASE REPORT", zap.L().Panic("Could NOT access the requested resource! THIS IS A BUG, PLEASE REPORT",
zap.String("name", name), zap.Error(err)) zap.String("name", name), zap.Error(err))