Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: e99706fd92
Fetching contributors…

Cannot retrieve contributors at this time

169 lines (147 sloc) 5.969 kb
(ns tryclojure.core
(:use [hiccup form-helpers page-helpers]
[noir.response :only [json]]
[clojure.stacktrace :only [root-cause]]
[clojail.core :only [sandbox eagerly-consume]]
[clojail.testers :only [secure-tester-without-def]])
(:require [noir.server :as server]
[noir.session :as session])
(defpartial links []
[(link-to "" "The official Clojure website")
(link-to "" "Getting started with Clojure")
(link-to "" "Clojure mailing list")
(link-to "" "A comprehensive Clojure tutorial")
(link-to "" "The Joy of Clojure: a book by Michael Fogus and Chris Houser")
(link-to "" "Disclojure")
(link-to "" "Planet Clojure")]))
(def github-link (link-to "" "github"))
(defpartial about-html []
"Please note that this REPL is sandboxed, so you wont be able to do everything in it "
"that you would in a local unsandboxed REPL. Keep in mind that this site is designed for "
"beginners to try out Clojure and not necessarily as a general-purpose server-side REPL."]
"One quirk you might run into is that things you bind with def can sometimes disappear. "
"The sandbox wipes defs if you def too many things, so don't be surprised. Furthermore, "
"The sandbox will automatically be wiped after 15 minutes and if you evaluate more after that,"
"It'll be in an entirely new namespace/sandbox."]
"You can find the site's source and such on its "
" page."]
"TryClojure is written in Clojure and JavaScript (JQuery), powered by "
(link-to "" "clojail")
" and Chris Done's "
(link-to "" "jquery-console")]
[:p.bottom "Design by " (link-to "" "Andrew Gwozdziewycz")])
(defpartial home-html []
"Welcome to Try Clojure. See that little box up there? That's a Clojure repl. You can type "
"expressions and see their results right here in your browser. We also have a brief tutorial to "
"give you a taste of Clojure. Try it out by typing " [:code.expr "tutorial"] " in the console!"]
"Check out the site's source on "
(link-to "" "github")
(defpartial root-html []
(include-css "/resources/public/css/tryclojure.css")
(include-js "/resources/public/javascript/jquery-1.4.2.min.js"
[:title "Try Clojure"]]
[:span.logo-try "Try"] " "
[:span.logo-clojure "Clo" [:em "j"] "ure"]]]
[:a#home.buttons "home"]
[:a#links.buttons "links"]
[:a#about.buttons.last "about"]]
[:div#changer (home-html)]]
[:p.bottom "©2011 Anthony Grimes and numerous contributors"]
"Built with "
(link-to "" "Noir")
"var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-27340918-1']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
(defpage "/" []
(defpage "/home" []
(defpage "/about" []
(defpage "/links" []
(defpage [:post "/tutorial"] {n :n}
(slurp (str "resources/public/tutorial/page" n ".html")))
(defn eval-form [form sbox]
(with-open [out (]
(let [result (sbox form {#'*out* out})]
{:expr form
:result [out result]})))
(defn eval-string [expr sbox]
(let [form (binding [*read-eval* false] (read-string expr))]
(eval-form form sbox)))
(def try-clojure-tester
(into secure-tester-without-def
(defn make-sandbox []
(sandbox try-clojure-tester
:timeout 2000
:init '(do (use '[clojure.repl :only [doc]])
(future (Thread/sleep 600000)
(-> *ns* .getName remove-ns)))))
(defn find-sb [old]
(if-let [sb (get old "sb")]
(assoc old "sb" (make-sandbox))))
(defn eval-request [expr]
(eval-string expr (get (session/swap! find-sb) "sb"))
(catch TimeoutException _
{:error true :message "Execution Timed Out!"})
(catch Exception e
{:error true :message (str (root-cause e))})))
(defpage "/eval.json" {:keys [expr jsonp]}
(let [{:keys [expr result error message] :as res} (eval-request expr)]
(if error
(let [[out res] result]
{:expr (pr-str expr)
:result (str out (pr-str res))}))))
#(if jsonp (str jsonp "(" % ")") %)))
(server/add-middleware wrap-file (System/getProperty "user.dir"))
(defn to-port [s]
(when-let [port s] (Long. port)))
(defn tryclj [& [port]]
(or (to-port port)
(to-port (System/getenv "PORT")) ;; For deploying to Heroku
{:session-cookie-attrs {:max-age 600}}))
(defn -main [& args] (tryclj (first args)))
Jump to Line
Something went wrong with that request. Please try again.