add route handler for creating snippets

This commit is contained in:
Travis Shears 2025-06-05 11:39:36 +02:00
parent b204fbb16d
commit 524b7520d8
5 changed files with 36 additions and 11 deletions

View file

@ -2,12 +2,15 @@
App to store my code snippets. App to store my code snippets.
## Links
- [reitit docs](https://cljdoc.org/d/metosin/reitit/0.9.1/doc/introduction)
- [basic web development clojure tutorial](https://clojure-doc.org/articles/tutorials/basic_web_development/)
- [reitit malli coercion](https://cljdoc.org/d/metosin/reitit/0.9.1/doc/coercion/malli)
- [malli docs](https://github.com/metosin/malli)
## Dev ## Dev
Starting by following this guide
https://clojure-doc.org/articles/tutorials/basic_web_development/
### How to run dev server ### How to run dev server
``` ```

View file

@ -0,0 +1,11 @@
meta {
name: create_snippet
type: http
seq: 3
}
get {
url:
body: none
auth: none
}

View file

@ -0,0 +1,3 @@
vars {
host: http://localhost:3000
}

View file

@ -5,7 +5,7 @@ meta {
} }
get { get {
url: http://localhost:3000/api/ping url: {{host}}/api/ping
body: none body: none
auth: none auth: none
} }

View file

@ -4,10 +4,14 @@
[clojure.pprint :as pprint] [clojure.pprint :as pprint]
[reitit.ring :as rr])) [reitit.ring :as rr]))
(defn handler [args] (defn handle-ping [args]
(pprint/pprint args) (pprint/pprint args)
{:status 200, :body "ok"}) {:status 200, :body "ok"})
(defn handle-create-snippet [args]
(pprint/pprint args)
{:status 200, :body "snippet created"})
(defn wrap [handler id] (defn wrap [handler id]
(fn [request] (fn [request]
(update (handler request) :wrap (fnil conj '()) id))) (update (handler request) :wrap (fnil conj '()) id)))
@ -16,11 +20,8 @@
(rr/ring-handler (rr/ring-handler
(rr/router (rr/router
["/api" {:middleware [[wrap :api]]} ["/api" {:middleware [[wrap :api]]}
["/ping" {:get handler ["/ping" {:get handle-ping}]
:name ::ping}] ["/snippet" {:post handle-create-snippet}]])
["/admin" {:middleware [[wrap :admin]]}
["/users" {:get handler
:post handler}]]])
(rr/create-default-handler))) (rr/create-default-handler)))
;; (defroutes app-routes ;; (defroutes app-routes
@ -33,3 +34,10 @@
(defn -main [] (defn -main []
(jetty/run-jetty #'app {:port 3000})) (jetty/run-jetty #'app {:port 3000}))
(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))