lets-go:6.2 middleware
This commit is contained in:
parent
3deb2022b8
commit
8678a04350
@ -22,9 +22,8 @@ func (app *application) home(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
data := templateData{
|
data := app.newTemplateData(r)
|
||||||
Snippets: snippets,
|
data.Snippets = snippets
|
||||||
}
|
|
||||||
|
|
||||||
app.render(w, r, http.StatusOK, "home.tmpl", data)
|
app.render(w, r, http.StatusOK, "home.tmpl", data)
|
||||||
}
|
}
|
||||||
@ -47,9 +46,8 @@ func (app *application) snippetView(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
data := templateData{
|
data := app.newTemplateData(r)
|
||||||
Snippet: snippet,
|
data.Snippet = snippet
|
||||||
}
|
|
||||||
|
|
||||||
app.render(w, r, http.StatusOK, "view.tmpl", data)
|
app.render(w, r, http.StatusOK, "view.tmpl", data)
|
||||||
}
|
}
|
||||||
|
@ -6,8 +6,17 @@ import (
|
|||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// newTemplateData ...
|
||||||
|
func (app *application )newTemplateData(r *http.Request) templateData {
|
||||||
|
return templateData{
|
||||||
|
CurrentYear: time.Now().Year(),
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// render ...
|
// render ...
|
||||||
func (app *application) render(w http.ResponseWriter, r *http.Request, status int, page string, data templateData) {
|
func (app *application) render(w http.ResponseWriter, r *http.Request, status int, page string, data templateData) {
|
||||||
ts, ok := app.templateCache[page]
|
ts, ok := app.templateCache[page]
|
||||||
|
18
snippetbox/cmd/web/middleware.go
Normal file
18
snippetbox/cmd/web/middleware.go
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
func secureHeaders(next http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("Content-Security-Policy",
|
||||||
|
"default-src 'self'; style-src 'self' fonts.googleapis.com; font-src fonts.gstatic.com")
|
||||||
|
w.Header().Set("Referrer-Policy", "origin-when-cross-origin")
|
||||||
|
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||||
|
w.Header().Set("X-Frame-Options", "deny")
|
||||||
|
w.Header().Set("X-XSS-Protection", "0")
|
||||||
|
|
||||||
|
next.ServeHTTP(w, r)
|
||||||
|
})
|
||||||
|
}
|
@ -5,7 +5,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// routes ...
|
// routes ...
|
||||||
func (app *application) routes() *http.ServeMux {
|
func (app *application) routes() http.Handler {
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
|
|
||||||
// setup server for static files
|
// setup server for static files
|
||||||
@ -16,5 +16,5 @@ func (app *application) routes() *http.ServeMux {
|
|||||||
mux.HandleFunc("/snippet/view", app.snippetView)
|
mux.HandleFunc("/snippet/view", app.snippetView)
|
||||||
mux.HandleFunc("/snippet/create", app.snippetCreate)
|
mux.HandleFunc("/snippet/create", app.snippetCreate)
|
||||||
|
|
||||||
return mux
|
return secureHeaders(mux)
|
||||||
}
|
}
|
||||||
|
@ -3,15 +3,26 @@ package main
|
|||||||
import (
|
import (
|
||||||
"html/template"
|
"html/template"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"time"
|
||||||
|
|
||||||
"snippetbox.chaosfem.tw/internal/models"
|
"snippetbox.chaosfem.tw/internal/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
type templateData struct {
|
type templateData struct {
|
||||||
|
CurrentYear int
|
||||||
Snippet models.Snippet
|
Snippet models.Snippet
|
||||||
Snippets []models.Snippet
|
Snippets []models.Snippet
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// humanDate ...
|
||||||
|
func humanDate(t time.Time) string {
|
||||||
|
return t.Format("02 Jan 2006 at 15:04")
|
||||||
|
}
|
||||||
|
|
||||||
|
var functions = template.FuncMap{
|
||||||
|
"humanDate": humanDate,
|
||||||
|
}
|
||||||
|
|
||||||
// newTemplateCache ...
|
// newTemplateCache ...
|
||||||
func newTemplateCache() (map[string]*template.Template, error) {
|
func newTemplateCache() (map[string]*template.Template, error) {
|
||||||
cache := map[string]*template.Template{}
|
cache := map[string]*template.Template{}
|
||||||
@ -24,7 +35,7 @@ func newTemplateCache() (map[string]*template.Template, error) {
|
|||||||
for _, page := range pages {
|
for _, page := range pages {
|
||||||
name := filepath.Base(page)
|
name := filepath.Base(page)
|
||||||
|
|
||||||
ts, err := template.ParseFiles("./ui/html/base.tmpl")
|
ts, err := template.New(name).Funcs(functions).ParseFiles("./ui/html/base.tmpl")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,9 @@
|
|||||||
<main>
|
<main>
|
||||||
{{template "main" .}}
|
{{template "main" .}}
|
||||||
</main>
|
</main>
|
||||||
<footer>Powered by <a href="https://golang.org">Go</a></footer>
|
<footer>
|
||||||
|
Powered by <a href="https://golang.org">Go</a> in {{.CurrentYear}}
|
||||||
|
</footer>
|
||||||
<script src="/static/js/main.js" type="text/javascript"></script>
|
<script src="/static/js/main.js" type="text/javascript"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
{{range .Snippets}}
|
{{range .Snippets}}
|
||||||
<tr>
|
<tr>
|
||||||
<td><a href='/snippet/view?id={{.ID}}'>{{.Title}}</td>
|
<td><a href='/snippet/view?id={{.ID}}'>{{.Title}}</td>
|
||||||
<td>{{.Created}}</td>
|
<td>{{humanDate .Created}}</td>
|
||||||
<td>{{.ID}}</td>
|
<td>{{.ID}}</td>
|
||||||
</tr>
|
</tr>
|
||||||
{{else}}
|
{{else}}
|
||||||
|
@ -10,8 +10,8 @@
|
|||||||
|
|
||||||
<pre><code>{{.Content}}</code></pre>
|
<pre><code>{{.Content}}</code></pre>
|
||||||
<div class="metadata">
|
<div class="metadata">
|
||||||
<time>Created: {{.Created}}</time>
|
<time>Created: {{humanDate .Created}}</time>
|
||||||
<time>Expires: {{.Expires}}</time>
|
<time>Expires: {{humanDate .Expires}}</time>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
Loading…
Reference in New Issue
Block a user