summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--cmd/web/handlers.go23
-rw-r--r--cmd/web/helpers.go21
-rw-r--r--cmd/web/main.go10
-rw-r--r--cmd/web/templates.go41
-rw-r--r--ui/html/pages/operations_list.tmpl.html (renamed from ui/html/pages/operations/list.tmpl.html)1
-rw-r--r--ui/html/pages/organisations_list.tmpl.html (renamed from ui/html/pages/organisations/list.tmpl.html)0
6 files changed, 74 insertions, 22 deletions
diff --git a/cmd/web/handlers.go b/cmd/web/handlers.go
index 6901661..4d90469 100644
--- a/cmd/web/handlers.go
+++ b/cmd/web/handlers.go
@@ -34,32 +34,15 @@ func (app *application) listOperations(w http.ResponseWriter, r *http.Request) {
}
}
- files := []string{
- "./ui/html/base.tmpl.html",
- "./ui/html/pages/operations/list.tmpl.html",
- "./ui/html/partials/nav.tmpl.html",
- }
-
- ts, err := template.ParseFiles(files...)
- if err != nil {
- app.serverError(w, r, err) // Use the serverError() helper
- return
- }
-
- data := operationsTemplateData{
+ app.render(w, r, http.StatusOK, "operations_list.tmpl.html", templateData{
Operations: newOps,
- }
-
- err = ts.ExecuteTemplate(w, "base", data)
- if err != nil {
- app.serverError(w, r, err) // Use the serverError() helper
- }
+ })
}
func (app *application) listOrganisations(w http.ResponseWriter, r *http.Request) {
files := []string{
"./ui/html/base.tmpl.html",
- "./ui/html/pages/organisations/list.tmpl.html",
+ "./ui/html/pages/organisations_list.tmpl.html",
"./ui/html/partials/nav.tmpl.html",
}
diff --git a/cmd/web/helpers.go b/cmd/web/helpers.go
index 5f7e4d3..fc9b8f8 100644
--- a/cmd/web/helpers.go
+++ b/cmd/web/helpers.go
@@ -1,6 +1,7 @@
package main
import (
+ "fmt"
"log"
"net/http"
"runtime/debug"
@@ -29,3 +30,23 @@ func (app *application) clientError(w http.ResponseWriter, status int) {
func (app *application) notFound(w http.ResponseWriter) {
app.clientError(w, http.StatusNotFound)
}
+
+func (app *application) render(w http.ResponseWriter, r *http.Request, status int, page string, data templateData) {
+ // retrieve the appropriate template set from the cache based on the page name.
+ // If no entry exists in the cache with the provided name, create a new error
+ // and call serverError() helper that we made earlier and return.
+ ts, ok := app.templateCache[page]
+ if !ok {
+ err := fmt.Errorf("the template %s does not exist", page)
+ app.serverError(w, r, err)
+ return
+ }
+
+ // Write out the provided HTTP status code('200 OK', '400 Bad Request', etc).
+ w.WriteHeader(status)
+
+ err := ts.ExecuteTemplate(w, "base", data)
+ if err != nil {
+ app.serverError(w, r, err)
+ }
+}
diff --git a/cmd/web/main.go b/cmd/web/main.go
index 7e25124..f0632a5 100644
--- a/cmd/web/main.go
+++ b/cmd/web/main.go
@@ -3,6 +3,7 @@ package main
import (
"database/sql"
"flag"
+ "html/template"
"log/slog"
"net/http"
"os"
@@ -16,6 +17,7 @@ type application struct {
operations *models.OperationModel
organisations *models.OrganisationModel
engagement_strategies *models.EngagementStrategyModel
+ templateCache map[string]*template.Template
}
func main() {
@@ -34,11 +36,19 @@ func main() {
defer db.Close()
+ // initialise the new template cache...
+ templateCache, err := newTemplateCache()
+ if err != nil {
+ logger.Error(err.Error())
+ os.Exit(1)
+ }
+
app := &application{
logger: logger,
operations: &models.OperationModel{DB: db},
organisations: &models.OrganisationModel{DB: db},
engagement_strategies: &models.EngagementStrategyModel{DB: db},
+ templateCache: templateCache,
}
// mux := http.NewServeMux()
diff --git a/cmd/web/templates.go b/cmd/web/templates.go
index 86e136d..2457053 100644
--- a/cmd/web/templates.go
+++ b/cmd/web/templates.go
@@ -1,7 +1,44 @@
package main
-import "github.com/yulqen/ded-go-core/internal/models"
+import (
+ "html/template"
+ "path/filepath"
-type operationsTemplateData struct {
+ "github.com/yulqen/ded-go-core/internal/models"
+)
+
+type templateData struct {
Operations []models.Operation
}
+
+func newTemplateCache() (map[string]*template.Template, error) {
+ cache := map[string]*template.Template{}
+
+ // get a slice of filepaths match the pattern for our page templates
+ // e.g. [ui/html/pages/home.tmpl.html ui/html/pages/view.tmpl.html]
+ pages, err := filepath.Glob("./ui/html/pages/*.html")
+ if err != nil {
+ return nil, err
+ }
+
+ // loop through the page filepaths one by one
+ for _, page := range pages {
+ // extract "home.tmp.html" from the full filepath.
+ name := filepath.Base(page)
+
+ files := []string{
+ "./ui/html/base.tmpl.html",
+ "./ui/html/partials/nav.tmpl.html",
+ page,
+ }
+
+ ts, err := template.ParseFiles(files...)
+ if err != nil {
+ return nil, err
+ }
+
+ cache[name] = ts
+ }
+
+ return cache, nil
+}
diff --git a/ui/html/pages/operations/list.tmpl.html b/ui/html/pages/operations_list.tmpl.html
index eac3325..2863d19 100644
--- a/ui/html/pages/operations/list.tmpl.html
+++ b/ui/html/pages/operations_list.tmpl.html
@@ -8,6 +8,7 @@
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
</p>
+
{{if .Operations}}
<table id="home-summary-table">
<thead>
diff --git a/ui/html/pages/organisations/list.tmpl.html b/ui/html/pages/organisations_list.tmpl.html
index 1b0e3a9..1b0e3a9 100644
--- a/ui/html/pages/organisations/list.tmpl.html
+++ b/ui/html/pages/organisations_list.tmpl.html