Skip to content

Commit

Permalink
webassets, client: embed assets in compiled binary
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ocelotsloth committed Oct 6, 2022
1 parent 44e2b1c commit 9485104
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
4 changes: 1 addition & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
2 changes: 0 additions & 2 deletions docs/ubuntu-install.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
19 changes: 17 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package main

import (
"embed"
"fmt"
"html/template"
"io/fs"
"log"
"net/http"
"os"
"path"
"strconv"
Expand All @@ -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()
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 9485104

Please sign in to comment.