use embedfs for static files and templates
Marco Andronaco andronacomarco@gmail.com
Sat, 02 Mar 2024 13:06:21 +0100
6 files changed,
33 insertions(+),
23 deletions(-)
M
.gitignore
→
.gitignore
@@ -1,6 +1,6 @@
.env __debug_bin* -static/res/cache +art credentials.json token.json dist
M
Dockerfile
→
Dockerfile
@@ -16,8 +16,6 @@ COPY *.go ./
# Build RUN CGO_ENABLED=0 go build -trimpath -o /dist/app -COPY templates /dist/templates -COPY static /dist/static # Test FROM build-stage AS run-test-stage
M
artbound.go
→
artbound.go
@@ -2,6 +2,7 @@ package main
import ( "bytes" + "embed" "encoding/json" "html/template" "log"@@ -14,9 +15,16 @@ "github.com/BiRabittoh/artbound-go/cache"
"github.com/joho/godotenv" ) -var templatesDirectory = "templates/" -var indexTemplate = template.Must(template.ParseFiles(templatesDirectory + "index.html")) -var helpTemplate = template.Must(template.ParseFiles(templatesDirectory + "help.html")) +const templatesDirectory = "templates/" + +var ( + //go:embed templates/index.html templates/help.html + templates embed.FS + //go:embed all:static + static embed.FS + indexTemplate = template.Must(template.ParseFS(templates, templatesDirectory+"index.html")) + helpTemplate = template.Must(template.ParseFS(templates, templatesDirectory+"help.html")) +) type TemplateData struct { Emoji EmojiDict@@ -139,7 +147,10 @@ if spreadsheetRange == "" {
log.Fatal("Please fill out SPREADSHEET_RANGE in .env") } - fs := http.FileServer(http.Dir("./static")) + cacheRemotePath := "/" + cache.CachePath + "/" + cacheFS := http.FileServer(http.Dir(cache.CachePath)) + + fs := http.FileServer(http.FS(static)) db := cache.InitDB(spreadsheetId, spreadsheetRange) r := http.NewServeMux()@@ -148,7 +159,8 @@ r.HandleFunc("/clear", clearHandler(db))
r.HandleFunc("/update", updateHandler(db)) r.HandleFunc("/get", getHandler(db)) r.HandleFunc("/help", helpHandler) - r.Handle("/static/", http.StripPrefix("/static/", fs)) + r.Handle("/static/", fs) + r.Handle(cacheRemotePath, http.StripPrefix(cacheRemotePath, cacheFS)) log.Println("Serving on port", port) err = http.ListenAndServe(":"+port, r)
M
cache/api.go
→
cache/api.go
@@ -131,7 +131,7 @@ }
return rows, nil } -func getFile(googleApi *GoogleAPI, fileID string, cachePath string) (string, error) { +func getFile(googleApi *GoogleAPI, fileID string, CachePath string) (string, error) { srv, err := drive.NewService(googleApi.Ctx, option.WithHTTPClient(googleApi.Client)) if err != nil { log.Fatalf("Unable to retrieve Drive client: %v", err)@@ -144,7 +144,7 @@ }
fileExt := filepath.Ext(fileInfo.Name) fileName := fileID + fileExt - filePath := filepath.Join(cachePath, fileName) + filePath := filepath.Join(CachePath, fileName) out, err := os.Create(filePath) if err != nil { return "", err
M
cache/cache.go
→
cache/cache.go
@@ -9,7 +9,7 @@ "strings"
"time" ) -var cachePath = filepath.Join("static", "res", "cache") +const CachePath = "art" type UpdateDBPayload struct { LastUpdated string `json:"timestamp"`@@ -37,10 +37,10 @@ }
return } -func listCachedEntries(cachePath string) ([]fileDetails, error) { +func listCachedEntries(CachePath string) ([]fileDetails, error) { var files []fileDetails - err := filepath.Walk(cachePath, func(path string, info os.FileInfo, err error) error { + err := filepath.Walk(CachePath, func(path string, info os.FileInfo, err error) error { if err != nil { return err }@@ -86,7 +86,7 @@ log.Println(entry.FileID, "is cached.")
return ext } log.Println(entry.FileID, "is not cached. Downloading.") - ext, err := getFile(&db.googleApi, entry.FileID, cachePath) + ext, err := getFile(&db.googleApi, entry.FileID, CachePath) if err != nil { log.Println("Could not download file", entry.FileID) }@@ -94,7 +94,7 @@ return ext
} func InitDB(spreadsheetId string, spreadsheetRange string) *DB { - files, err := listCachedEntries(cachePath) + files, err := listCachedEntries(CachePath) if err != nil { log.Println("Could not list cached entries.") }@@ -104,20 +104,20 @@ db.update()
return db } -func (db *DB) update() (error, int) { +func (db *DB) update() (int, error) { entries, err := getEntries(&db.googleApi) if err != nil { log.Println("Could not update DB!", err) - return err, 0 + return 0, err } newEntries := len(entries) - len(db.Entries) db.Entries = entries db.LastUpdated = time.Now() - return nil, newEntries + return newEntries, nil } func (db *DB) UpdateCall() UpdateDBPayload { - err, newEntries := db.update() + newEntries, err := db.update() if err != nil { log.Println("Could not update DB!", err) newEntries = 0@@ -129,21 +129,21 @@ func (db *DB) GetEntries(month string) ([]Entry, error) {
monthTest := func(f Entry) bool { return f.Month == month } res := filter(db.Entries, monthTest) - if err := os.MkdirAll(cachePath, os.ModePerm); err != nil { + if err := os.MkdirAll(CachePath, os.ModePerm); err != nil { return nil, err } for i := range res { e := &res[i] ext := handleEntry(e, db) - e.FilePath = filepath.Join(cachePath, e.FileID+ext) + e.FilePath = filepath.Join(CachePath, e.FileID+ext) } return res, nil } func (db *DB) Clear() error { - err := filepath.Walk(cachePath, func(path string, info os.FileInfo, err error) error { + err := filepath.Walk(CachePath, func(path string, info os.FileInfo, err error) error { if err != nil { return err }
M
docker-compose.yaml
→
docker-compose.yaml
@@ -13,6 +13,6 @@ volumes:
- /etc/localtime:/etc/localtime:ro - ${PWD}/credentials.json:/app/credentials.json:ro - ${PWD}/token.json:/app/token.json:ro - - cache:/app/static/res/cache/ + - cache:/app/art/ volumes: cache: