create pedestal hello world
This commit is contained in:
parent
0f3caef7e7
commit
46c6de9491
2 changed files with 20 additions and 62 deletions
|
|
@ -1,47 +1,19 @@
|
|||
(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]))
|
||||
(:require [io.pedestal.connector :as conn]
|
||||
[io.pedestal.http.http-kit :as hk]))
|
||||
|
||||
(defn handle-ping [_args]
|
||||
(tel/log! :info "Got ping request")
|
||||
{:status 200, :body "ok"})
|
||||
(defn greet-handler [_request]
|
||||
{:status 200
|
||||
:body "Hello, world!\n"})
|
||||
|
||||
(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}))
|
||||
(def routes
|
||||
#{["/greet" :get greet-handler :route-name :greet]})
|
||||
|
||||
(defn wrap [handler id]
|
||||
(fn [request]
|
||||
(update (handler request) :wrap (fnil conj '()) id)))
|
||||
(defn create-connector []
|
||||
(-> (conn/default-connector-map 8890)
|
||||
(conn/with-default-interceptors)
|
||||
(conn/with-routes routes)
|
||||
(hk/create-connector nil)))
|
||||
|
||||
(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))
|
||||
(defn start []
|
||||
(conn/start! (create-connector)))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue