From d3a896e79f971d8ca4a234c01c346c0d0ac043f6 Mon Sep 17 00:00:00 2001 From: vindarel Date: Wed, 14 Dec 2022 12:02:41 +0100 Subject: [PATCH] scripting: add simple HTTP server with Hunchentoot --- docs/scripting.md | 72 +++++++++++++++++++++++++++++++ src/scripts/simpleHTTPserver.lisp | 29 +++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 src/scripts/simpleHTTPserver.lisp diff --git a/docs/scripting.md b/docs/scripting.md index f29a693..76bad76 100644 --- a/docs/scripting.md +++ b/docs/scripting.md @@ -134,6 +134,78 @@ $ ciel -e "(-> (http:get \"https://fakestoreapi.com/products/1\") (json:read-jso ) ``` +## Other scripts + +### Simple HTTP server + +see `src/scripts/simpleHTTPserver.lisp` in the CIEL repository. + +~~~lisp +(in-package :ciel-user) + +;; CLI args: the script name, an optional port number. +(defparameter *port* (or (parse-integer (second (uiop:command-line-arguments))) + 8000)) + +(defvar *acceptor* (make-instance 'hunchentoot:easy-acceptor + :document-root "./" + :port *port*)) +(hunchentoot:start *acceptor*) ;; async, runs in its own thread. + +(format! t "~&Serving files on port ~a…~&" *port*) +(handler-case + ;; The server runs on another thread, don't quit instantly. + ;; Catch a C-c and quit gracefully. + (sleep most-positive-fixnum) + (sb-sys:interactive-interrupt () + (format! t "Bye!"))) +~~~ + +Given you have an `index.html` file: + +```html + + + Hello! + + +

Hello CIEL!

+

+ We just served our own files. +

+ + +``` + +If you want to serve static assets under a `static/` directory: + +~~~lisp +;; Serve static assets under static/ +(push (hunchentoot:create-folder-dispatcher-and-handler + "/static/" "static/" ;; starts without a / + ) + hunchentoot:*dispatch-table*) +~~~ + +Now load a .js file as usual in your template: + + + +which can be: + +~~~javascript +// ciel.js +alert("hello CIEL!"); +~~~ + +Example output: + +``` +$ ciel src/scripts/simpleHTTPserver.lisp 4444 +Serving files on port 4444… +127.0.0.1 - [2022-12-14 12:06:00] "GET / HTTP/1.1" 200 200 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:103.0) Gecko/20100101 Firefox/103.0" +``` + --- Now, let us iron out the details ;) diff --git a/src/scripts/simpleHTTPserver.lisp b/src/scripts/simpleHTTPserver.lisp new file mode 100644 index 0000000..29db7bb --- /dev/null +++ b/src/scripts/simpleHTTPserver.lisp @@ -0,0 +1,29 @@ +;;; +;;; Run with: +;;; $ ciel simpleHTTPserver.lisp 4242 +;;; +;;; or add a shebang line and make this script executable. +;;; + +(in-package :ciel-user) + +;; CLI args: the script name, an optional port number. +(defparameter *port* (or (parse-integer (second (uiop:command-line-arguments))) + 8000)) + +(defvar *acceptor* (make-instance 'hunchentoot:easy-acceptor + :document-root "./" + :port *port*)) +(hunchentoot:start *acceptor*) + +;; Serve static assets under a static/ directory (optional). +(push (hunchentoot:create-folder-dispatcher-and-handler + "/static/" "static/" ;; starts without a / + ) + hunchentoot:*dispatch-table*) + +(format! t "~&Serving files on port ~a…~&" *port*) +(handler-case + (sleep most-positive-fixnum) + (sb-sys:interactive-interrupt () + (format! t "Bye!")))