Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (ns org.schipplock.todoserver
- (:use compojure))
- (require '(org.danlarkin [json :as json]))
- (use '[clojure.contrib.sql :as sql])
- (def version "0.1")
- (def database (-> (json/decode-from-str (slurp "config.json")) :database))
- (def main-config (-> (json/decode-from-str (slurp "config.json")) :todoserver))
- (defn now [] (str (java.sql.Timestamp. (.getTime (java.util.Date.)))))
- (defn log [type message] (println (str (now)"::"type": "message)))
- (def dbc {:classname "org.postgresql.Driver"
- :subprotocol "postgresql"
- :subname (str "//" (database :host) ":5432/" (database :db))
- :user (database :user)
- :password (database :pass) })
- (defn master-password-correct?
- [password]
- (if-not (empty? password) (= password (main-config :master-password)) false))
- (defn user-exists?
- [username]
- (sql/with-connection dbc
- (if (> (count (:username (sql/with-query-results res
- ["select username from users where username=?" username]
- (first res)))) 0) true false)))
- (defn user-id
- [username]
- (:id
- (sql/with-connection dbc
- (sql/with-query-results res
- ["select id from users where username=?" username]
- (first res)))))
- (defn create-user
- [username password status rank]
- (sql/insert-values
- :users
- [:username :password :status :rank]
- [username password status rank]))
- (defn timestamp [time-string] (java.sql.Timestamp/valueOf time-string))
- (defn create-todo
- [user_id title body created_on created_by finish_on repeat]
- (sql/insert-values
- :todos
- [:user_id :title :body :created_on :created_by :finish_on :repeat]
- [user_id title body (timestamp created_on) created_by (timestamp finish_on) repeat]))
- (defn show-todo-list
- [username]
- (sql/with-connection dbc
- (sql/with-query-results res ["select id, user_id, title, body, finish_on::text from todo_results(?)" (user-id username)]
- (into [] res))))
- (defn update-todo [id attrib_map] (sql/update-values :todos ["id=?::integer" id] attrib_map))
- (defroutes todoserver
- (GET "/"
- (html (json/encode-to-str {:todoserver {:version version}} :indent 2)))
- (GET "/create-user"
- (if (master-password-correct? (params :mp))
- (if-not (empty? (params :username))
- (if (false? (user-exists? (params :username)))
- (try
- (sql/with-connection dbc
- (sql/transaction
- (create-user (params :username) (params :password) "active" (params :rank))))
- (html "ok")
- (log "HINT" (str "user " (params :username) " successfully created"))
- (catch Exception _))
- (log "HINT" (str "user " (params :username) " already exists"))))))
- (GET "/create-todo"
- (if (master-password-correct? (params :mp))
- (if (every? #(-> % params empty? not) [:for :title :body :created_on :created_by :finish_on :repeat])
- (try
- (sql/with-connection dbc
- (sql/transaction
- (create-todo (user-id (params :for)) (params :title) (params :body) (params :created_on) (user-id (params :created_by)) (params :finish_on) (params :repeat))))
- (html "ok")
- (catch Exception _))
- (html "2:parameter list incomplete"))))
- (GET "/todo-done"
- (if (master-password-correct? (params :mp))
- (if-not (empty? (params :id))
- (try
- (sql/with-connection dbc
- (sql/transaction
- (update-todo (params :id) {:done true})))
- (html "ok")
- (log "HINT" (str "todo entry w. id " (params :id) " has been set to done = true"))
- (catch Exception _)))))
- (GET "/show-todo-list"
- (if (master-password-correct? (params :mp))
- (if-not (empty? (params :username))
- (html (json/encode-to-str {:todolist (show-todo-list (params :username))} :indent 2)))))
- (GET "/favicon.ico"
- (redirect-to "/fav.ico"))
- (GET "/*"
- (or (serve-file (params :*)) :next))
- (ANY "*"
- (page-not-found)))
- (run-server {:port 8080}
- "/*" (servlet todoserver))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement