From bddc4bb0fce25e7738f504d06fc458d9651f7e5e Mon Sep 17 00:00:00 2001 From: Ruidy Date: Tue, 4 Feb 2025 18:49:20 +0100 Subject: [PATCH] embed html template (#40) ### TL;DR Implemented embedded file system for static assets using Go's `embed` package. ### What changed? - Created a new `assets.go` file to define an embedded filesystem for static assets - Moved all static assets (HTML, icons, images, JS) under a nested `assets` directory - Updated PDF generation to use the embedded filesystem when parsing HTML templates - Modified main application to use the embedded filesystem for serving static files - Added logging statements for invoice generation ### How to test? 1. Run the application and verify static assets are served correctly 2. Generate a PDF invoice and confirm it renders properly 3. Check that all HTML error pages (400, 401, 403, 404, 500) are accessible 4. Verify images and icons load correctly throughout the application ### Why make this change? Using an embedded filesystem ensures all static assets are compiled into the binary, making deployment simpler and more reliable. This eliminates the need to manage separate asset files and ensures the application has all required resources available at runtime. --- assets/assets.go | 6 ++++++ assets/{ => assets}/html/HTTP400.html | 0 assets/{ => assets}/html/HTTP401.html | 0 assets/{ => assets}/html/HTTP403.html | 0 assets/{ => assets}/html/HTTP404.html | 0 assets/{ => assets}/html/HTTP500.html | 0 assets/{ => assets}/html/invoice.html | 0 assets/{ => assets}/html/report.html | 0 assets/{ => assets}/icons/favicon-main.png | Bin assets/{ => assets}/icons/favicon.png | Bin assets/{ => assets}/icons/stripe.png | Bin assets/{ => assets}/icons/whatsapp.png | Bin assets/{ => assets}/img/logo.png | Bin assets/{ => assets}/js/htmx.js | 0 internal/driver/pdf/html.go | 8 ++++++-- internal/service/booking/service.go | 1 + main.go | 7 ++----- 17 files changed, 15 insertions(+), 7 deletions(-) create mode 100644 assets/assets.go rename assets/{ => assets}/html/HTTP400.html (100%) rename assets/{ => assets}/html/HTTP401.html (100%) rename assets/{ => assets}/html/HTTP403.html (100%) rename assets/{ => assets}/html/HTTP404.html (100%) rename assets/{ => assets}/html/HTTP500.html (100%) rename assets/{ => assets}/html/invoice.html (100%) rename assets/{ => assets}/html/report.html (100%) rename assets/{ => assets}/icons/favicon-main.png (100%) rename assets/{ => assets}/icons/favicon.png (100%) rename assets/{ => assets}/icons/stripe.png (100%) rename assets/{ => assets}/icons/whatsapp.png (100%) rename assets/{ => assets}/img/logo.png (100%) rename assets/{ => assets}/js/htmx.js (100%) diff --git a/assets/assets.go b/assets/assets.go new file mode 100644 index 0000000..4818be8 --- /dev/null +++ b/assets/assets.go @@ -0,0 +1,6 @@ +package assets + +import "embed" + +//go:embed assets +var Static embed.FS diff --git a/assets/html/HTTP400.html b/assets/assets/html/HTTP400.html similarity index 100% rename from assets/html/HTTP400.html rename to assets/assets/html/HTTP400.html diff --git a/assets/html/HTTP401.html b/assets/assets/html/HTTP401.html similarity index 100% rename from assets/html/HTTP401.html rename to assets/assets/html/HTTP401.html diff --git a/assets/html/HTTP403.html b/assets/assets/html/HTTP403.html similarity index 100% rename from assets/html/HTTP403.html rename to assets/assets/html/HTTP403.html diff --git a/assets/html/HTTP404.html b/assets/assets/html/HTTP404.html similarity index 100% rename from assets/html/HTTP404.html rename to assets/assets/html/HTTP404.html diff --git a/assets/html/HTTP500.html b/assets/assets/html/HTTP500.html similarity index 100% rename from assets/html/HTTP500.html rename to assets/assets/html/HTTP500.html diff --git a/assets/html/invoice.html b/assets/assets/html/invoice.html similarity index 100% rename from assets/html/invoice.html rename to assets/assets/html/invoice.html diff --git a/assets/html/report.html b/assets/assets/html/report.html similarity index 100% rename from assets/html/report.html rename to assets/assets/html/report.html diff --git a/assets/icons/favicon-main.png b/assets/assets/icons/favicon-main.png similarity index 100% rename from assets/icons/favicon-main.png rename to assets/assets/icons/favicon-main.png diff --git a/assets/icons/favicon.png b/assets/assets/icons/favicon.png similarity index 100% rename from assets/icons/favicon.png rename to assets/assets/icons/favicon.png diff --git a/assets/icons/stripe.png b/assets/assets/icons/stripe.png similarity index 100% rename from assets/icons/stripe.png rename to assets/assets/icons/stripe.png diff --git a/assets/icons/whatsapp.png b/assets/assets/icons/whatsapp.png similarity index 100% rename from assets/icons/whatsapp.png rename to assets/assets/icons/whatsapp.png diff --git a/assets/img/logo.png b/assets/assets/img/logo.png similarity index 100% rename from assets/img/logo.png rename to assets/assets/img/logo.png diff --git a/assets/js/htmx.js b/assets/assets/js/htmx.js similarity index 100% rename from assets/js/htmx.js rename to assets/assets/js/htmx.js diff --git a/internal/driver/pdf/html.go b/internal/driver/pdf/html.go index 3ed2dc2..180f862 100644 --- a/internal/driver/pdf/html.go +++ b/internal/driver/pdf/html.go @@ -6,6 +6,8 @@ import ( "os" "text/template" + "github.com/labstack/gommon/log" + "github.com/rjNemo/rentease/assets" "github.com/rjNemo/rentease/internal/service/booking" ) @@ -16,9 +18,10 @@ func NewPdfClient() (*HtmlPdfClient, error) { } func (pc *HtmlPdfClient) BuildInvoice(data booking.Invoice) (string, error) { - tmpl, err := template.ParseFiles("assets/html/invoice.html") + log.Info("building invoice") + tmpl, err := template.ParseFS(assets.Static, "assets/html/invoice.html") if err != nil { - return "", fmt.Errorf("Error parsing template: %v", err) + return "", fmt.Errorf("error parsing template: %v", err) } // Create a buffer to hold the rendered HTML. @@ -32,6 +35,7 @@ func (pc *HtmlPdfClient) BuildInvoice(data booking.Invoice) (string, error) { return "", fmt.Errorf("error writing HTML file: %v", err) } + log.Info("building invoice") return outputPath, nil } diff --git a/internal/service/booking/service.go b/internal/service/booking/service.go index 3f22020..6f4dbf5 100644 --- a/internal/service/booking/service.go +++ b/internal/service/booking/service.go @@ -101,5 +101,6 @@ func (bs Service) Cancel(id int) { } func (bs Service) BuildInvoice(b *Booking, hc *config.Host) (string, error) { + log.Println("Build invoice") return bs.pdf.BuildInvoice(b.ToInvoice(hc)) } diff --git a/main.go b/main.go index 399174b..a982f43 100644 --- a/main.go +++ b/main.go @@ -2,7 +2,6 @@ package main import ( "context" - "embed" "fmt" "os" "os/signal" @@ -11,6 +10,7 @@ import ( "github.com/getsentry/sentry-go" + "github.com/rjNemo/rentease/assets" "github.com/rjNemo/rentease/internal/config" "github.com/rjNemo/rentease/internal/driver/calendar" "github.com/rjNemo/rentease/internal/driver/database" @@ -22,9 +22,6 @@ import ( "github.com/rjNemo/rentease/internal/service/booking" ) -//go:embed assets -var static embed.FS - func main() { ctx := context.Background() @@ -102,7 +99,7 @@ func run(c context.Context, getEnv func(string) string) error { as, config.NewHost(), // TODO: move to the database at some point server.WithPort(port), - server.WithFileSystem(static), + server.WithFileSystem(assets.Static), server.WithDebug(strings.ToLower(getEnv("DEBUG")) == "true"), server.WithSecretKey(getEnv("SECRET_KEY")), server.WithOrigins(origins),