-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrouting.lisp
48 lines (42 loc) · 1.82 KB
/
routing.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
;; [[file:~/git_repos/lisp-sandbox/todo/README.org::*routing.lisp%20source][routing.lisp source:1]]
;; [[file:~/git_repos/lisp-sandbox/todo/README.org::package-include][package-include]]
(in-package :fwoar.todo)
;; package-include ends here
;; [[file:~/git_repos/lisp-sandbox/todo/README.org::defroutes][defroutes]]
(defmacro defroutes (app &body routes)
(alexandria:once-only (app)
`(setf
,@(loop for (target . descriptors) in routes
append (loop for (method callback) in descriptors
append `((ningle:route ,app ,target
:method ,method)
,callback))))))
;; defroutes ends here
;; [[file:~/git_repos/lisp-sandbox/todo/README.org::routing-helpers][routing-helpers]]
(defun success (value)
(list 200 '(:conent-type "application/json") value))
(defmacro handler ((&optional (sym (gensym "PARAMS"))) &body body)
`(lambda (,sym)
(declare (ignorable ,sym))
(success
(fwoar.lack.json.middleware:wrap-result
(progn ,@body)))))
;; routing-helpers ends here
;; [[file:~/git_repos/lisp-sandbox/todo/README.org::todo-routes][todo-routes]]
;; routing
(defun get-id (params)
(parse-integer (serapeum:assocdr :id params)))
(defun setup-routes (app)
(defroutes app
("/" (:GET (handler () (todos)))
(:POST (handler (v) (new-todo v)))
(:DELETE (handler () (clear-todos))))
("/todo/:id" (:GET (handler (v) (todo (get-id v))))
(:DELETE (handler (v)
(delete-todo (get-id v))
nil))
(:PATCH (handler (v)
(update-todo (get-id v)
(remove :id v :key #'car)))))))
;; todo-routes ends here
;; routing.lisp source:1 ends here