Skip to content

Instantly share code, notes, and snippets.

@devn
Forked from yokolet/gemstoclojure.clj
Created September 17, 2011 17:39
Show Gist options
  • Save devn/1224167 to your computer and use it in GitHub Desktop.
Save devn/1224167 to your computer and use it in GitHub Desktop.
(ns demo.gemstoclojure
(:require [ring.adapater.jetty :as jetty]
[clojure.contrib.io :as io]
[clojure.contrib.classpath :as cp])
(:import [org.jruby.embed ScriptingContainer LocalContextScope]))
(def container (ScriptingContainer. LocalContextScope/THREADSAFE))
(println (cp/classpath))
(println (io/pwd))
;; Using $LOAD_PATH to load haml gem
;(def gempath [(str (io/pwd) "/src/haml-3.1.2/gem")])
;(. container setLoadPaths gempath)
;(. container runScriptlet "require 'rubygems'; require 'haml'")
;; Using classpath to load haml gem
(. container runScriptlet "require 'rubygems'; require 'haml-3.1.2/gem/haml'")
(def engineclass (. container runScriptlet "Haml::Engine"))
(def template
"%html
%head
%title
Hello Clojure!
%body
%h2
Hello Clojure from Haml!")
(def engine (. container callMethod engineclass "new" template Object))
(defn app [req]
{:status 200
:headers {"Content-Type" "text/html"}
:body (. container callMethod engine "render" String)})
(defn -main []
(let [port (Integer/parseInt (System/getenv "PORT"))]
(jetty/run-jetty app {:port port})))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment