Skip to content

Instantly share code, notes, and snippets.

@sleepynate
Created October 5, 2011 00:05
Show Gist options
  • Save sleepynate/1263214 to your computer and use it in GitHub Desktop.
Save sleepynate/1263214 to your computer and use it in GitHub Desktop.
(ns tootr.core
(:use
[compojure.core :only (defroutes GET POST)]
[hiccup.page-helpers :only (html5)]
[hiccup.form-helpers :only (form-to label text-area submit-button)]
)
(:require [ring.adapter.jetty :as ring]
[compojure.handler :as handler]
[clojure.contrib.sql :as sql]
)
)
(def db-info {:classname "org.sqlite.JDBC" :subprotocol "sqlite" :subname "./db.sqlite"})
(defn get-from-db [query]
(sql/with-connection db-info (sql/with-query-results results [query] (into [] results)))
)
(defn get-all-toots []
(get-from-db "select * from toots"))
(defn layout [title & body]
(html5 [:head [:title title]]
[:body [:div {:id "content"} body]]
))
(defn show-toot [toot]
[:div {:class "toot"} [:h3 (:toot toot)] [:p {:style "font-size:x-small;"} (toot :user_id)]]
)
(defn make-toot [params]
(sql/with-connection db-info (sql/insert-values :toots [:toot :user_id] [(params :toot) 1] ))
)
(defn index [] (layout "tootr"
[:div {:id "tootform"}
(form-to [:post "/"]
(label "tootlabel" "what do wanna toot?")
(text-area "toot")
(submit-button "tootr it")
)
]
(map show-toot (get-all-toots))
))
(defroutes routes
"where we put routes for compojure"
(GET "/" [] (index))
(POST "/" {params :params} (make-toot params))
)
(def application (handler/site routes))
(defn start [port]
(ring/run-jetty (var application) {:port (or port 8080) :join? false}))
(defn -main []
(let [port (Integer/parseInt (or (System/getenv "PORT") "8080")) ] (start port)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment