From ee22d5783daf362334626f18d516a73d1fcf5dce Mon Sep 17 00:00:00 2001 From: not manx Date: Mon, 25 May 2020 09:47:14 +0000 Subject: [PATCH] Initial commit --- hunchenhelpers.asd | 9 +++++++++ main.lisp | 26 ++++++++++++++++++++++++++ package.lisp | 6 ++++++ 3 files changed, 41 insertions(+) create mode 100644 hunchenhelpers.asd create mode 100644 main.lisp create mode 100644 package.lisp diff --git a/hunchenhelpers.asd b/hunchenhelpers.asd new file mode 100644 index 0000000..2d99b0c --- /dev/null +++ b/hunchenhelpers.asd @@ -0,0 +1,9 @@ +(asdf:defsystem #:hunchenhelpers + :description "A helper library to simplify hunchentoot" + :author "Manx (boku@plum.moe)" + :license "X11/MIT" + :version "0.1.0" + :serial t + :depends-on (:hunchentoot) + :Components ((:file "package") + (:file "main"))) diff --git a/main.lisp b/main.lisp new file mode 100644 index 0000000..d0e43a0 --- /dev/null +++ b/main.lisp @@ -0,0 +1,26 @@ +(in-package :hunchenhelpers) + +(defun hunchenhost (func uri path &optional content-type) + (let ((thing (if (null content-type) + (funcall func uri path) + (funcall func uri path content-type)))) + (push thing tbnl:*dispatch-table*))) + +(defun host-file (uri path &optional content-type) + (hunchenhost 'tbnl:create-static-file-dispatcher-and-handler uri path content-type)) + +(defun host-dir (uri path &optional content-type) + (hunchenhost 'tbnl:create-folder-dispatcher-and-handler uri path content-type)) + +(defmacro handle (method uri content-type params &body body) + "Creates an easy handles for a specific HTTP request method. If the +method provided sent from the client isn't correct, return 404 and +stop processing the request. + +(handle :get (uri-fun :uri \"/path/to/page\"/) @content-type (args) (body))" + `(tbnl:define-easy-handler ,uri ,params + (unless (eq ,method (tbnl:request-method*)) + (setf (tbnl:return-code*) tbnl:+http-not-found+) + (tbnl:abort-request-handler)) + (setf (tbnl:content-type* tbnl:*reply*) ,content-type) + ,@body)) diff --git a/package.lisp b/package.lisp new file mode 100644 index 0000000..be2bcfc --- /dev/null +++ b/package.lisp @@ -0,0 +1,6 @@ +(defpackage #:hunchenhelpers + (:nicknames :henh) + (:use :cl :hunchentoot) + (:export + :host-file :host-dir + :handle))