-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
87 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
(ns ars-magna.handler | ||
(:require | ||
[clojure.string :as s] | ||
[compojure.handler :as handler] | ||
[compojure.route :as route] | ||
[compojure.core :refer [defroutes GET POST]] | ||
[ring.logger.timbre :as logger.timbre] | ||
[metrics.ring.expose :refer [expose-metrics-as-json]] | ||
[metrics.ring.instrument :refer [instrument]] | ||
[ars-magna.json :refer :all] | ||
[ars-magna.dict :refer :all] | ||
[ars-magna.solver :refer :all])) | ||
|
||
(defn clean [word] | ||
(-> | ||
word | ||
s/lower-case | ||
(s/replace #"\W" ""))) | ||
|
||
(defroutes app-routes | ||
(let [dict (load-word-list :en-GB) | ||
index (partition-by-word-length dict)] | ||
(GET "/search/:word" [word :as req] | ||
(json-exception-handler | ||
(to-json identity | ||
(let [min-size (Integer/parseInt (or (get-in req [:params :min]) "3"))] | ||
(sort | ||
(search index (clean word) min-size nil)))))))) | ||
|
||
(def app | ||
(-> | ||
app-routes | ||
(logger.timbre/wrap-with-logger) | ||
(expose-metrics-as-json) | ||
(instrument) | ||
(handler/api))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
(ns ars-magna.json | ||
(:require | ||
[ring.util.response :refer [response status content-type charset]] | ||
[clojure.data.json :as json] | ||
[taoensso.timbre :as timbre])) | ||
|
||
(defn- value-writer [key value] | ||
(if (instance? java.util.Date value) | ||
(str value) | ||
value)) | ||
|
||
(defn to-json [f & q] | ||
(-> | ||
(apply f q) | ||
(json/write-str :value-fn value-writer :key-fn name) | ||
(response) | ||
(content-type "application/json") | ||
(charset "UTF-8"))) | ||
|
||
(defn json-exception-handler* [f] | ||
(try | ||
(f) | ||
(catch IllegalArgumentException ile | ||
(-> | ||
(to-json identity {:error (.getMessage ile)}) | ||
(status 400))) | ||
(catch Exception e | ||
(do | ||
(timbre/error e) | ||
(-> | ||
(to-json identity {:error (.toString e)}) | ||
(status 500)))))) | ||
|
||
(defmacro json-exception-handler [& body] | ||
`(json-exception-handler* (^:once fn* [] ~@body))) |