mirror of
https://github.com/rjNemo/rentease.git
synced 2026-06-10 12:46:53 +00:00
### 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.
44 lines
1.1 KiB
Go
44 lines
1.1 KiB
Go
package pdf
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"os"
|
|
"text/template"
|
|
|
|
"github.com/labstack/gommon/log"
|
|
"github.com/rjNemo/rentease/assets"
|
|
"github.com/rjNemo/rentease/internal/service/booking"
|
|
)
|
|
|
|
type HtmlPdfClient struct{}
|
|
|
|
func NewPdfClient() (*HtmlPdfClient, error) {
|
|
return &HtmlPdfClient{}, nil
|
|
}
|
|
|
|
func (pc *HtmlPdfClient) BuildInvoice(data booking.Invoice) (string, error) {
|
|
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)
|
|
}
|
|
|
|
// Create a buffer to hold the rendered HTML.
|
|
var buf bytes.Buffer
|
|
if err := tmpl.Execute(&buf, data); err != nil {
|
|
return "", fmt.Errorf("error executing template: %v", err)
|
|
}
|
|
|
|
outputPath := fmt.Sprintf("%s.html", data.ID)
|
|
if err := os.WriteFile(outputPath, buf.Bytes(), 0644); err != nil {
|
|
return "", fmt.Errorf("error writing HTML file: %v", err)
|
|
}
|
|
|
|
log.Info("building invoice")
|
|
return outputPath, nil
|
|
}
|
|
|
|
func (pc *HtmlPdfClient) BuildReport(context map[string]any, period string, month int, year int) error {
|
|
panic("unimplemented")
|
|
}
|