ch13.1
This commit is contained in:
@@ -3,6 +3,8 @@ package main
|
|||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"gitea.local.lab/Lbenedar/snippetbox/ui"
|
||||||
|
|
||||||
"github.com/julienschmidt/httprouter"
|
"github.com/julienschmidt/httprouter"
|
||||||
"github.com/justinas/alice"
|
"github.com/justinas/alice"
|
||||||
)
|
)
|
||||||
@@ -14,8 +16,8 @@ func (app *application) routes() http.Handler {
|
|||||||
app.notFound(w)
|
app.notFound(w)
|
||||||
})
|
})
|
||||||
|
|
||||||
fileServer := http.FileServer(http.Dir("./ui/static/"))
|
fileServer := http.FileServer(http.FS(ui.Files))
|
||||||
router.Handler(http.MethodGet, "/static/*filepath", http.StripPrefix("/static", fileServer))
|
router.Handler(http.MethodGet, "/static/*filepath", fileServer)
|
||||||
|
|
||||||
dynamic := alice.New(app.sessionManager.LoadAndSave, noSurf, app.authenticate)
|
dynamic := alice.New(app.sessionManager.LoadAndSave, noSurf, app.authenticate)
|
||||||
|
|
||||||
|
|||||||
@@ -2,11 +2,13 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"html/template"
|
"html/template"
|
||||||
|
"io/fs"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gitea.local.lab/Lbenedar/snippetbox/internal/models"
|
"gitea.local.lab/Lbenedar/snippetbox/internal/models"
|
||||||
|
"gitea.local.lab/Lbenedar/snippetbox/ui"
|
||||||
"github.com/justinas/nosurf"
|
"github.com/justinas/nosurf"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -40,24 +42,20 @@ func (app *application) newTemplateData(r *http.Request) *templateData {
|
|||||||
func newTemplateCache() (map[string]*template.Template, error) {
|
func newTemplateCache() (map[string]*template.Template, error) {
|
||||||
cache := map[string]*template.Template{}
|
cache := map[string]*template.Template{}
|
||||||
|
|
||||||
pages, err := filepath.Glob("./ui/html/pages/*.tmpl")
|
pages, err := fs.Glob(ui.Files, "html/pages/*.tmpl")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, page := range pages {
|
for _, page := range pages {
|
||||||
name := filepath.Base(page)
|
name := filepath.Base(page)
|
||||||
ts, err := template.New(name).Funcs(functions).ParseFiles("./ui/html/base.tmpl")
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
ts, err = ts.ParseGlob("./ui/html/partials/nav.tmpl")
|
patterns := []string{
|
||||||
if err != nil {
|
"html/base.tmpl",
|
||||||
return nil, err
|
"html/partials/*.tmpl",
|
||||||
|
page,
|
||||||
}
|
}
|
||||||
|
ts, err := template.New(name).Funcs(functions).ParseFS(ui.Files, patterns...)
|
||||||
ts, err = ts.ParseFiles(page)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -74,6 +74,5 @@ func (m *UserModel) Exists(id int) (bool, error) {
|
|||||||
stmt := `SELECT EXISTS(SELECT true FROM users WHERE id = ?)`
|
stmt := `SELECT EXISTS(SELECT true FROM users WHERE id = ?)`
|
||||||
|
|
||||||
err := m.DB.QueryRow(stmt, id).Scan(&exists)
|
err := m.DB.QueryRow(stmt, id).Scan(&exists)
|
||||||
|
|
||||||
return exists, err
|
return exists, err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user