;;; ;;; This is a little Lisp HTTP REPL with AJAX ;;; Based on HT-AJAX tutorial ;;; ;;; To test under SBCL (http://www.sbcl.org/): ;;; ;;; First change de parameter */static-local-dir* bellow ;;; to point to your local directory with prototype.js ;;; ;;; $ sbcl ;;; * (require :asdf-install) ;;; * (asdf-install:install :hunchentoot) ;;; * (asdf-install:install :cl-who) ;;; * (asdf-install:install :ht-ajax) ;;; * (load "ajax-test.lisp") ;;; * (ajax-test:start-web) ;;; ;;; Go to http://localhost:4242/ (eval-when (:compile-toplevel :load-toplevel :execute) (require :hunchentoot) (require :cl-who) (require :ht-ajax)) (defpackage :ajax-test (:use :cl :hunchentoot :cl-who) (:export #:start-web #:stop-web)) (in-package :ajax-test) ;; local directory with lokris.js (defparameter */static-local-dir* "/home/lucindo/lisp/ajax/") ;; can be any url (defparameter *ajax-handler-url* "/ajax") ;; using prototype processor (defparameter *ajax-processor* (ht-ajax:make-ajax-processor :type :prototype :server-uri *ajax-handler-url* :js-file-uris "/static/prototype.js")) ;; hunchentoot handlers setup (eval-when (:compile-toplevel :load-toplevel :execute) (setf *dispatch-table* (list 'dispatch-easy-handlers (create-folder-dispatcher-and-handler "/static/" */static-local-dir* "text/plain") (create-prefix-dispatcher *ajax-handler-url* (ht-ajax:get-handler *ajax-processor*)) (create-prefix-dispatcher "/js" 'java-script) (create-prefix-dispatcher "/" 'main-page)))) (defparameter *web-server* nil) (defun start-web (&optional (port 4242)) (setf *web-server* (start-server :port port))) (defun stop-web () (stop-server *web-server*)) ;; eval and print a lisp expression (defun testfunc (command) (prin1-to-string (handler-case (eval (read-from-string command nil)) (error (c) (format nil "~a" c))))) ;; exporting testfunc to be called from JavaScript (ht-ajax:export-func *ajax-processor* 'testfunc :method :post) ;; JavaScript code that call testfunc (defun java-script () "function command_clicked(txt) { var command = document.getElementById('command').value; ajax_testfunc_set_element('result', command); }") (defun main-page () (with-html-output-to-string (*standard-output* nil :prologue t :indent t) (:html (:head (:script :type "text/javascript" :src "/js") (:title "AJAX test") (fmt "~a" (ht-ajax:generate-prologue *ajax-processor*))) (:body (:h1 "AJAX test") (:table :width "50%" (:tr (:td :colspan "2" (:span :id "result" (:i "no results yet")))) (:tr (:td :width "70%" (:input :type "text" :size "70" :name "command" :id "command")) (:td (:input :type "button" :value "Eval" :onclick "javascript:command_clicked();"))))))))