/
responses.clj
43 lines (38 loc) · 1.09 KB
/
responses.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
(ns coast.responses
(:require [hiccup.core :as h]
[clojure.data.json :as json]))
(defn content-type [k]
(condp = k
:json {"Content-Type" "application/json"}
:xml {"Content-Type" "text/xml"}
{"Content-Type" "text/html"}))
(defn body [content-type bd]
(condp = (get content-type "Content-Type")
"text/html" (h/html bd)
"application/json" (json/write-str bd)))
(defn response
([status val ct headers]
(let [cont-type (content-type ct)
b (body cont-type val)]
{:status status
:body b
:headers (merge cont-type headers)}))
([status body ct]
(response status body ct {}))
([status body]
(response status body :html {})))
(defn redirect
([url flash]
{:status 302
:body ""
:headers {"Location" url
"Turbolinks-Location" url}
:flash flash})
([url]
(redirect url nil)))
(def ok (partial response 200))
(def bad-request (partial response 400))
(def unauthorized (partial response 401))
(def not-found (partial response 404))
(def forbidden (partial response 403))
(def internal-server-error (partial response 500))