diff --git a/config.edn b/config.edn index 4a6ad7f..82f81db 100644 --- a/config.edn +++ b/config.edn @@ -5,4 +5,7 @@ :pocket-base-host "http://aemos:5000" :blue-sky-host "https://bsky.social/xrpc" - :blue-sky-username "travisshears.bsky.social"} + :blue-sky-username "travisshears.bsky.social" + + :api-host "localhost" + :api-port 3000} diff --git a/deps.edn b/deps.edn index 3a11f98..3caa086 100644 --- a/deps.edn +++ b/deps.edn @@ -1,5 +1,6 @@ {:paths ["src"] - :deps {clj-http/clj-http {:mvn/version "3.13.1"} + :deps {;; http client + clj-http/clj-http {:mvn/version "3.13.1"} ;; logging com.taoensso/telemere {:mvn/version "1.0.0"} ;; schema validation @@ -7,10 +8,17 @@ ;; scheduling jarohen/chime {:mvn/version "0.3.3"} org.clojure/core.async {:mvn/version "1.8.741"} - ;; json decoding + ;; json cheshire/cheshire {:mvn/version "6.0.0"} ;; metosin/muuntaja {:mvn/version "0.6.11"} - org.clojure/clojure {:mvn/version "1.12.1"}} + org.clojure/clojure {:mvn/version "1.12.1"} + ;; api + ring/ring-core {:mvn/version "1.13.0"} + ring/ring-jetty-adapter {:mvn/version "1.13.0"} + org.slf4j/slf4j-simple {:mvn/version "2.0.16"} + metosin/muuntaja {:mvn/version "0.6.11"} + metosin/reitit {:mvn/version "0.9.1"}} + :aliases {;; Run with clj -T:build function-in-build :build {:deps {io.github.clojure/tools.build {:git/tag "v0.10.9" :git/sha "e405aac"}} diff --git a/src/micro_blog/api.clj b/src/micro_blog/api.clj new file mode 100644 index 0000000..9a3dcdc --- /dev/null +++ b/src/micro_blog/api.clj @@ -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))