Skip to content

Commit

Permalink
added final code for implementation of a static template cache and a …
Browse files Browse the repository at this point in the history
…turn on/off feature
  • Loading branch information
jagottsicher committed Mar 6, 2023
1 parent b3ede7f commit c0f2332
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 8 deletions.
10 changes: 8 additions & 2 deletions cmd/web/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,15 @@ func main() {
}

app.TemplateCache = tc
app.UseCache = false

http.HandleFunc("/", handlers.Home)
http.HandleFunc("/about", handlers.About)
repo := handlers.NewRepo(&app)
handlers.NewHandlers(repo)

render.NewTemplates(&app)

http.HandleFunc("/", handlers.Repo.Home)
http.HandleFunc("/about", handlers.Repo.About)

fmt.Println(fmt.Sprintf("Starting application on port %s", portNumber))
_ = http.ListenAndServe(portNumber, nil)
Expand Down
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ import "html/template"
// AppConfig is a struct holding die application's configuration
type AppConfig struct {
TemplateCache map[string]*template.Template
UseCache bool
}
25 changes: 23 additions & 2 deletions pkg/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,36 @@ package handlers
import (
"net/http"

"github.com/jagottsicher/myGoWebApplication/pkg/config"
"github.com/jagottsicher/myGoWebApplication/pkg/render"
)

// Repository is the repository type
type Repository struct {
App *config.AppConfig
}

// Repo the repository used by the handlers
var Repo *Repository

// 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 handler for the home page
func Home(w http.ResponseWriter, r *http.Request) {
func (m *Repository) Home(w http.ResponseWriter, r *http.Request) {
render.RenderTemplate(w, "home-page.tpml")
}

// About is the handler for the about page
func About(w http.ResponseWriter, r *http.Request) {
func (m *Repository) About(w http.ResponseWriter, r *http.Request) {
render.RenderTemplate(w, "about-page.tpml")
}
22 changes: 19 additions & 3 deletions pkg/render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,39 @@ import (
"log"
"net/http"
"path/filepath"

"github.com/jagottsicher/myGoWebApplication/pkg/config"
)

var app *config.AppConfig

// NewTemplates sets the config for the template package
func NewTemplates(a *config.AppConfig) {
app = a
}

// rendernTemplate serves as a wrapper and renders
// a layout and a template from folder /templates to a desired writer
func RenderTemplate(w http.ResponseWriter, tpml string) {
// get the template cache from the app config
var tc map[string]*template.Template

if app.UseCache {
// get the template cache from the app config
tc = app.TemplateCache
} else {
tc, _ = CreateTemplateCache()
}

// get the right template from cache
t, ok := tc[tpml]
if !ok {
log.Fatalln("template not in cache for some reason ", err)
log.Fatalln("template not in cache for some reason ", ok)
}

// store result in a buffer and double-check if it is a valid value
buf := new(bytes.Buffer)

err = t.Execute(buf, nil)
err := t.Execute(buf, nil)
if err != nil {
log.Println(err)
}
Expand Down
2 changes: 1 addition & 1 deletion templates/about-page.tpml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<div class="row">
<div class="col">
<h1>This is the about page</h1>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sapiente ducimus possimus praesentium quaerat libero omnis rem eius quas qui iure id tenetur autem est eum, commodi ipsa sit labore nam!
YYY Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sapiente ducimus possimus praesentium quaerat libero omnis rem eius quas qui iure id tenetur autem est eum, commodi ipsa sit labore nam!
</div>
</div>
</div>
Expand Down

0 comments on commit c0f2332

Please sign in to comment.