From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Jan Wedekind Newsgroups: gmane.lisp.guile.user Subject: Artanis web REPL Date: Fri, 24 Jun 2016 12:43:29 +0100 (BST) Message-ID: Reply-To: Jan Wedekind NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: TEXT/PLAIN; format=flowed; charset=US-ASCII X-Trace: ger.gmane.org 1466768645 4921 80.91.229.3 (24 Jun 2016 11:44:05 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 24 Jun 2016 11:44:05 +0000 (UTC) To: guile-user@gnu.org Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Fri Jun 24 13:43:56 2016 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1bGPWe-0006YW-3L for guile-user@m.gmane.org; Fri, 24 Jun 2016 13:43:56 +0200 Original-Received: from localhost ([::1]:42759 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bGPWd-0006Ce-8f for guile-user@m.gmane.org; Fri, 24 Jun 2016 07:43:55 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:54341) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bGPWI-0006CN-FH for guile-user@gnu.org; Fri, 24 Jun 2016 07:43:35 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bGPWF-0006FA-A1 for guile-user@gnu.org; Fri, 24 Jun 2016 07:43:34 -0400 Original-Received: from basicbox4.server-home.net ([195.137.212.26]:39615) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bGPWF-0006DK-3I for guile-user@gnu.org; Fri, 24 Jun 2016 07:43:31 -0400 Original-Received: from [10.10.10.250] (salt-ext.roke.co.uk [109.207.29.2]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by basicbox4.server-home.net (Postfix) with ESMTPSA id 1EF5B1530665 for ; Fri, 24 Jun 2016 13:43:27 +0200 (CEST) X-X-Sender: jan@wedemob User-Agent: Alpine 2.11 (DEB 23 2013-08-11) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x X-Received-From: 195.137.212.26 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Original-Sender: "guile-user" Xref: news.gmane.org gmane.lisp.guile.user:12696 Archived-At: Just managed to get a simple web REPL working. It still needs some form of safe environments for each browser sessions. Maybe one can do something like the IPython notebooks in the future. Any comments/suggestions welcome. ; prototype Guile repl for the browser (use-modules (srfi srfi-26) (artanis artanis) (artanis cookie) (artanis utils) (ice-9 regex)) (init-server) (define (dot lst) (apply cons lst)) (define (quoted expr) (call-with-output-string (cut write expr <>))) (define (line-breaks s) (regexp-substitute/global #f "\n" s 'pre "
" 'post)) (define lines '()) (define (reset session) (set! lines (assoc-set! lines session '())) (output session (format #f "; session: ~a~&" session))) (define (output session line) (assoc-set! lines session (cons line (assoc-ref lines session)))) (define (repl session line) (catch #t (lambda () (output session line) (let [(result (eval-string line (current-module)))] (if (not (unspecified? result)) (output session (format #f "; ~a~&" (quoted result)))))) (lambda (key function fmt vals . args) (let* [(msg (apply format #f fmt vals)) (info (format #f "; ~a" msg))] (output session info))))) (define (editor session) (tpl->response `(html (body ,(map (lambda (line) `(p ,(line-breaks (eliminate-evil-HTML-entities line)))) (reverse (assoc-ref lines session))) (form (@ (action "") (method "post")) (input (@ (type "text") (name "line") (autofocus "autofocus")))))))) (get "/" #:session 'spawn (lambda (rc) (let [(session (:session rc 'spawn))] (reset session) (editor session)))) (post "/" (lambda (rc) (let* [(session (cookie-ref (rc-cookie rc) "sid")) (post-data (map dot (generate-kv-from-post-qstr (rc-body rc)))) (line (uri-decode (assoc-ref post-data "line")))] (repl session line) (editor session)))) (run)