Skip to content

Commit

Permalink
Add web service
Browse files Browse the repository at this point in the history
  • Loading branch information
rm-hull committed Mar 21, 2016
1 parent fcf6b79 commit 5610ec4
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
17 changes: 16 additions & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,34 @@
:name "The MIT License (MIT)"
:url "http://opensource.org/licenses/MIT"}
:dependencies [
[org.clojure/clojure "1.8.0"]]
[org.clojure/clojure "1.8.0"]
[org.clojure/data.json "0.2.6"]
[org.clojure/data.csv "0.1.3"]
[com.taoensso/timbre "4.3.1"]
[compojure "1.5.0"]
[ring "1.4.0"]
[hiccup "1.0.5"]
[ring-logger-timbre "0.7.5"]
[metrics-clojure-ring "2.6.1"]]
:scm {:url "git@github.com:rm-hull/ars-magna.git"}
:ring {
:handler ars-magna.handler/app }
:plugins [
[lein-ring "0.9.7"]
[codox "0.9.1"] ]
:source-paths ["src"]
:jar-exclusions [#"(?:^|/).git"]
:uberjar-exclusions [#"\.SF" #"\.RSA" #"\.DSA"]
:resouce-paths [
"resouces" ]
:codox {
:sources ["src"]
:output-dir "doc/api"
:src-dir-uri "http://github.com/rm-hull/ars-magna/blob/master/"
:src-linenum-anchor-prefix "L" }
:min-lein-version "2.6.1"
:profiles {
:uberjar {:aot :all}
:dev {
:global-vars {*warn-on-reflection* true}
:dependencies [
Expand Down
36 changes: 36 additions & 0 deletions src/ars_magna/handler.clj
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)))
35 changes: 35 additions & 0 deletions src/ars_magna/json.clj
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)))

0 comments on commit 5610ec4

Please sign in to comment.