Skip to content

Instantly share code, notes, and snippets.

@ppsdatta
Last active April 14, 2021 18:00
Show Gist options
  • Save ppsdatta/c32fea392665ecfe607e38441a98d5d3 to your computer and use it in GitHub Desktop.
Save ppsdatta/c32fea392665ecfe607e38441a98d5d3 to your computer and use it in GitHub Desktop.
A simple clack server with Hunchentoot backend and routing via myway

Requirements

  • clack
  • myway
  • quicklisp

Code

(defpackage :server
  (:use :cl :clack :myway)
  (:export :server :*mapper*))

(in-package :server)

(defvar *mapper* (make-mapper))

(connect *mapper* "/"
	 (lambda (params)
	   '(200
	     (:content-type "text/plain")
	     ("Hello, world"))))

(connect *mapper* "/greet/?:name?"
	 (lambda (params)
	   (list 200
		 '(:content-type "text/plain")
		 (list
		  (format nil "Hello ~A"
			  (getf params :name))))))

(connect *mapper* "/favicon.ico"
	 (lambda (params)
	   '(200
	     (:content-type "text/plain")
	     ("hello"))))

(defun server (port)
  (clackup (to-app *mapper*)
	   :address "0.0.0.0"
	   :port port))

Running

sbcl --eval "(ql:quickload '(clack myway))" --load server.lisp --eval "(server:server 4000)"

Starter script for deployed app

(ql:quickload '(clack myway))
(load "/src/server.lisp")
(server:server 4000)

Dockerfile

FROM ubuntu

RUN apt-get update && \
    apt-get install -y build-essential && \
    apt-get install -y curl \
                libblas-dev \
                libffi-dev \
                liblapack-dev \
                libz-dev \
                libzmq3-dev \
                rlwrap \
                sbcl && \
    apt-get clean

WORKDIR /src

RUN curl -o /tmp/quicklisp.lisp 'https://beta.quicklisp.org/quicklisp.lisp' && \
    sbcl --noinform --non-interactive --load /tmp/quicklisp.lisp --eval \
        "(quicklisp-quickstart:install)" && \
    sbcl --noinform --non-interactive --load ~/quicklisp/setup.lisp --eval \
        '(ql-util:without-prompting (ql:add-to-init-file))' && \
    echo '#+quicklisp(push "/src" ql:*local-project-directories*)' >> ~/.sbclrc && \
    rm -f /tmp/quicklisp.lisp


COPY ./server.lisp /src/server.lisp
COPY ./start.lisp /src/start.lisp

RUN sbcl --noinform \
         --disable-ldb \
         --lose-on-corruption \
         --eval "(ql:quickload '(clack myway))"

CMD ["sbcl", "--load", "start.lisp"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment