Clojure Game Of Life App on Heroku
2012-01-29 Hinterlasse einen Kommentar
A while ago I created a heroku account to try it out. They allow you to host clojure web apps in the cloud. One worker is for free and there are also quite more languages supported.
In order to practice some clojure I implemented game of life and packaged it into a lib. Here does leiningen help you a lot, it is the maven of the clojure world and also creates maven compatible jar files.
My app was originally printing out the game of life world to the CLI but now outputs it as text/plain.
I won’t repeat the steps to setup heroku since they are explained very well on heroku: http://devcenter.heroku.com/articles/clojure
The result can be accessed here: http://gentle-journey-9851.herokuapp.com/
The state is globally shared through all requests. So don’t be surprised if there are some generations missing.
The code and library is available on github. The game of life source code is included in the jar, in case some is interested. I might have a different post about this later.
https://github.com/dedeibel/herokus_game_of_clojure_life
To start the app:
Prerequisites: leiningen
To run locally, simple run
$ lein run -m herokutest.web 5000
or
$ foreman start
And visit http://localhost:5000
(ns herokutest.web (:use [game_of_life.cell :only (new_cell)]) (:use [game_of_life.world_printer :only (to_string)]) (:use [game_of_life.world_builder :only (from_string)]) (:use [game_of_life.game :only (next_generation)]) (:use ring.adapter.jetty) ) (def twentyfour_and_more "xx ,xx , , , , x x xxx , x x x , xxx xxx , x x , x xxx , xx , x x , x x , x x , x x , x x , x x , xx" ) (def counter (atom 0)) (def world (atom (from_string twentyfour_and_more new_cell))) (defn app [req] (let [current @world] (swap! counter inc) (swap! world next_generation) {:status 200 :headers {"Content-Type" "text/plain"} :body (str "Game Of Life (Step " @counter "):\n" (to_string current)) } ) ) (defn -main [port] (run-jetty app {:port (Integer. port)}))