Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions cmd/web/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -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
}
33 changes: 29 additions & 4 deletions pkg/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}
13 changes: 13 additions & 0 deletions pkg/models/templateData.go
Original file line number Diff line number Diff line change
@@ -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
}
41 changes: 34 additions & 7 deletions pkg/render/render.go
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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")
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions templates/about.page.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<div class="col">
<h1>This is the About page</h1>
<p>welcome to the &#x1F170; page</p>
<p>welcome to the &#x1F170; from: {{index .StringMap "test"}}</p>
</div>
</div>
</div>
Expand Down
1 change: 1 addition & 0 deletions templates/home.page.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<div class="col">
<h1>This is the Home page</h1>
<p class="text-secondary">welcome to the home page &#x1F62C; &#x1F600;</p>
<p class="text-secondary">welcome to the home page &#x1F62C; &#x1F600;</p>
</div>
</div>
</div>
Expand Down