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

49 lines
1.4 KiB
Clojure
Raw Normal View History

2013-07-24 17:39:02 +00:00
(ns grub.core
2013-07-24 18:38:14 +00:00
(:use [org.httpkit.server
:only [run-server with-channel on-receive send! websocket?]]
[compojure.handler :only [site]]
[compojure.core :only [defroutes GET POST]])
2013-07-24 17:39:02 +00:00
(:require [ring.middleware.reload :as reload]
2013-07-24 18:38:14 +00:00
[compojure.route :as route]
[hiccup
[page :refer [html5]]
2013-07-24 19:43:26 +00:00
[page :refer [include-js include-css]]]))
2013-07-24 18:38:14 +00:00
2013-07-24 17:39:02 +00:00
2013-07-24 18:38:14 +00:00
(defn async-handler [request]
(if-not (:websocket? request)
{:status 200 :body "WebSocket server"}
(with-channel request channel
(on-receive channel (fn [data]
(send! channel data)))
(send! channel {:status 200
:headers {"Content-Type" "text/plain"}
:body "Long polling?"}))))
(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"}]
(include-css "/css/bootstrap.css")
(include-css "/css/bootstrap-responsive.css")]
2013-07-24 18:38:14 +00:00
[:body
2013-07-24 19:43:26 +00:00
(include-js "http://code.jquery.com/jquery.js")
(include-js "/js/bootstrap.js")
(include-js "/js/main.js")]))
2013-07-24 18:38:14 +00:00
(defroutes routes
(GET "/ws" [] async-handler)
(GET "/" [] (index-page))
(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 (site #'routes))
(site routes))))
2013-07-24 17:39:02 +00:00
2013-07-24 18:38:14 +00:00
(defn -main [& args]
(run-server app {:port 8080}))