~yerinalexey/gobin

af3b43dfc4ac678c36279eab024918ddc9b02018 — Alexey Yerin 1 year, 1 month ago 0a74cfa
feat: 404 page
2 files changed, 25 insertions(+), 3 deletions(-)

M main.go
A templates/not-found.html
M main.go => main.go +6 -3
@@ 99,10 99,8 @@ func main() {
		err = pool.QueryRow(context.Background(), "select contents from pastes where id=$1", id).Scan(&contents)

		if err != nil {
			// TODO: Add proper 404 page
			w.WriteHeader(404)

			log.Printf("Paste #%d doesn't exist: %v\n", id, err)
			tmpl.ExecuteTemplate(w, "not-found", nil)

			return
		}


@@ 110,6 108,11 @@ func main() {
		tmpl.ExecuteTemplate(w, "paste", Paste{id, contents})
	})

	r.PathPrefix("/").HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
			w.WriteHeader(404)
			tmpl.ExecuteTemplate(w, "not-found", nil)
	})

	// Static files
	fs := http.FileServer(http.Dir("static/"))
	r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", fs))

A templates/not-found.html => templates/not-found.html +19 -0
@@ 0,0 1,19 @@
{{ define "not-found" }}
<!DOCTYPE html>
<html>
  <head>
    {{ template "head" }}
  </head>
  <body>
    {{ template "header" }}

    <main>
      <h1>Oops...</h1>
      <p>
        The page you're looking for doesn't exist. Would you like to go to
        <a class="link" href="/">homepage</a> instead?
      </p>
    </main>
  </body>
</html>
{{ end }}