lets-go:5.4

This commit is contained in:
tamsin johnson 2024-01-25 15:06:28 -08:00
parent 0db8a9f74d
commit 3deb2022b8
4 changed files with 70 additions and 33 deletions

View File

@ -3,7 +3,6 @@ package main
import ( import (
"errors" "errors"
"fmt" "fmt"
"html/template"
"net/http" "net/http"
"strconv" "strconv"
@ -23,26 +22,11 @@ func (app *application) home(w http.ResponseWriter, r *http.Request) {
return return
} }
files := []string{
"./ui/html/base.tmpl",
"./ui/html/partials/nav.tmpl",
"./ui/html/pages/home.tmpl",
}
ts, err := template.ParseFiles(files...)
if err != nil {
app.serverError(w, r, err)
return
}
data := templateData{ data := templateData{
Snippets: snippets, Snippets: snippets,
} }
err = ts.ExecuteTemplate(w, "base", data) app.render(w, r, http.StatusOK, "home.tmpl", data)
if err != nil {
app.serverError(w, r, err)
}
} }
// snippetView ... // snippetView ...
@ -63,26 +47,11 @@ func (app *application) snippetView(w http.ResponseWriter, r *http.Request) {
return return
} }
files := []string{
"./ui/html/base.tmpl",
"./ui/html/partials/nav.tmpl",
"./ui/html/pages/view.tmpl",
}
ts, err := template.ParseFiles(files...)
if err != nil {
app.serverError(w, r, err)
return
}
data := templateData{ data := templateData{
Snippet: snippet, Snippet: snippet,
} }
err = ts.ExecuteTemplate(w, "base", data) app.render(w, r, http.StatusOK, "view.tmpl", data)
if err != nil {
app.serverError(w, r, err)
}
} }
// snippetCreate ... // snippetCreate ...

View File

@ -1,11 +1,34 @@
package main package main
import ( import (
"bytes"
"fmt"
"log/slog" "log/slog"
"net/http" "net/http"
"runtime/debug" "runtime/debug"
) )
// render ...
func (app *application) render(w http.ResponseWriter, r *http.Request, status int, page string, data templateData) {
ts, ok := app.templateCache[page]
if !ok {
err := fmt.Errorf("the template %s does not exist", page)
app.serverError(w, r, err)
return
}
buf := new(bytes.Buffer)
err := ts.ExecuteTemplate(buf, "base", data)
if err != nil {
app.serverError(w, r, err)
return
}
w.WriteHeader(status)
buf.WriteTo(w)
}
func (app *application) serverError(w http.ResponseWriter, r *http.Request, err error) { func (app *application) serverError(w http.ResponseWriter, r *http.Request, err error) {
var ( var (
method = r.Method method = r.Method

View File

@ -3,6 +3,7 @@ package main
import ( import (
"database/sql" "database/sql"
"flag" "flag"
"html/template"
"log/slog" "log/slog"
"net/http" "net/http"
"regexp" "regexp"
@ -17,6 +18,7 @@ import (
type application struct { type application struct {
logger *slog.Logger logger *slog.Logger
snippets *models.SnippetModel snippets *models.SnippetModel
templateCache map[string]*template.Template
} }
// main it's the snippetbox webapp // main it's the snippetbox webapp
@ -38,10 +40,17 @@ func main() {
defer db.Close() defer db.Close()
templateCache, err := newTemplateCache()
if err != nil {
logger.Error(err.Error())
os.Exit(1)
}
// setup the application // setup the application
app := &application{ app := &application{
logger: logger, logger: logger,
snippets: &models.SnippetModel{DB: db}, snippets: &models.SnippetModel{DB: db},
templateCache: templateCache,
} }
logger.Info("starting server", slog.String("addr", *addr)) logger.Info("starting server", slog.String("addr", *addr))

View File

@ -1,6 +1,9 @@
package main package main
import ( import (
"html/template"
"path/filepath"
"snippetbox.chaosfem.tw/internal/models" "snippetbox.chaosfem.tw/internal/models"
) )
@ -8,3 +11,36 @@ type templateData struct {
Snippet models.Snippet Snippet models.Snippet
Snippets []models.Snippet Snippets []models.Snippet
} }
// newTemplateCache ...
func newTemplateCache() (map[string]*template.Template, error) {
cache := map[string]*template.Template{}
pages, err := filepath.Glob("./ui/html/pages/*.tmpl")
if err != nil {
return nil, err
}
for _, page := range pages {
name := filepath.Base(page)
ts, err := template.ParseFiles("./ui/html/base.tmpl")
if err != nil {
return nil, err
}
ts, err = ts.ParseGlob("./ui/html/partials/*.tmpl")
if err != nil {
return nil, err
}
ts, err = ts.ParseFiles(page)
if err != nil {
return nil, err
}
cache[name] = ts
}
return cache, nil
}