Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. (ns org.schipplock.todoserver
  2. (:use compojure))
  3.  
  4. (require '(org.danlarkin [json :as json]))
  5. (use '[clojure.contrib.sql :as sql])
  6.  
  7. (def version "0.1")
  8. (def database (-> (json/decode-from-str (slurp "config.json")) :database))
  9. (def main-config (-> (json/decode-from-str (slurp "config.json")) :todoserver))
  10.  
  11. (defn now [] (str (java.sql.Timestamp. (.getTime (java.util.Date.)))))
  12. (defn log [type message] (println (str (now)"::"type": "message)))
  13.  
  14. (def dbc {:classname "org.postgresql.Driver"
  15. :subprotocol "postgresql"
  16. :subname (str "//" (database :host) ":5432/" (database :db))
  17. :user (database :user)
  18. :password (database :pass) })
  19.  
  20. (defn master-password-correct?
  21. [password]
  22. (if-not (empty? password) (= password (main-config :master-password)) false))
  23.  
  24. (defn user-exists?
  25. [username]
  26. (sql/with-connection dbc
  27. (if (> (count (:username (sql/with-query-results res
  28. ["select username from users where username=?" username]
  29. (first res)))) 0) true false)))
  30.  
  31. (defn user-id
  32. [username]
  33. (:id
  34. (sql/with-connection dbc
  35. (sql/with-query-results res
  36. ["select id from users where username=?" username]
  37. (first res)))))
  38.  
  39. (defn create-user
  40. [username password status rank]
  41. (sql/insert-values
  42. :users
  43. [:username :password :status :rank]
  44. [username password status rank]))
  45.  
  46. (defn timestamp [time-string] (java.sql.Timestamp/valueOf time-string))
  47.  
  48. (defn create-todo
  49. [user_id title body created_on created_by finish_on repeat]
  50. (sql/insert-values
  51. :todos
  52. [:user_id :title :body :created_on :created_by :finish_on :repeat]
  53. [user_id title body (timestamp created_on) created_by (timestamp finish_on) repeat]))
  54.  
  55. (defn show-todo-list
  56. [username]
  57. (sql/with-connection dbc
  58. (sql/with-query-results res ["select id, user_id, title, body, finish_on::text from todo_results(?)" (user-id username)]
  59. (into [] res))))
  60.  
  61. (defn update-todo [id attrib_map] (sql/update-values :todos ["id=?::integer" id] attrib_map))
  62.  
  63. (defroutes todoserver
  64. (GET "/"
  65. (html (json/encode-to-str {:todoserver {:version version}} :indent 2)))
  66. (GET "/create-user"
  67. (if (master-password-correct? (params :mp))
  68. (if-not (empty? (params :username))
  69. (if (false? (user-exists? (params :username)))
  70. (try
  71. (sql/with-connection dbc
  72. (sql/transaction
  73. (create-user (params :username) (params :password) "active" (params :rank))))
  74. (html "ok")
  75. (log "HINT" (str "user " (params :username) " successfully created"))
  76. (catch Exception _))
  77. (log "HINT" (str "user " (params :username) " already exists"))))))
  78. (GET "/create-todo"
  79. (if (master-password-correct? (params :mp))
  80. (if (every? #(-> % params empty? not) [:for :title :body :created_on :created_by :finish_on :repeat])
  81. (try
  82. (sql/with-connection dbc
  83. (sql/transaction
  84. (create-todo (user-id (params :for)) (params :title) (params :body) (params :created_on) (user-id (params :created_by)) (params :finish_on) (params :repeat))))
  85. (html "ok")
  86. (catch Exception _))
  87. (html "2:parameter list incomplete"))))
  88. (GET "/todo-done"
  89. (if (master-password-correct? (params :mp))
  90. (if-not (empty? (params :id))
  91. (try
  92. (sql/with-connection dbc
  93. (sql/transaction
  94. (update-todo (params :id) {:done true})))
  95. (html "ok")
  96. (log "HINT" (str "todo entry w. id " (params :id) " has been set to done = true"))
  97. (catch Exception _)))))
  98. (GET "/show-todo-list"
  99. (if (master-password-correct? (params :mp))
  100. (if-not (empty? (params :username))
  101. (html (json/encode-to-str {:todolist (show-todo-list (params :username))} :indent 2)))))
  102. (GET "/favicon.ico"
  103. (redirect-to "/fav.ico"))
  104. (GET "/*"
  105. (or (serve-file (params :*)) :next))
  106. (ANY "*"
  107. (page-not-found)))
  108.  
  109. (run-server {:port 8080}
  110. "/*" (servlet todoserver))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement