Serve 404 page when none route matches

This commit is contained in:
Marcin Kulik 2017-02-28 18:35:32 +01:00
parent 1e08b39e49
commit 4fe17fbc82
2 changed files with 13 additions and 9 deletions

View File

@ -7,7 +7,7 @@
[user-database :as udb]]
[asciinema.model.asciicast :as asciicast]
[asciinema.util.io :refer [with-tmp-dir]]
[asciinema.yada :refer [resource]]
[asciinema.yada :refer [resource not-found-model]]
[clj-time.core :as t]
[clojure.java
[io :as io]
@ -103,4 +103,4 @@
(defn asciicasts-endpoint [{:keys [db file-store exp-set executor]}]
["" [["/a/" [[[:token ".json"] (asciicast-json-resource db file-store)]
[[:token ".png"] (asciicast-png-resource db file-store exp-set executor)]]]
[true (yada/as-resource nil)]]])
[true (yada/resource not-found-model)]]])

View File

@ -2,14 +2,18 @@
(:require [clojure.java.io :as io]
[yada.yada :as yada]))
(defn not-found-response [ctx]
(case (yada/content-type ctx)
(def not-found-model
{:produces
#{"text/html" "text/plain"}
:response
(fn [ctx]
(assoc (:response ctx)
:status 404
:body (case (yada/content-type ctx)
"text/html" (io/input-stream (io/resource "asciinema/errors/404.html"))
"Not found"))
"Not found")))})
(defn resource [model]
(-> model
(update-in [:responses 404] #(or %
{:produces #{"text/html" "text/plain"}
:response not-found-response}))
(update-in [:responses 404] #(or % not-found-model))
yada/resource))