Skip to content

Instantly share code, notes, and snippets.

@jgarte
Forked from monomon/simple-file-server.lisp
Created January 27, 2022 21:22
Show Gist options
  • Save jgarte/b76f76f531af1bfc49b7bce03d5fe018 to your computer and use it in GitHub Desktop.
Save jgarte/b76f76f531af1bfc49b7bce03d5fe018 to your computer and use it in GitHub Desktop.
Simple hunchentoot server with file form and post handler for the file
(define-easy-handler (submission-form :uri "/" :default-request-type :get) ()
(setf (hunchentoot:content-type*) "text/html")
(with-html-output-to-string (s)
(:html
(:head
(:title "File Processor")
(:style :rel "stylesheet" :type "text/css"
(str (css-lite:css (("body") (:background-color "#666"))))))
(:body
(:form :action "/process"
:method :post
:enctype "multipart/form-data"
(:input :type :file :name "file" :id "file")
(:select :name "preset" :id "preset"
(dolist (file (directory (merge-pathnames
#P"*.txt"
(asdf:system-relative-pathname
:mnm-file-processor
"presets/"))))
(htm (:option :value (file-namestring file)
(str (file-namestring file))))))
(:input :type :submit :name "submit" :value "submit"))))))
(define-easy-handler (submission-handler :uri "/process" :default-request-type :get) ()
(setf (hunchentoot:content-type*) "text/html")
(destructuring-bind (tempfile filename mimetype) (post-parameter "file")
(let ((tempfile2 (merge-pathnames filename "/tmp/mnm")))
(uiop:copy-file tempfile tempfile2)
(mnm-process-file tempfile2 (merge-pathnames (post-parameter "preset")
(asdf:system-relative-pathname
:mnm-file-processor
"presets/")))
(format nil "~a~%" (namestring tempfile2)))))
(defun start-server ()
(when (and *acceptor* (started-p *acceptor*))
(stop *acceptor*))
(setf *acceptor* (make-instance 'easy-acceptor :port 8080))
(start *acceptor*))
(defun stop-server ()
(stop *acceptor*))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment