webassets, client: embed assets in compiled binary

Go 1.16 introduced the embed library, which allows for embedding files
into the compiled binary. This simplifies deployment as only the
executable is required on the host, without any separate web assets
folders.

This patch applies this change to the client and webassets directories.
pull/242/head
Mark Stenglein 3 years ago
parent 44e2b1c207
commit 9eee8e781e

@ -34,9 +34,7 @@ RUN chmod 777 /config; \
WORKDIR /api
COPY --from=builder /api/app .
COPY client ./client
COPY webassets ./webassets
EXPOSE 8080
ENTRYPOINT ["./app"]
ENTRYPOINT ["./app"]

@ -6,7 +6,7 @@ This guide has been written with Ubuntu in mind. If you are using any other flav
## Install Go
Podgrab is built using Go which would be needed to compile and build the source code. Podgrab is written with Go 1.15 so any version equal to or above this should be good to Go.
Podgrab is built using Go which would be needed to compile and build the source code. Podgrab is written with Go 1.16 so any version equal to or above this should be good to Go.
If you already have Go installed on your machine, you can skip to the next step.
@ -31,8 +31,6 @@ git clone --depth 1 https://github.com/akhilrex/podgrab
``` bash
cd podgrab
mkdir -p ./dist
cp -r client ./dist
cp -r webassets ./dist
cp .env ./dist
go build -o ./dist/podgrab ./main.go
```

@ -1,6 +1,6 @@
module github.com/akhilrex/podgrab
go 1.15
go 1.16
require (
github.com/TheHippo/podcastindex v1.0.0

@ -1,9 +1,12 @@
package main
import (
"embed"
"fmt"
"html/template"
"io/fs"
"log"
"net/http"
"os"
"path"
"strconv"
@ -18,6 +21,13 @@ import (
_ "github.com/joho/godotenv/autoload"
)
var (
//go:embed client
clientEmbed embed.FS
//go:embed webassets
webAssetsEmbed embed.FS
)
func main() {
var err error
db.DB, err = db.Init()
@ -127,7 +137,7 @@ func main() {
return fmt.Sprintf("%02d:%02d", mins, secs)
},
}
tmpl := template.Must(template.New("main").Funcs(funcMap).ParseGlob("client/*"))
tmpl := template.Must(template.New("main").Funcs(funcMap).ParseFS(clientEmbed, "client/*"))
//r.LoadHTMLGlob("client/*")
r.SetHTMLTemplate(tmpl)
@ -145,7 +155,12 @@ func main() {
dataPath := os.Getenv("DATA")
backupPath := path.Join(os.Getenv("CONFIG"), "backups")
router.Static("/webassets", "./webassets")
webAssets, err := fs.Sub(webAssetsEmbed, "webassets")
if err != nil {
log.Fatal(err)
}
router.StaticFS("/webassets", http.FS(webAssets))
router.Static("/assets", dataPath)
router.Static(backupPath, backupPath)
router.POST("/podcasts", controllers.AddPodcast)

Loading…
Cancel
Save