diff --git a/cmd/web/main.go b/cmd/web/main.go index 0829bc0..dff222e 100644 --- a/cmd/web/main.go +++ b/cmd/web/main.go @@ -2,16 +2,32 @@ package main import ( "fmt" + "log" "net/http" + "github.com/jbrainz/go-web/pkg/config" "github.com/jbrainz/go-web/pkg/handlers" + "github.com/jbrainz/go-web/pkg/render" ) const port = ":8080" 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 file") + } + app.TemplateCache = tc + app.UseCache = false + + repo := handlers.NewRepo(&app) + handlers.NewHandlers(repo) + + render.NewTemplate(&app) + http.HandleFunc("/", repo.Home) + http.HandleFunc("/about", repo.About) fmt.Println(fmt.Sprintf("starting application o port %s", port)) _ = http.ListenAndServe(port, nil) diff --git a/pkg/config/config.go b/pkg/config/config.go new file mode 100644 index 0000000..7644b16 --- /dev/null +++ b/pkg/config/config.go @@ -0,0 +1,13 @@ +package config + +import ( + "html/template" + "log" +) + +// AppConfig holds the application configuration +type AppConfig struct { + UseCache bool + TemplateCache map[string]*template.Template + InfoLog *log.Logger +} diff --git a/pkg/handlers/handlers.go b/pkg/handlers/handlers.go index 689482f..1bc8f4d 100644 --- a/pkg/handlers/handlers.go +++ b/pkg/handlers/handlers.go @@ -3,15 +3,40 @@ package handlers import ( "net/http" + "github.com/jbrainz/go-web/pkg/config" + "github.com/jbrainz/go-web/pkg/models" "github.com/jbrainz/go-web/pkg/render" ) +var Repo *Repository + +// Repository type for the repo +type Repository struct { + App *config.AppConfig +} + +// NewRepo creates the repository for the +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 homepage handler -func Home(w http.ResponseWriter, r *http.Request) { - render.Template(w, "home.page.html") +func (m *Repository) Home(w http.ResponseWriter, r *http.Request) { + render.Template(w, "home.page.html", &models.TemplateData{}) } // About : the about page handler -func About(w http.ResponseWriter, r *http.Request) { - render.Template(w, "about.page.html") +func (m *Repository) About(w http.ResponseWriter, r *http.Request) { + stringMap := make(map[string]string) + stringMap["test"] = "Check this out" + render.Template(w, "about.page.html", &models.TemplateData{ + StringMap: stringMap, + }) } diff --git a/pkg/models/templateData.go b/pkg/models/templateData.go new file mode 100644 index 0000000..0119cd5 --- /dev/null +++ b/pkg/models/templateData.go @@ -0,0 +1,13 @@ +package models + +// TemplateData structure of the data of a template +type TemplateData struct { + StringMap map[string]string + IntMap map[string]int + FloatMap map[string]float32 + Data map[string]interface{} + CSRFToken string + Flash string + Warning string + Error string +} diff --git a/pkg/render/render.go b/pkg/render/render.go index f375f46..49e0b4a 100644 --- a/pkg/render/render.go +++ b/pkg/render/render.go @@ -1,20 +1,48 @@ package render import ( + "bytes" "fmt" "html/template" + "log" "net/http" "path/filepath" + + "github.com/jbrainz/go-web/pkg/config" + "github.com/jbrainz/go-web/pkg/models" ) var functions = template.FuncMap{} +var app *config.AppConfig + +// NewTemplate creates a new template. +func NewTemplate(a *config.AppConfig) { + app = a +} + // Template renders and html template on the frontend -func Template(w http.ResponseWriter, html string) { - _, err := TemplateTest(w) +func Template(w http.ResponseWriter, html string, td *models.TemplateData) { + + var tc map[string]*template.Template + if app.UseCache { + tc = app.TemplateCache + } else { + tc, _ = CreateTemplateCache() + } + + t, ok := tc[html] + if !ok { + log.Fatal("Could not get template from cache") + } + + buff := new(bytes.Buffer) + + _ = t.Execute(buff, td) + + _, err := buff.WriteTo(w) if err != nil { - fmt.Println("Error Fetching template", err) - return + fmt.Println("Error writing template to the browser", err) } parsedTemplate, _ := template.ParseFiles("./templates/" + html) err = parsedTemplate.Execute(w, nil) @@ -24,8 +52,8 @@ func Template(w http.ResponseWriter, html string) { } } -// TemplateTest finds the base template file and renders -func TemplateTest(w http.ResponseWriter) (map[string]*template.Template, error) { +// CreateTemplateCache finds the base template cache as a map +func CreateTemplateCache() (map[string]*template.Template, error) { myCache := map[string]*template.Template{} pages, err := filepath.Glob("./templates/*.page.html") @@ -35,7 +63,6 @@ func TemplateTest(w http.ResponseWriter) (map[string]*template.Template, error) for _, page := range pages { name := filepath.Base(page) - fmt.Println("Page is currently", page) ts, err := template.New(name).Funcs(functions).ParseFiles(page) if err != nil { return myCache, err diff --git a/templates/about.page.html b/templates/about.page.html index e46b22e..6a1f26a 100644 --- a/templates/about.page.html +++ b/templates/about.page.html @@ -4,6 +4,7 @@
welcome to the 🅰 page
+welcome to the 🅰 from: {{index .StringMap "test"}}
welcome to the home page 😬 😀
+welcome to the home page 😬 😀