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]] [user-database :as udb]]
[asciinema.model.asciicast :as asciicast] [asciinema.model.asciicast :as asciicast]
[asciinema.util.io :refer [with-tmp-dir]] [asciinema.util.io :refer [with-tmp-dir]]
[asciinema.yada :refer [resource]] [asciinema.yada :refer [resource not-found-model]]
[clj-time.core :as t] [clj-time.core :as t]
[clojure.java [clojure.java
[io :as io] [io :as io]
@ -103,4 +103,4 @@
(defn asciicasts-endpoint [{:keys [db file-store exp-set executor]}] (defn asciicasts-endpoint [{:keys [db file-store exp-set executor]}]
["" [["/a/" [[[:token ".json"] (asciicast-json-resource db file-store)] ["" [["/a/" [[[:token ".json"] (asciicast-json-resource db file-store)]
[[:token ".png"] (asciicast-png-resource db file-store exp-set executor)]]] [[: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] (:require [clojure.java.io :as io]
[yada.yada :as yada])) [yada.yada :as yada]))
(defn not-found-response [ctx] (def not-found-model
(case (yada/content-type ctx) {:produces
"text/html" (io/input-stream (io/resource "asciinema/errors/404.html")) #{"text/html" "text/plain"}
"Not found")) :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")))})
(defn resource [model] (defn resource [model]
(-> model (-> model
(update-in [:responses 404] #(or % (update-in [:responses 404] #(or % not-found-model))
{:produces #{"text/html" "text/plain"}
:response not-found-response}))
yada/resource)) yada/resource))