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

45 lines
1.2 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]]
[page :refer [include-js]]]))
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
[:head
[:title "Hello World"]
(include-js "/js/main.js")]
[:body
[:h3 "Grub"]]))
(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}))