Skip to content

Commit

Permalink
Merge pull request #70 from ieugen/master
Browse files Browse the repository at this point in the history
[#69] Added redirect-trailing-slash middleware + fix content-type for 4xx response
  • Loading branch information
yogthos authored Sep 13, 2022
2 parents 358c795 + 5d121db commit befb336
Showing 1 changed file with 18 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
(:require
[<<ns-name>>.web.middleware.core :as middleware]
[integrant.core :as ig]
[ring.util.http-response :as http-response]
[reitit.ring :as ring]
[reitit.swagger-ui :as swagger-ui]))

Expand All @@ -10,17 +11,23 @@
(ring/ring-handler
router
(ring/routes
(ring/create-resource-handler {:path "/"})
(when (some? api-path)
(swagger-ui/create-swagger-ui-handler {:path api-path
:url (str api-path "/swagger.json")}))
(ring/create-default-handler
{:not-found
(constantly {:status 404, :body "Page not found"})
:method-not-allowed
(constantly {:status 405, :body "Not allowed"})
:not-acceptable
(constantly {:status 406, :body "Not acceptable"})}))
;; Handle trailing slash in routes - add it + redirect to it
;; https://github.com/metosin/reitit/blob/master/doc/ring/slash_handler.md
(ring/redirect-trailing-slash-handler)
(ring/create-resource-handler {:path "/"})
(when (some? api-path)
(swagger-ui/create-swagger-ui-handler {:path api-path
:url (str api-path "/swagger.json")}))
(ring/create-default-handler
{:not-found
(constantly (-> {:status 404, :body "Page not found"}
(http-response/content-type "text/html")))
:method-not-allowed
(constantly (-> {:status 405, :body "Not allowed"}
(http-response/content-type "text/html")))
:not-acceptable
(constantly (-> {:status 406, :body "Not acceptable"}
(http-response/content-type "text/html")))}))
{:middleware [(middleware/wrap-base opts)]}))

(defmethod ig/init-key :router/routes
Expand Down

0 comments on commit befb336

Please # to comment.