Move hardcoded values for OpenAPI to config

This commit is contained in:
2026-02-01 15:27:26 +01:00
parent a569c30771
commit 1b1bdfbd04
2 changed files with 12 additions and 9 deletions

View File

@@ -19,11 +19,11 @@
(ring/router
(conj api-routes
;; Add OpenAPI description to the default API routes
["/openapi.json" {:get (openapi/create-openapi-handler)
:no-doc true
:openapi {:info {:title "Yo-ho-ho API"
:description "A take-home assignment for HolidayPirates"
:version "0.42.0"}}}])
[(:json-url (:apidoc config)) {:get (openapi/create-openapi-handler)
:no-doc true
:openapi {:info {:title "Yo-ho-ho API"
:description "A take-home assignment for HolidayPirates"
:version "0.42.0"}}}])
;; Middlewares:
;; - wrap-content-type-json: ensure POST routes are sent JSON payload
@@ -39,7 +39,8 @@
(ring/routes
;; Add Swagger UI
(swagger-ui/create-swagger-ui-handler {:path "/doc" :url "/openapi.json"})
(swagger-ui/create-swagger-ui-handler {:path (:ui-url (:apidoc config))
:url (:json-url (:apidoc config))})
;; Default route: anything not explicitely handled should give a 404
(ring/create-default-handler
{:not-found (constantly {:status 404
@@ -50,9 +51,10 @@
"HolidayPirates take-home assignement.
Goal: build a (very) small REST API that exposes to endpoints."
[& args]
(let [{{port :port} :server} config]
(let [{{port :port} :server} config
{{json-doc :json-url ui-doc :ui-url} :apidoc} config]
(println "Initializing SQLite database...")
(db/init!)
(println "Ahoy! Yo-ho-ho API starting on port" port)
(println "API documentation on /openapi.json and on /doc (interactive UI)")
(println "API documentation on" json-doc "and on" ui-doc "(interactive UI)")
(http-server/run-jetty app {:port port})))

View File

@@ -3,4 +3,5 @@
(def config
{:db {:dbtype "sqlite" :dbname "yohoho.db"}
:server {:port 3000}})
:server {:port 3000}
:apidoc {:json-url "/openapi.json" :ui-url "/doc"}})