Optimizing template cache by using an application config ***

This commit is contained in:
Muyao CHEN 2024-06-27 14:03:43 +02:00
parent 2391f5a160
commit 1dd22ba8db
4 changed files with 69 additions and 11 deletions

View File

@ -2,7 +2,10 @@ package main
import ( import (
"fmt" "fmt"
"go-udemy-web-1/pkg/config"
"go-udemy-web-1/pkg/handlers" "go-udemy-web-1/pkg/handlers"
"go-udemy-web-1/pkg/render"
"log"
"net/http" "net/http"
) )
@ -10,8 +13,22 @@ const portNumber = ":8080"
// main is the main application function // main is the main application function
func main() { func main() {
http.HandleFunc("/", handlers.Home) var app config.AppConfig
http.HandleFunc("/about", handlers.About)
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) fmt.Printf("Starting application on port %s\n", portNumber)

9
pkg/config/config.go Normal file
View File

@ -0,0 +1,9 @@
package config
import "html/template"
// AppConfig holds the application config
type AppConfig struct {
TemplateCahce map[string]*template.Template
UseCache bool
}

View File

@ -1,16 +1,37 @@
package handlers package handlers
import ( import (
"go-udemy-web-1/pkg/config"
"go-udemy-web-1/pkg/render" "go-udemy-web-1/pkg/render"
"net/http" "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 // 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") render.RenderTemplate(w, "home.page.tmpl")
} }
// About is the about page handler // 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") render.RenderTemplate(w, "about.page.tmpl")
} }

View File

@ -2,29 +2,40 @@ package render
import ( import (
"bytes" "bytes"
"go-udemy-web-1/pkg/config"
"html/template" "html/template"
"log" "log"
"net/http" "net/http"
"path/filepath" "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 // RenderTemplate renders a HTML template file
func RenderTemplate(w http.ResponseWriter, tmpl string) { func RenderTemplate(w http.ResponseWriter, tmpl string) {
// create a template cache var tc map[string]*template.Template
tc, err := createTemplateCache() if app.UseCache {
if err != nil { // get the template cache from the app config
log.Fatal(err) tc = app.TemplateCahce
} else {
tc, _ = CreateTemplateCache()
} }
// get requested template from cache // get requested template from cache
t, ok := tc[tmpl] t, ok := tc[tmpl]
if !ok { 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 // Write to a buffer to make sure that the template can be read and
// written successfully // written successfully
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
err = t.Execute(buf, nil) err := t.Execute(buf, nil)
if err != nil { if err != nil {
log.Println(err) 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{} myCache := map[string]*template.Template{}
// get all of the files named *.page.tmpl from ./templates // get all of the files named *.page.tmpl from ./templates