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 (
"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)

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
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")
}

View File

@ -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