diff --git a/cmd/web/routes.go b/cmd/web/routes.go index 14b92c5..c22b316 100644 --- a/cmd/web/routes.go +++ b/cmd/web/routes.go @@ -3,6 +3,8 @@ package main import ( "net/http" + "gitea.local.lab/Lbenedar/snippetbox/ui" + "github.com/julienschmidt/httprouter" "github.com/justinas/alice" ) @@ -14,8 +16,8 @@ func (app *application) routes() http.Handler { app.notFound(w) }) - fileServer := http.FileServer(http.Dir("./ui/static/")) - router.Handler(http.MethodGet, "/static/*filepath", http.StripPrefix("/static", fileServer)) + fileServer := http.FileServer(http.FS(ui.Files)) + router.Handler(http.MethodGet, "/static/*filepath", fileServer) dynamic := alice.New(app.sessionManager.LoadAndSave, noSurf, app.authenticate) diff --git a/cmd/web/templates.go b/cmd/web/templates.go index b115051..ba74078 100644 --- a/cmd/web/templates.go +++ b/cmd/web/templates.go @@ -2,11 +2,13 @@ package main import ( "html/template" + "io/fs" "net/http" "path/filepath" "time" "gitea.local.lab/Lbenedar/snippetbox/internal/models" + "gitea.local.lab/Lbenedar/snippetbox/ui" "github.com/justinas/nosurf" ) @@ -40,24 +42,20 @@ func (app *application) newTemplateData(r *http.Request) *templateData { func newTemplateCache() (map[string]*template.Template, error) { 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 { return nil, err } for _, page := range pages { 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") - if err != nil { - return nil, err + patterns := []string{ + "html/base.tmpl", + "html/partials/*.tmpl", + page, } - - ts, err = ts.ParseFiles(page) + ts, err := template.New(name).Funcs(functions).ParseFS(ui.Files, patterns...) if err != nil { return nil, err } diff --git a/internal/models/users.go b/internal/models/users.go index 4927c2f..a65fa4d 100644 --- a/internal/models/users.go +++ b/internal/models/users.go @@ -74,6 +74,5 @@ func (m *UserModel) Exists(id int) (bool, error) { stmt := `SELECT EXISTS(SELECT true FROM users WHERE id = ?)` err := m.DB.QueryRow(stmt, id).Scan(&exists) - return exists, err } diff --git a/ui/efs.go b/ui/efs.go new file mode 100644 index 0000000..9acafcd --- /dev/null +++ b/ui/efs.go @@ -0,0 +1,8 @@ +package ui + +import ( + "embed" +) + +//go:embed "html" "static" +var Files embed.FS