payit/internal/web/page.go
Ruidy bf721dc130
feat(web): add logging middleware and refactor handlers
Introduced LoggerMiddleware for HTTP request logging. Refactored handler
methods to return http.HandlerFunc for improved composability. Updated
route registration and tests to use new handler signatures.
2025-09-28 19:43:30 +02:00

32 lines
829 B
Go

package web
import (
"fmt"
"net/http"
"strings"
)
type checkoutPageData struct {
ProductName string
ProductDescription string
PriceDisplay string
Currency string
}
func (h *Handler) renderCheckoutPage() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
price := float64(h.cfg.Product.PriceCents) / 100
data := checkoutPageData{
ProductName: h.cfg.Product.Name,
ProductDescription: h.cfg.Product.Description,
PriceDisplay: fmt.Sprintf("$%.2f", price),
Currency: strings.ToUpper(h.cfg.Product.Currency),
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
if err := h.page.ExecuteTemplate(w, "index.html", data); err != nil {
http.Error(w, "failed to render page", http.StatusInternalServerError)
return
}
}
}