forked from dakrone/cd-wsapi
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add search method, display better example output.
Remove some of the extraneous output from the examples method. Added two search methods, one for searching across all namespaces, the other for searching inside a namespace.
- Loading branch information
Showing
1 changed file
with
82 additions
and
34 deletions.
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,67 +1,115 @@ | |||
(ns cd-wsapi.core | (ns cd-wsapi.core | ||
(:use [aleph core http] | (:use [aleph core http] | ||
[net.cgrand.moustache] | [net.cgrand.moustache] | ||
[clojure.contrib.sql] | [clojure.contrib.sql] | ||
[org.danlarkin.json])) | [org.danlarkin.json])) | ||
|
|
||
(def *server-port* 8080) | (def *server-port* 8080) | ||
|
|
||
;;JSON Encoders | ;;JSON Encoders | ||
(add-encoder | (add-encoder | ||
java.util.Date | java.util.Date | ||
(fn [#^java.util.Date date #^java.io.Writer writer | (fn [#^java.util.Date date #^java.io.Writer writer | ||
#^String pad #^String current-indent | #^String pad #^String current-indent | ||
#^String start-token-indent #^Integer indent-size] | #^String start-token-indent #^Integer indent-size] | ||
(.append writer (str start-token-indent \" date \")))) | (.append writer (str start-token-indent \" date \")))) | ||
|
|
||
;; Database | ;; Database | ||
|
|
||
(def db {:classname "com.mysql.jdbc.Driver" | (def db {:classname "com.mysql.jdbc.Driver" | ||
:subprotocol "mysql" | :subprotocol "mysql" | ||
:subname "//localhost:3306/clojuredocs?user=cd_wsapi&password=cd_wsapi" | :subname "//localhost:3306/clojuredocs?user=cd_wsapi&password=cd_wsapi" | ||
:create true | :create true | ||
:username "cd_wsapi" | :username "cd_wsapi" | ||
:password "cd_wsapi"}) | :password "cd_wsapi"}) | ||
|
|
||
(defn default [request] | (defn default [request] | ||
{:status 200 | {:status 200 | ||
:headers {"Content-Type" "application/json"} | :headers {"Content-Type" "application/json"} | ||
:body "null"}) | :body "null"}) | ||
|
|
||
|
|||
(defn format-example | |||
"Given an example, format the example for the API JSON output." | |||
[example] | |||
(dissoc (into {} example) :id :function_id :user_id)) | |||
|
|||
|
|||
(defn examples [ns name] | (defn examples [ns name] | ||
(fn [r] | (fn [r] | ||
{:status 200 | {:status 200 | ||
:headers {"Content-Type" "application/json"} | :headers {"Content-Type" "application/json"} | ||
:body (encode-to-str | :body (encode-to-str | ||
(with-connection db | (with-connection db | ||
(transaction | (transaction | ||
(when-let [id (with-query-results | (when-let [id (with-query-results | ||
rs | rs | ||
["select id from functions where ns = ? and name = ?" ns name] | ["select id from functions where ns = ? and name = ?" ns name] | ||
(:id (first (doall rs))))] | (:id (first (doall rs))))] | ||
(when-let [examples (with-query-results | (when-let [examples (with-query-results | ||
rs | rs | ||
["select * from examples where function_id = ?" id] | ["select * from examples where function_id = ?" id] | ||
(doall rs))] | (doall rs))] | ||
examples)))))})) | (map format-example examples))))))})) | ||
|
|||
|
|||
; Doesn't do anything yet, but it might in the future. | |||
(defn format-search | |||
"Given a function result set, format the function for the API JSON output." | |||
[function] | |||
function) | |||
|
|||
|
|||
; TODO: reduce code duplication smell. | |||
(defn search | |||
([name] | |||
(fn [n] | |||
{:status 200 | |||
:headers {"Content-Type" "application/json"} | |||
:body (encode-to-str | |||
(with-connection db | |||
(transaction | |||
(when-let [functions (with-query-results | |||
rs | |||
; There is definitely a better way to do this. | |||
[(str "select id,name,ns from functions where name like '%" name "%'")] | |||
(doall rs))] | |||
(map format-search functions)))))})) | |||
([ns name] | |||
(fn [n] | |||
{:status 200 | |||
:headers {"Content-Type" "application/json"} | |||
:body (encode-to-str | |||
(with-connection db | |||
(transaction | |||
(when-let [functions (with-query-results | |||
rs | |||
; There is definitely a better way to do this. | |||
[(str "select id,name,ns from functions where ns = ? and name like '%" name "%'") ns] | |||
(doall rs))] | |||
(map format-search functions)))))}))) | |||
|
|||
|
|
||
(defn app-handler [channel request] | (defn app-handler [channel request] | ||
(enqueue-and-close | (enqueue-and-close | ||
channel | channel | ||
((app | ((app | ||
["stuff"] examples | ["stuff"] examples | ||
["examples" ns name] (examples ns name) | ["examples" ns name] (examples ns name) | ||
[&] default) | ["search" ns name] (search ns name) | ||
request))) | ["search" name] (search name) | ||
[&] default) | |||
request))) | |||
|
|
||
(defn app-wrapper [channel request] | (defn app-wrapper [channel request] | ||
(app-handler channel request)) | (app-handler channel request)) | ||
|
|
||
(comment (def server (start-http-server app-wrapper {:port *server-port*})) | (comment (def server (start-http-server app-wrapper {:port *server-port*})) | ||
|
|
||
(defn restart-server [] | |||
(server) | |||
(def server (start-http-server app-wrapper {:port *server-port*}))) | |||
|
|
||
(restart-server)) | (defn restart-server [] | ||
(server) | |||
(def server (start-http-server app-wrapper {:port *server-port*}))) | |||
|
|||
(restart-server)) | |||
|
|