about summary refs log tree commit diff
path: root/src/pronouns/web.clj
blob: 24588c0a7a90bb6aff7803b6a6b73787e0f5db21 (plain) (blame)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
(ns pronouns.web
  (:require [compojure.core :refer [defroutes GET PUT POST DELETE ANY]]
            [compojure.handler :refer [site]]
            [compojure.route :as route]
            [clojure.java.io :as io]
            [clojure.string :as s]
            [ring.middleware.logger :as logger]
            [ring.middleware.stacktrace :as trace]
            [ring.middleware.session :as session]
            [ring.middleware.session.cookie :as cookie]
            [ring.adapter.jetty :as jetty]
            [environ.core :refer [env]]))

(def config {:default-server-port 5000})

(defn render-examples-page
  [subject object possessive-determiner possessive-pronoun reflexive]
  (s/join "\n"
          [(str subject " went to the park")
           (str "I went with " object)
           (str subject " brought " possessive-determiner " frisbee")
           (str "at least I think it was " possessive-pronoun)
           (str subject " threw it to " reflexive)]))

(defroutes app-routes
  (GET "/" []
       {:status 200
        :headers {"Content-Type" "text/plain"}
        :body "a blurb explaining how to use this site"})
  (GET "/:subject/:object/:possessive-determiner/:possessive-pronoun/:reflexive" {params :params}
       {:status 200
        :headers {"Content-Type" "text/plain"}
        ;; ew this is super gross there's certainly a better way
        :body (let [{:keys
                     [subject object possessive-determiner possessive-pronoun reflexive]}
                    params]
                (render-examples-page subject
                                      object
                                      possessive-determiner
                                      possessive-pronoun
                                      reflexive))})
  (ANY "*" []
       (route/not-found (slurp (io/resource "404.html")))))

(defn wrap-error-page [handler]
  (fn [req]
    (try (handler req)
         (catch Exception e
           {:status 500
            :headers {"Content-Type" "text/html"}
            :body (slurp (io/resource "500.html"))}))))

(def app
  (-> app-routes
      logger/wrap-with-logger
      wrap-error-page
      trace/wrap-stacktrace))

(defn -main []
  (let [port (Integer. (:port env
                              (:default-server-port config)))]
    (jetty/run-jetty app {:port port})))

;; For interactive development:
;; (.stop server)
;; (def server (-main))