From 1dd22ba8dbaf690e779150858271fe5c096a44fc Mon Sep 17 00:00:00 2001 From: Muyao CHEN Date: Thu, 27 Jun 2024 14:03:43 +0200 Subject: [PATCH] Optimizing template cache by using an application config *** --- cmd/web/main.go | 21 +++++++++++++++++++-- pkg/config/config.go | 9 +++++++++ pkg/handlers/handlers.go | 25 +++++++++++++++++++++++-- pkg/render/render.go | 25 ++++++++++++++++++------- 4 files changed, 69 insertions(+), 11 deletions(-) create mode 100644 pkg/config/config.go diff --git a/cmd/web/main.go b/cmd/web/main.go index 2ab165e..282c677 100644 --- a/cmd/web/main.go +++ b/cmd/web/main.go @@ -2,7 +2,10 @@ package main import ( "fmt" + "go-udemy-web-1/pkg/config" "go-udemy-web-1/pkg/handlers" + "go-udemy-web-1/pkg/render" + "log" "net/http" ) @@ -10,8 +13,22 @@ const portNumber = ":8080" // main is the main application function func main() { - http.HandleFunc("/", handlers.Home) - http.HandleFunc("/about", handlers.About) + var app config.AppConfig + + tc, err := render.CreateTemplateCache() + if err != nil { + log.Fatal("cannot create template cache") + } + app.TemplateCahce = tc + app.UseCache = false + + repo := handlers.NewRepo(&app) + handlers.NewHandlers(repo) + + render.NewTemplates(&app) + + http.HandleFunc("/", handlers.Repo.Home) + http.HandleFunc("/about", handlers.Repo.About) fmt.Printf("Starting application on port %s\n", portNumber) diff --git a/pkg/config/config.go b/pkg/config/config.go new file mode 100644 index 0000000..1da9e87 --- /dev/null +++ b/pkg/config/config.go @@ -0,0 +1,9 @@ +package config + +import "html/template" + +// AppConfig holds the application config +type AppConfig struct { + TemplateCahce map[string]*template.Template + UseCache bool +} diff --git a/pkg/handlers/handlers.go b/pkg/handlers/handlers.go index 201c327..9d8207f 100644 --- a/pkg/handlers/handlers.go +++ b/pkg/handlers/handlers.go @@ -1,16 +1,37 @@ package handlers import ( + "go-udemy-web-1/pkg/config" "go-udemy-web-1/pkg/render" "net/http" ) +// Repo the repository used by the handlers +var Repo *Repository + +// Repository is the repository type +type Repository struct { + App *config.AppConfig +} + +// NewRepo creates a new repository +func NewRepo(a *config.AppConfig) *Repository { + return &Repository{ + App: a, + } +} + +// NewHandlers sets the repository for the handlers +func NewHandlers(r *Repository) { + Repo = r +} + // Home is the about page handler -func Home(w http.ResponseWriter, r *http.Request) { +func (m *Repository) Home(w http.ResponseWriter, r *http.Request) { render.RenderTemplate(w, "home.page.tmpl") } // About is the about page handler -func About(w http.ResponseWriter, r *http.Request) { +func (m *Repository) About(w http.ResponseWriter, r *http.Request) { render.RenderTemplate(w, "about.page.tmpl") } diff --git a/pkg/render/render.go b/pkg/render/render.go index 54cd584..89ea25b 100644 --- a/pkg/render/render.go +++ b/pkg/render/render.go @@ -2,29 +2,40 @@ package render import ( "bytes" + "go-udemy-web-1/pkg/config" "html/template" "log" "net/http" "path/filepath" ) +var app *config.AppConfig + +// NewTemplates sets the config for the template package +func NewTemplates(a *config.AppConfig) { + app = a +} + // RenderTemplate renders a HTML template file func RenderTemplate(w http.ResponseWriter, tmpl string) { - // create a template cache - tc, err := createTemplateCache() - if err != nil { - log.Fatal(err) + var tc map[string]*template.Template + if app.UseCache { + // get the template cache from the app config + tc = app.TemplateCahce + } else { + tc, _ = CreateTemplateCache() } + // get requested template from cache t, ok := tc[tmpl] if !ok { - log.Fatal(err) + log.Fatal("Could not get template from template cache") } // Write to a buffer to make sure that the template can be read and // written successfully buf := new(bytes.Buffer) - err = t.Execute(buf, nil) + err := t.Execute(buf, nil) if err != nil { log.Println(err) } @@ -36,7 +47,7 @@ func RenderTemplate(w http.ResponseWriter, tmpl string) { } } -func createTemplateCache() (map[string]*template.Template, error) { +func CreateTemplateCache() (map[string]*template.Template, error) { myCache := map[string]*template.Template{} // get all of the files named *.page.tmpl from ./templates