Skip to content

Instantly share code, notes, and snippets.

@monomon
Created January 27, 2022 21:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save monomon/433c32099e3436033d4a7053c6f7b3b6 to your computer and use it in GitHub Desktop.
Save monomon/433c32099e3436033d4a7053c6f7b3b6 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