embed html template

This commit is contained in:
Ruidy 2025-02-04 18:46:56 +01:00
parent cf1620592a
commit 71a0c033e8
No known key found for this signature in database
GPG key ID: E00F51288CB857CC
17 changed files with 15 additions and 7 deletions

6
assets/assets.go Normal file
View file

@ -0,0 +1,6 @@
package assets
import "embed"
//go:embed assets
var Static embed.FS

View file

Before

Width:  |  Height:  |  Size: 4.8 KiB

After

Width:  |  Height:  |  Size: 4.8 KiB

View file

Before

Width:  |  Height:  |  Size: 23 KiB

After

Width:  |  Height:  |  Size: 23 KiB

View file

Before

Width:  |  Height:  |  Size: 19 KiB

After

Width:  |  Height:  |  Size: 19 KiB

View file

Before

Width:  |  Height:  |  Size: 30 KiB

After

Width:  |  Height:  |  Size: 30 KiB

View file

Before

Width:  |  Height:  |  Size: 95 KiB

After

Width:  |  Height:  |  Size: 95 KiB

View file

@ -6,6 +6,8 @@ import (
"os" "os"
"text/template" "text/template"
"github.com/labstack/gommon/log"
"github.com/rjNemo/rentease/assets"
"github.com/rjNemo/rentease/internal/service/booking" "github.com/rjNemo/rentease/internal/service/booking"
) )
@ -16,9 +18,10 @@ func NewPdfClient() (*HtmlPdfClient, error) {
} }
func (pc *HtmlPdfClient) BuildInvoice(data booking.Invoice) (string, 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 { 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. // 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) return "", fmt.Errorf("error writing HTML file: %v", err)
} }
log.Info("building invoice")
return outputPath, nil return outputPath, nil
} }

View file

@ -101,5 +101,6 @@ func (bs Service) Cancel(id int) {
} }
func (bs Service) BuildInvoice(b *Booking, hc *config.Host) (string, error) { func (bs Service) BuildInvoice(b *Booking, hc *config.Host) (string, error) {
log.Println("Build invoice")
return bs.pdf.BuildInvoice(b.ToInvoice(hc)) return bs.pdf.BuildInvoice(b.ToInvoice(hc))
} }

View file

@ -2,7 +2,6 @@ package main
import ( import (
"context" "context"
"embed"
"fmt" "fmt"
"os" "os"
"os/signal" "os/signal"
@ -11,6 +10,7 @@ import (
"github.com/getsentry/sentry-go" "github.com/getsentry/sentry-go"
"github.com/rjNemo/rentease/assets"
"github.com/rjNemo/rentease/internal/config" "github.com/rjNemo/rentease/internal/config"
"github.com/rjNemo/rentease/internal/driver/calendar" "github.com/rjNemo/rentease/internal/driver/calendar"
"github.com/rjNemo/rentease/internal/driver/database" "github.com/rjNemo/rentease/internal/driver/database"
@ -22,9 +22,6 @@ import (
"github.com/rjNemo/rentease/internal/service/booking" "github.com/rjNemo/rentease/internal/service/booking"
) )
//go:embed assets
var static embed.FS
func main() { func main() {
ctx := context.Background() ctx := context.Background()
@ -102,7 +99,7 @@ func run(c context.Context, getEnv func(string) string) error {
as, as,
config.NewHost(), // TODO: move to the database at some point config.NewHost(), // TODO: move to the database at some point
server.WithPort(port), server.WithPort(port),
server.WithFileSystem(static), server.WithFileSystem(assets.Static),
server.WithDebug(strings.ToLower(getEnv("DEBUG")) == "true"), server.WithDebug(strings.ToLower(getEnv("DEBUG")) == "true"),
server.WithSecretKey(getEnv("SECRET_KEY")), server.WithSecretKey(getEnv("SECRET_KEY")),
server.WithOrigins(origins), server.WithOrigins(origins),