Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
(ns demo.gemstoclojure
(:use ring.adapter.jetty)
(:use clojure.contrib.classpath))
(import '(org.jruby.embed ScriptingContainer LocalContextScope))
(def c (ScriptingContainer. LocalContextScope/THREADSAFE))
(println (classpath))
(println (pwd))
;; Using $LOAD_PATH to load haml gem
;(def gempath [(str (pwd) "/src/haml-3.1.2/gem")])
;(. c setLoadPaths gempath)
;(. c runScriptlet "require 'rubygems'; require 'haml'")
;; Using classpath to load haml gem
(. c runScriptlet "require 'rubygems'; require 'haml-3.1.2/gem/haml'")
(def engineclass (. c runScriptlet "Haml::Engine"))
(def template
Hello Clojure!
Hello Clojure from Haml!")
(def engine (. c callMethod engineclass "new" template Object))
(defn app [req]
{:status 200
:headers {"Content-Type" "text/html"}
:body (. c callMethod engine "render" String)})
(defn -main []
(let [port (Integer/parseInt (System/getenv "PORT"))]
(run-jetty app {:port port})))

Very cool.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment