Created
March 4, 2022 13:04
-
-
Save svetlyak40wt/14d42db8b344492f53e0a0aac2e3c4ca to your computer and use it in GitHub Desktop.
Example of a simple DEFROUTE macro for Ningle Common Lisp web framework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(uiop:define-package #:ningle-defroute | |
(:use #:cl)) | |
(in-package #:ningle-defroute) | |
(defmacro defroute (app path-or-path-with-options (&rest view-arguments) &body view-body) | |
(let* ((path-with-options (uiop:ensure-list path-or-path-with-options)) | |
(params (gensym "PARAMS")) | |
(bindings (loop for arg-name in view-arguments | |
for arg-name-kw = (alexandria:make-keyword arg-name) | |
collect (list arg-name | |
`(alexandria:assoc-value ,params ,arg-name-kw))))) | |
`(setf (ningle:route ,app ,@path-with-options) | |
#'(lambda (,params) | |
(let ,bindings | |
,@view-body))))) | |
(defvar *app* (make-instance 'ningle:app)) | |
(defroute *app* "/guess/:who" (who) | |
(if (string= who "Eitaro") | |
"You got me!" | |
"Hello stranger!")) | |
(defroute *app* ("/chat/:id" :method :POST) (id message) | |
(format nil "Sending message ~A to the chat ~A" | |
message | |
id)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment