-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9a4a3d4
commit 6fa7644
Showing
3 changed files
with
95 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,28 @@ | ||
{:paths ["src" "resources"] | ||
:deps {org.clojure/clojure {:mvn/version "1.11.2"} | ||
com.github.parenthesin/components {:mvn/version "0.2.5" | ||
:deps {org.clojure/clojure {:mvn/version "1.11.3"} | ||
com.github.parenthesin/components {:mvn/version "0.3.0" | ||
:exclusions [prismatic/schema]} | ||
com.github.seancorfield/honeysql {:mvn/version "2.6.1126"} | ||
metosin/malli {:mvn/version "0.15.0"} | ||
metosin/reitit-swagger {:mvn/version "0.6.0"} | ||
com.github.seancorfield/honeysql {:mvn/version "2.6.1147"} | ||
metosin/malli {:mvn/version "0.16.2"} | ||
org.postgresql/postgresql {:mvn/version "42.7.3"}} | ||
:aliases | ||
{:dev {:extra-paths ["test" "dev"]} | ||
:test {:extra-paths ["test"] | ||
:extra-deps {org.clojars.bigsy/pg-embedded-clj {:mvn/version "1.0.1"} | ||
lambdaisland/kaocha {:mvn/version "1.88.1376"} | ||
lambdaisland/kaocha {:mvn/version "1.91.1392"} | ||
lambdaisland/kaocha-cloverage {:mvn/version "1.1.89"} | ||
nubank/matcher-combinators {:mvn/version "3.9.1"} | ||
nubank/state-flow {:mvn/version "5.15.0"}} | ||
nubank/state-flow {:mvn/version "5.17.0"}} | ||
:main-opts ["-m" "kaocha.runner"]} | ||
|
||
:clojure-lsp {:replace-deps {com.github.clojure-lsp/clojure-lsp-standalone {:mvn/version "2024.03.13-13.11.00"}} | ||
:clojure-lsp {:replace-deps {com.github.clojure-lsp/clojure-lsp-standalone {:mvn/version "2024.04.22-11.50.26"}} | ||
:main-opts ["-m" "clojure-lsp.main"]} | ||
|
||
:nrepl {:extra-deps {cider/cider-nrepl {:mvn/version "0.47.1"}} | ||
:nrepl {:extra-deps {cider/cider-nrepl {:mvn/version "0.49.1"}} | ||
:main-opts ["-m" "nrepl.cmdline" "--middleware" "[cider.nrepl/cider-middleware]"]} | ||
|
||
:migratus {:main-opts ["-m" "parenthesin.helpers.migrations"]} | ||
|
||
:build {:deps {io.github.clojure/tools.build {:mvn/version "0.10.0"}} | ||
:build {:deps {io.github.clojure/tools.build {:mvn/version "0.10.4"}} | ||
:ns-default build | ||
:exec-args {:uber-file "target/service.jar"}}}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
(ns reitit-malli | ||
(:require [com.stuartsierra.component :as component] | ||
[muuntaja.core :as m] | ||
[parenthesin.helpers.logs :as logs] | ||
[reitit.coercion.malli :as reitit.malli] | ||
[reitit.dev.pretty :as pretty] | ||
[reitit.http :as http] | ||
[reitit.http.coercion :as coercion] | ||
[reitit.http.interceptors.exception :as exception] | ||
[reitit.http.interceptors.multipart :as multipart] | ||
[reitit.http.interceptors.muuntaja :as muuntaja] | ||
[reitit.http.interceptors.parameters :as parameters] | ||
[reitit.pedestal :as pedestal] | ||
[reitit.ring :as ring] | ||
[reitit.swagger :as swagger] | ||
[reitit.swagger-ui :as swagger-ui])) | ||
|
||
(defn- coercion-error-handler [status] | ||
(fn [exception _request] | ||
(logs/log :error exception :coercion-errors (:errors (ex-data exception))) | ||
{:status status | ||
:body (if (= 400 status) | ||
(str "Invalid path or request parameters, with the following errors: " | ||
(:errors (ex-data exception))) | ||
"Error checking path or request parameters.")})) | ||
|
||
(defn- exception-info-handler [exception _request] | ||
(logs/log :error exception "Server exception:" :exception exception) | ||
{:status 500 | ||
:body "Internal error."}) | ||
|
||
(def router-settings | ||
{;:reitit.interceptor/transform dev/print-context-diffs ;; pretty context diffs | ||
;;:validate spec/validate ;; enable spec validation for route data | ||
;;:reitit.spec/wrap spell/closed ;; strict top-level validation | ||
:exception pretty/exception | ||
:data {:coercion (reitit.malli/create (assoc reitit.malli/default-options :lite false)) | ||
:muuntaja (m/create | ||
(-> m/default-options | ||
(assoc-in [:formats "application/json" :decoder-opts :bigdecimals] true))) | ||
:interceptors [;; swagger feature | ||
swagger/swagger-feature | ||
;; query-params & form-params | ||
(parameters/parameters-interceptor) | ||
;; content-negotiation | ||
(muuntaja/format-negotiate-interceptor) | ||
;; encoding response body | ||
(muuntaja/format-response-interceptor) | ||
;; exception handling | ||
(exception/exception-interceptor | ||
(merge | ||
exception/default-handlers | ||
{:reitit.coercion/request-coercion (coercion-error-handler 400) | ||
:reitit.coercion/response-coercion (coercion-error-handler 500) | ||
clojure.lang.ExceptionInfo exception-info-handler})) | ||
;; decoding request body | ||
(muuntaja/format-request-interceptor) | ||
;; coercing response bodys | ||
(coercion/coerce-response-interceptor) | ||
;; coercing request parameters | ||
(coercion/coerce-request-interceptor) | ||
;; multipart | ||
(multipart/multipart-interceptor)]}}) | ||
|
||
(defn router [routes] | ||
(pedestal/routing-interceptor | ||
(http/router routes router-settings) | ||
;; optional default ring handler (if no routes have matched) | ||
(ring/routes | ||
(swagger-ui/create-swagger-ui-handler | ||
{:path "/" | ||
:config {:validatorUrl nil | ||
:operationsSorter "alpha"}}) | ||
(ring/create-resource-handler) | ||
(ring/create-default-handler)))) | ||
|
||
(defrecord Router [router] | ||
component/Lifecycle | ||
(start [this] this) | ||
(stop [this] this)) | ||
|
||
(defn new-router | ||
[routes] | ||
(map->Router {:router (router routes)})) |