Skip to content

Instantly share code, notes, and snippets.

@svetlyak40wt
Created March 4, 2022 13:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save svetlyak40wt/14d42db8b344492f53e0a0aac2e3c4ca to your computer and use it in GitHub Desktop.
Save svetlyak40wt/14d42db8b344492f53e0a0aac2e3c4ca to your computer and use it in GitHub Desktop.
Example of a simple DEFROUTE macro for Ningle Common Lisp web framework
(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