Advertisement
Guest User

Untitled

a guest
Jun 5th, 2021
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns ring-todo-list.core
  2.   (:require [muuntaja.core :as m]
  3.             [reitit.coercion.schema]
  4.             [reitit.ring.middleware.muuntaja :as muuntaja]
  5.             [reitit.ring :as reitit]
  6.             [reitit.ring.coercion :as coercion]
  7.             [reitit.swagger :as swagger]
  8.             [reitit.swagger-ui :as swagger-ui]
  9.             [ring.middleware.reload :refer [wrap-reload]]
  10.             [ring.middleware.cors :refer [wrap-cors]]
  11.             [ring.adapter.jetty :refer [run-jetty]]
  12.             [ring.util.response :as http]
  13.             [schema.core :as s]
  14.             [ring-todo-list.db :as db]
  15.             ))
  16.  
  17. ; TODO add oauth2
  18.  
  19. (def app
  20.   (let [conn (atom (db/db-connection!))
  21.         transform-for-view #(update % :_id str)]
  22.     (reitit/ring-handler
  23.       (reitit/router
  24.         [
  25.          ["" {:no-doc true}
  26.           ["/swagger.json"
  27.           {:get (swagger/create-swagger-handler)}]
  28.          ["/swagger-ui*"
  29.           {:get (swagger-ui/create-swagger-ui-handler {:url "/swagger.json"})}]]
  30.          ["/api/v1/todo-lists"
  31.           [""
  32.            {
  33.             :post
  34.             {
  35.              :middleware [#(wrap-cors %
  36.                                       :access-control-allow-origin [#".*"]
  37.                                       :access-control-allow-methods [:get :post])]
  38.              :summary "Create a new todo list"
  39.              :parameters {:body {:todo-list [{:id  s/Int :text s/Str}]}}
  40.              :responses {201 {:body {:_id s/Str :todo-list [{:id  s/Int :text s/Str}]}}}
  41.              :handler
  42.              (fn [{todo-list :body-params}]
  43.                (http/created
  44.                  ""
  45.                  (transform-for-view
  46.                    (db/insert-todo-list! @conn todo-list))))
  47.              }
  48.  
  49.             :get
  50.             {:summary "Get all the todo lists"
  51.              :responses {200 {:body [{:_id s/Str :todo-list [{:id  s/Int :text s/Str}]}]}}
  52.              :handler
  53.              (fn [_]
  54.                (http/response
  55.                  (map transform-for-view (db/get-all))))
  56.              }
  57.             }]
  58.  
  59.           ["/:id"
  60.            {
  61.             :get
  62.             {:parameters {:path {:id s/Str}}
  63.              :summary "Get specific todo lists"
  64.              :responses {200 {:body {:_id s/Str :todo-list [{:id  s/Int :text s/Str}]}}}
  65.              :handler
  66.                          (fn [{:keys [parameters]}]
  67.                            (let [id (-> parameters :path :id)]
  68.                              (http/response
  69.                                (transform-for-view
  70.                                  (db/get-one @conn id)))
  71.                              ))
  72.              }}]
  73.           ]]
  74.  
  75.  
  76.         {:data {
  77.                 :coercion reitit.coercion.schema/coercion
  78.                 :muuntaja   m/instance
  79.                 :middleware [muuntaja/format-middleware
  80.                              coercion/coerce-exceptions-middleware
  81.                              coercion/coerce-request-middleware
  82.                              coercion/coerce-response-middleware]
  83.                 }}))))
  84.  
  85. (defn -main [& args]
  86.   (run-jetty
  87.     (-> app
  88.         var
  89.         wrap-reload)
  90.     {:port 3000}))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement