Skip to content

Commit

Permalink
chore: Extract routes to separate namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
goshatch committed Sep 30, 2024
1 parent 9e86725 commit db034ae
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 77 deletions.
84 changes: 7 additions & 77 deletions src/tools/ifs/parts.clj
Original file line number Diff line number Diff line change
@@ -1,98 +1,28 @@
;; ---------------------------------------------------------
;; tools.ifs.parts
;;
;; TODO: Provide a meaningful description of the project
;; Parts is a toolkit for therapists working with the Internal Family Systems
;; model (https://en.wikipedia.org/wiki/Internal_Family_Systems_Model). It
;; provides a tool for easy, collaborative parts mapping, which can be used to
;; facilitate conversations with clients during sessions.

;; ---------------------------------------------------------

(ns tools.ifs.parts
(:gen-class)
(:require
[com.brunobonacci.mulog :as mulog]
[org.httpkit.server :as server]
[reitit.ring :as ring]
[reitit.coercion.spec]
[reitit.swagger :as swagger]
[reitit.swagger-ui :as swagger-ui]
[ring.middleware.json :refer [wrap-json-body wrap-json-response]]
[ring.middleware.params :refer [wrap-params]]
[tools.ifs.parts.api.middleware :as middleware]
[tools.ifs.parts.db :as db]
[tools.ifs.parts.pages :as pages]
[tools.ifs.parts.api.auth :as auth]
[tools.ifs.parts.api.account :as account]
[tools.ifs.parts.waitlist :as waitlist]))
[tools.ifs.parts.routes :as routes]))

;; ---------------------------------------------------------
;; Application

(def prelaunch-app
(middleware/wrap-default-middlewares
(ring/ring-handler
(ring/router
[["/" {:get {:handler #(pages/home-page %)}}]
["/waitlist-signup" {:post {:handler #(waitlist/signup %)}}]]
{:data {:middleware [wrap-params
middleware/exception
middleware/logging
middleware/wrap-html-response]}}))))

;; TODO: We need to later figure out a way to combine HTML routes and API
;; routes.
;;
;; Currently, the main issue is that I cannot figure out a way to combine API
;; namespaces with different sets of middleware for each.
;;
;; For example, I want the /api namespace to have JSON-related middlewares, but
;; not the root namespace, which serves text/html instead.
;;
;; It is also entirely possible that API routes will be removed, and only HTML
;; routes will remain.
(def app
(middleware/wrap-default-middlewares
(ring/ring-handler
(ring/router
[["/swagger.json"
{:get {:no-doc true
:swagger {:info {:title "Parts API"
:description "API for Parts"}}
:handler (swagger/create-swagger-handler)}}]
["/api"
["/ping"
{:get {:swagger {:tags ["Utility"]}
:handler (fn [_] {:status 200 :body {:message "Pong!"}})}}]
["/auth" {:swagger {:tags ["Authentication"]}}
["/login"
{:post {:handler auth/login}}]
["/logout"
{:post {:handler auth/logout
:middleware [middleware/jwt-auth]}}]]
["/account" {:swagger {:tags ["Account"]}}
[""
{:get {:handler account/get-account}
:patch {:handler account/update-account}
:delete {:handler account/delete-account}
:middleware [middleware/jwt-auth]}]
["/register"
{:post {:handler account/register-account}}]]]
["/" {:get {:handler #(pages/home-page %)}}]]
{:data {:middleware [wrap-params
middleware/exception
middleware/logging
[wrap-json-body {:keywords? true}]
wrap-json-response
middleware/wrap-jwt-authentication]}})
(ring/routes
(swagger-ui/create-swagger-ui-handler
{:path "/swagger-ui"
:config {:validatorUrl nil
:operationsSorter "alpha"}})
(ring/create-default-handler)))))

(defn start-server
"Starts the web server"
[port]
(mulog/log ::starting-server :port port)
(server/run-server #'prelaunch-app {:port port}))
(server/run-server #'routes/prelaunch-app {:port port}))

(defn -main
"Entry point into the application via clojure.main -M"
Expand Down
79 changes: 79 additions & 0 deletions src/tools/ifs/parts/routes.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
(ns tools.ifs.parts.routes
(:require
[reitit.ring :as ring]
[reitit.swagger :as swagger]
[reitit.swagger-ui :as swagger-ui]
[ring.middleware.json :refer [wrap-json-body wrap-json-response]]
[ring.middleware.params :refer [wrap-params]]
[tools.ifs.parts.api.account :as account]
[tools.ifs.parts.api.auth :as auth]
[tools.ifs.parts.api.middleware :as middleware]
[tools.ifs.parts.pages :as pages]
[tools.ifs.parts.waitlist :as waitlist]))

(defn prelaunch-app
"Routes for the pre-launch version of the app"
[]
(middleware/wrap-default-middlewares
(ring/ring-handler
(ring/router
[["/" {:get {:handler #(pages/home-page %)}}]
["/waitlist-signup" {:post {:handler #(waitlist/signup %)}}]]
{:data {:middleware [wrap-params
middleware/exception
middleware/logging
middleware/wrap-html-response]}}))))

;; TODO: We need to later figure out a way to combine HTML routes and API
;; routes.
;;
;; Currently, the main issue is that I cannot figure out a way to combine API
;; namespaces with different sets of middleware for each.
;;
;; For example, I want the /api namespace to have JSON-related middlewares, but
;; not the root namespace, which serves text/html instead.
;;
;; It is also entirely possible that API routes will be removed, and only HTML
;; routes will remain.
(defn app
"Routes for the full application"
[]
(middleware/wrap-default-middlewares
(ring/ring-handler
(ring/router
[["/swagger.json"
{:get {:no-doc true
:swagger {:info {:title "Parts API"
:description "API for Parts"}}
:handler (swagger/create-swagger-handler)}}]
["/api"
["/ping"
{:get {:swagger {:tags ["Utility"]}
:handler (fn [_] {:status 200 :body {:message "Pong!"}})}}]
["/auth" {:swagger {:tags ["Authentication"]}}
["/login"
{:post {:handler auth/login}}]
["/logout"
{:post {:handler auth/logout
:middleware [middleware/jwt-auth]}}]]
["/account" {:swagger {:tags ["Account"]}}
[""
{:get {:handler account/get-account}
:patch {:handler account/update-account}
:delete {:handler account/delete-account}
:middleware [middleware/jwt-auth]}]
["/register"
{:post {:handler account/register-account}}]]]
["/" {:get {:handler #(pages/home-page %)}}]]
{:data {:middleware [wrap-params
middleware/exception
middleware/logging
[wrap-json-body {:keywords? true}]
wrap-json-response
middleware/wrap-jwt-authentication]}})
(ring/routes
(swagger-ui/create-swagger-ui-handler
{:path "/swagger-ui"
:config {:validatorUrl nil
:operationsSorter "alpha"}})
(ring/create-default-handler)))))

0 comments on commit db034ae

Please sign in to comment.