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.
6
assets/assets.go
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
package assets
|
||||
|
||||
import "embed"
|
||||
|
||||
//go:embed assets
|
||||
var Static embed.FS
|
||||
|
Before Width: | Height: | Size: 4.8 KiB After Width: | Height: | Size: 4.8 KiB |
|
Before Width: | Height: | Size: 23 KiB After Width: | Height: | Size: 23 KiB |
|
Before Width: | Height: | Size: 19 KiB After Width: | Height: | Size: 19 KiB |
|
Before Width: | Height: | Size: 30 KiB After Width: | Height: | Size: 30 KiB |
|
Before Width: | Height: | Size: 95 KiB After Width: | Height: | Size: 95 KiB |
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
}
|
||||
|
|
|
|||
7
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),
|
||||
|
|
|
|||