init api
This commit is contained in:
parent
32355ae894
commit
0f3caef7e7
3 changed files with 62 additions and 4 deletions
47
src/micro_blog/api.clj
Normal file
47
src/micro_blog/api.clj
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
(ns micro-blog.api
|
||||
(:require
|
||||
[ring.adapter.jetty :as jetty]
|
||||
[micro-blog.config :refer [config]]
|
||||
[taoensso.telemere :as tel]
|
||||
[muuntaja.middleware :as mm]
|
||||
[ring.middleware.params]
|
||||
[reitit.ring :as rr]))
|
||||
|
||||
(defn handle-ping [_args]
|
||||
(tel/log! :info "Got ping request")
|
||||
{:status 200, :body "ok"})
|
||||
|
||||
(defn handle-proc [{params :query-params}]
|
||||
(let [id (get params "id")]
|
||||
(tel/log! {:level :info :data {:id id}} "Got proc request")
|
||||
{:status 200
|
||||
:body :ok}))
|
||||
|
||||
(defn wrap [handler id]
|
||||
(fn [request]
|
||||
(update (handler request) :wrap (fnil conj '()) id)))
|
||||
|
||||
(def app
|
||||
(rr/ring-handler
|
||||
(rr/router
|
||||
["/api" {:middleware [ring.middleware.params/wrap-params
|
||||
mm/wrap-format
|
||||
[wrap :api]]}
|
||||
["/ping" {:get handle-ping}]
|
||||
["/proc" {:get handle-proc}]])
|
||||
(rr/create-default-handler)))
|
||||
|
||||
(defn run-server []
|
||||
(let [port (@config :api-port)
|
||||
host (@config :api-host)]
|
||||
(jetty/run-jetty #'app {:port port :host host})))
|
||||
|
||||
(defn -main []
|
||||
(run-server))
|
||||
|
||||
(comment
|
||||
;; evaluate this def form to start the webapp via the REPL:
|
||||
;; :join? false runs the web server in the background!
|
||||
(def server (jetty/run-jetty #'app {:port 3000 :join? false}))
|
||||
;; evaluate this form to stop the webapp via the the REPL:
|
||||
(.stop server))
|
||||
Loading…
Add table
Add a link
Reference in a new issue