Skip to content

Commit

Permalink
Mainified for use with leiningen init script
Browse files Browse the repository at this point in the history
  • Loading branch information
zk committed Aug 4, 2010
1 parent f0ee5c4 commit 01b3a16
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 34 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pom.xml
*jar
lib
classes
classes
init-script
6 changes: 5 additions & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)
55 changes: 23 additions & 32 deletions src/cd_wsapi/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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))

10 changes: 10 additions & 0 deletions src/main.clj
Original file line number Diff line number Diff line change
@@ -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)))

0 comments on commit 01b3a16

Please sign in to comment.