From def8d767f262ad51a86ac6fc2ffdb3c729c9c2e2 Mon Sep 17 00:00:00 2001 From: Immanuel Onyeka Date: Mon, 4 Sep 2023 19:55:06 -0400 Subject: [PATCH] Cache report view at startup --- skouter.go | 44 ++++++++++++++++------- views/{test.tpl => report/comparison.tpl} | 40 +++++++++++---------- views/{pdf.tpl => report/master.tpl} | 1 - 3 files changed, 53 insertions(+), 32 deletions(-) rename views/{test.tpl => report/comparison.tpl} (59%) rename views/{pdf.tpl => report/master.tpl} (86%) diff --git a/skouter.go b/skouter.go index 80d45f0..bf57607 100644 --- a/skouter.go +++ b/skouter.go @@ -179,13 +179,13 @@ var paths = map[string]string { "home": "views/home.tpl", "terms": "views/terms.tpl", "app": "views/app.tpl", - "test": "views/test.tpl", + "comparison": "views/report/comparison.tpl", } var pages = map[string]Page { "home": cache("home", "Home"), "terms": cache("terms", "Terms and Conditions"), - "test": cache("test", "PDF test"), + "test": cachePdf("comparison"), "app": cache("app", "App"), } @@ -208,10 +208,25 @@ func (c UserClaims) Valid() error { func cache(name string, title string) Page { var p = []string{"views/master.tpl", paths[name]} tpl := template.Must(template.ParseFiles(p...)) - return Page{tpl: tpl, - Title: title, - Name: name, - } + return Page{tpl: tpl, Title: title, Name: name} +} + +func cachePdf(name string) Page { + dollars := func(cents int) string { + return strconv.FormatFloat(float64(cents)/100, 'f', 2, 32) + } + diff := func(a, b int) string { + return strconv.FormatFloat(float64(b - a)/100, 'f', 2, 32) + } + + fm := template.FuncMap{ + "dollars": dollars, + "diff": diff, + } + var p = []string{"views/report/master.tpl", paths[name]} + + tpl := template.Must(template.New("master.tpl").Funcs(fm).ParseFiles(p...)) + return Page{ tpl: tpl, Title: "", Name: name } } func (page Page) Render(w http.ResponseWriter) { @@ -1822,8 +1837,8 @@ func showPDF(w http.ResponseWriter, r *http.Request) { // maybe os.Exit(1) instead } - var pa = template.Must(template.ParseFiles("views/master.tpl", - "views/test.tpl")) + page := pages["test"] + estimates, err := getEstimates(db, 1, 0) if err != nil { w.WriteHeader(500); return } @@ -1851,7 +1866,7 @@ func showPDF(w http.ResponseWriter, r *http.Request) { info.Letterhead = base64.StdEncoding.EncodeToString(letterhead) - err = pa.Execute(w, info) + err = page.tpl.ExecuteTemplate(w, "master.tpl", info) if err != nil {fmt.Println(err)} } @@ -1879,8 +1894,13 @@ func getPdf(w http.ResponseWriter, db *sql.DB, r *http.Request) { log.Fatal(err) } - var pa = template.Must(template.ParseFiles("views/pdf.tpl", - "views/test.tpl")) + fm := template.FuncMap{ + "dollars": func(cents int) string { + return strconv.FormatFloat(float64(cents)/100, 'f', 2, 32) + }} + + t := template.New("master.tpl").Funcs(fm) + var pa = template.Must(t.ParseFiles("views/report/master.tpl", "views/report/comparison.tpl")) claims, err := getClaims(r) if err != nil { @@ -1911,7 +1931,7 @@ func getPdf(w http.ResponseWriter, db *sql.DB, r *http.Request) { base64.StdEncoding.EncodeToString(letterhead) - err = pa.Execute(stdin, info) + err = pa.ExecuteTemplate(stdin, "master.tpl", info) if err != nil { w.WriteHeader(500); log.Println(err) diff --git a/views/test.tpl b/views/report/comparison.tpl similarity index 59% rename from views/test.tpl rename to views/report/comparison.tpl index a723bca..7193a3d 100644 --- a/views/test.tpl +++ b/views/report/comparison.tpl @@ -1,10 +1,8 @@ {{define "header"}} -
-
{{end}} {{define "main"}} -
+

Actual costs may vary from estimates after approval. Get an official quote before choosing a loan.

@@ -33,38 +31,42 @@ Type -{{range $l := .Estimate.Loans}}{{$l.Type.Name}}{{end}} +{{range .Estimate.Loans}}{{.Type.Name}}{{end}} Term -{{range $l := .Estimate.Loans}}{{$l.Term}} Years{{end}} +{{range .Estimate.Loans}}{{.Term}} Years{{end}} Amortization -{{range $l := .Estimate.Loans}}{{$l.Amortization}}{{end}} +{{range .Estimate.Loans}}{{.Amortization}}{{end}} Interest rate -{{range $l := .Estimate.Loans}}{{$l.Interest}}%{{end}} +{{range .Estimate.Loans}}{{.Interest}}%{{end}} Monthly MI - {{range $l := .Estimate.Loans}}${{$l.Mi.Premium}}/month{{end}} + {{range .Estimate.Loans}}${{.Mi.Premium}}/month{{end}} MI Premium - {{range $l := .Estimate.Loans}}${{$l.Mi.Upfront}}{{end}} + {{range .Estimate.Loans}}${{.Mi.Upfront}}{{end}} Real estate taxes - {{range $l := .Estimate.Loans}}${{$l.Tax}}{{end}} + {{range .Estimate.Loans}}${{.Tax}}{{end}} Total monthly payment -{{range $l := .Estimate.Loans}}{{$l.Result}}{{end}} +{{range .Estimate.Loans}}{{.Result}}{{end}} Purchase price -{{range $l := .Estimate.Loans}}{{$.Estimate.Price}}{{end}} +{{range .Estimate.Loans}}${{dollars $.Estimate.Price}}{{end}} +Loan amount +{{range .Estimate.Loans}}${{dollars .Amount}}{{end}} Term -{{range $l := .Estimate.Loans}}{{$l.Term}} Years{{end}} +{{range .Estimate.Loans}}{{.Term}} Years{{end}} Amortization -{{range $l := .Estimate.Loans}}{{$l.Amortization}}{{end}} +{{range .Estimate.Loans}}{{.Amortization}}{{end}} Interest rate -{{range $l := .Estimate.Loans}}{{$l.Interest}}%{{end}} +{{range .Estimate.Loans}}{{.Interest}}%{{end}} Monthly MI - {{range $l := .Estimate.Loans}}${{$l.Mi.Premium}}/month{{end}} + {{range .Estimate.Loans}}${{.Mi.Premium}}/month{{end}} MI Premium - {{range $l := .Estimate.Loans}}${{$l.Mi.Upfront}}{{end}} + {{range .Estimate.Loans}}${{.Mi.Upfront}}{{end}} Real estate taxes - {{range $l := .Estimate.Loans}}${{$l.Tax}}{{end}} + {{range .Estimate.Loans}}${{.Tax}}{{end}} Total monthly payment -{{range $l := .Estimate.Loans}}{{$l.Result}}{{end}} +{{range .Estimate.Loans}}{{.Result.TotalMonthly}}{{end}} +Purchase price +{{range .Estimate.Loans}}${{dollars $.Estimate.Price}}{{end}} diff --git a/views/pdf.tpl b/views/report/master.tpl similarity index 86% rename from views/pdf.tpl rename to views/report/master.tpl index cc695d4..6d45880 100644 --- a/views/pdf.tpl +++ b/views/report/master.tpl @@ -3,7 +3,6 @@ -