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/241/head
Mark Stenglein 3 years ago
parent 457f95885d
commit e34bce82ac

@ -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"]

@ -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,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