Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Mainified for use with leiningen init script

  • Loading branch information...
commit 01b3a1662489bae15e1bd54677af1caa58b8fde1 1 parent f0ee5c4
@zk authored
Showing with 40 additions and 34 deletions.
  1. +2 −1  .gitignore
  2. +5 −1 project.clj
  3. +23 −32 src/cd_wsapi/core.clj
  4. +10 −0 src/main.clj
View
3  .gitignore
@@ -1,4 +1,5 @@
pom.xml
*jar
lib
-classes
+classes
+init-script
View
6 project.clj
@@ -6,4 +6,8 @@
[aleph "0.1.0-SNAPSHOT"]
[net.cgrand/moustache "1.0.0-SNAPSHOT"]
[org.danlarkin/clojure-json "1.1"]]
- :dev-dependencies [[leiningen/lein-swank "1.2.0-SNAPSHOT"]])
+ :dev-dependencies [[leiningen/lein-swank "1.2.0-SNAPSHOT"]
+ [leiningen-init-script "1.2.0-SNAPSHOT"]]
+ :lis-opts {:properties {:config-clj.env "prod"}
+ :redirect-output-to "/var/log/cd-wsapi.log"}
+ :main main)
View
55 src/cd_wsapi/core.clj
@@ -7,11 +7,12 @@
(def *server-port* 8080)
;;JSON Encoders
-(add-encoder java.util.Date
- (fn [#^java.util.Date date #^java.io.Writer writer
- #^String pad #^String current-indent
- #^String start-token-indent #^Integer indent-size]
- (.append writer (str start-token-indent \" date \"))))
+(add-encoder
+ java.util.Date
+ (fn [#^java.util.Date date #^java.io.Writer writer
+ #^String pad #^String current-indent
+ #^String start-token-indent #^Integer indent-size]
+ (.append writer (str start-token-indent \" date \"))))
;; Database
@@ -31,22 +32,18 @@
(fn [r]
{:status 200
:headers {"Content-Type" "application/json"}
- :body (encode-to-str (with-connection db
- (transaction
- (when-let [id (with-query-results rs ["select id from functions where ns = ? and name = ?" ns name] (:id (first (doall rs))))]
- (when-let [examples (with-query-results rs ["select * from examples where function_id = ?" id] (doall rs))]
- (if examples
- examples
- []))))))
- }))
-
-(let [ns "clojure.core"
- name "mapa"]
- (encode-to-str (with-connection db
- (transaction
- (when-let [id (with-query-results rs ["select id from functions where ns = ? and name = ?" ns name] (:id (first (doall rs))))]
- (when-let [examples (with-query-results rs ["select * from examples where function_id = ?" id] (doall rs))]
- examples))))))
+ :body (encode-to-str
+ (with-connection db
+ (transaction
+ (when-let [id (with-query-results
+ rs
+ ["select id from functions where ns = ? and name = ?" ns name]
+ (:id (first (doall rs))))]
+ (when-let [examples (with-query-results
+ rs
+ ["select * from examples where function_id = ?" id]
+ (doall rs))]
+ examples)))))}))
(defn app-handler [channel request]
(enqueue-and-close
@@ -60,17 +57,11 @@
(defn app-wrapper [channel request]
(app-handler channel request))
-(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)
-
-
+(comment (def server (start-http-server app-wrapper {:port *server-port*}))
-(def req {:remote-addr "127.0.0.1", :scheme :http, :query-string "asdf=qwer", :uri "/foo", :keep-alive? true, :server-port "8080", :server-name "localhost", :headers {"host" "localhost:8080", "connection" "keep-alive", "user-agent" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_3; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.125 Safari/533.4", "cache-control" "max-age=0", "accept" "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5", "accept-encoding" "gzip,deflate,sdch", "accept-language" "en-US,en;q=0.8", "accept-charset" "ISO-8859-1,utf-8;q=0.7,*;q=0.3"}, :request-method :get})
+ (defn restart-server []
+ (server)
+ (def server (start-http-server app-wrapper {:port *server-port*})))
-#_ ((app ["foo"] (partial app-handler nil)) req)
+ (restart-server))
View
10 src/main.clj
@@ -0,0 +1,10 @@
+(ns main
+ (:use [cd-wsapi.core]
+ [aleph core http])
+ (:gen-class))
+
+(defn -main [& args]
+ (start-http-server app-wrapper {:port 8080})
+ (loop []
+ (Thread/sleep 1000)
+ (recur)))
Please sign in to comment.
Something went wrong with that request. Please try again.