grub-fork/src/clj/grub/core.clj

72 lines
2.2 KiB
Clojure
Raw Normal View History

2013-07-24 17:39:02 +00:00
(ns grub.core
(:require [grub.websocket :as ws]
[grub.db :as db]
[grub.integration-test :as integration-test]
[ring.middleware.reload :as reload]
2013-09-05 09:39:10 +00:00
[ring.middleware.file :as file]
[ring.util.response :as resp]
[compojure.core :refer [defroutes GET POST]]
[compojure.handler :as handler]
2013-07-24 18:38:14 +00:00
[compojure.route :as route]
[org.httpkit.server :as httpkit]
2013-07-24 18:38:14 +00:00
[hiccup
[page :refer [html5]]
[page :refer [include-js include-css]]]))
2013-07-24 18:38:14 +00:00
2013-08-20 15:59:17 +00:00
(def js-file (atom "/js/grub_dev.js"))
2013-07-24 18:38:14 +00:00
(defn index-page []
(html5
2013-07-24 19:43:26 +00:00
[:head
[:title "Grub"]
[:meta {:name "viewport" :content "width=device-width, initial-scale=1.0"}]
2013-08-04 18:15:56 +00:00
(include-css "/css/bootstrap.css")
(include-css "/css/styles.css")]
[:body
2013-09-30 14:49:57 +00:00
(include-js "/js/jquery.js")
(include-js "/js/bootstrap.js")
2013-08-20 15:59:17 +00:00
(include-js @js-file)]))
2013-07-24 18:38:14 +00:00
(defroutes routes
(GET "/ws" [] ws/websocket-handler)
2013-07-24 18:38:14 +00:00
(GET "/" [] (index-page))
2013-09-05 09:39:10 +00:00
(GET "*/src/cljs/grub/:file" [file] (resp/file-response file {:root "src/cljs/grub"}))
(GET "/js/public/js/:file" [file] (resp/redirect (str "/js/" file)))
2013-07-24 18:38:14 +00:00
(route/files "/")
(route/not-found "<p>Page not found.</p>"))
2013-07-24 17:39:02 +00:00
2013-07-24 18:38:14 +00:00
(def app
(let [dev? true]
(if dev?
(reload/wrap-reload (handler/site #'routes) {:dirs ["src/clj"]})
(handler/site routes))))
2013-07-24 17:39:02 +00:00
(def default-port 3000)
(def integration-test-port 3456)
(defn start-server [port]
(println (str "Starting server on localhost:" port))
(httpkit/run-server app {:port port}))
(defn run-integration-test []
(let [stop-server (start-server integration-test-port)]
(integration-test/run integration-test-port)
(stop-server)))
(defn start-production-server []
(reset! js-file "/js/grub.js")
(let [db-chan (db/connect-production-database)]
(ws/pass-received-events-to-clients-and-db db-chan)
(start-server default-port)))
(defn start-development-server []
(let [db-chan (db/connect-development-database)]
(ws/pass-received-events-to-clients-and-db db-chan)
(start-server default-port)))
2013-07-24 18:38:14 +00:00
(defn -main [& args]
2013-08-20 15:59:17 +00:00
(cond
(some #(= % "integration") args) (run-integration-test)
(some #(= % "production") args) (start-production-server)
:else (start-development-server)))