2011年9月8日木曜日

clojureでhelloworld

インストールについて下記参照
 >> http://y-takagi.blogspot.com/2011/09/clojure.html

1. lein newコマンドでプロジェクトのひな形を作成
 $ lein new helloworld

2. プロジェクトの設定ファイル(project.clj)を以下のように記述する
(defproject helloworld "1.0.0-SNAPSHOT"
:description "hello world"
:dependencies [[org.clojure/clojure "1.2.1"]
[org.clojure/clojure-contrib "1.2.0"]
[ring/ring "0.3.5"]
[compojure "0.6.4"]]
:dev-dependencies [[lein-ring "0.4.5"]
[swank-clojure "1.3.2"]]
:uberjar-name "helloworld.war"
:ring {:handler helloworld.core/app}
)
view raw project.clj hosted with ❤ by GitHub
 :descriptionはプロジェクトの概要
 :dependenciesはライブラリの依存関係を解決。ライブラリを取得してきてくれる。
 :dev-dependenciesはプラグインの依存関係を解決。
 :handlerにはエントリポイントとなる関数名を指定
 ring : pythonのwsgiみたいなもの https://github.com/mmcgrana/ring
 compojure : webフレームワーク https://github.com/weavejester/compojure

 ring + compojureはclojureでwebアプリ開発をする際の定番の組み合わせらしいです。

3. 依存ライブラリのダウンロード
 $ lein deps

4. webアプリケーションの作成(src/helloworld/core.clj)
(ns helloworld.core
(:use compojure.core))
(defroutes app
(GET "/" req
{:status 200
:headers {"Content-Type" "text/html"}
:body "<h3>HelloWorld</h3>"})
(GET "/hello/:name" [name]
{:status 200
:headers {"Content-Type" "text/html"}
:body (format "<p>Hello, %s!</p>" name)})
(ANY "*" _
{:status 404
:headers {"Content-Type" "text/html"}
:body "<p>not found</p>"}))
view raw core.clj hosted with ❤ by GitHub

5. ローカル環境で試す
 $ lein ring server
 を実行するとデフォルトで3000番ポートでJettyが起動します。
 http://localhost:3000にアクセスすると「HelloWorld」が表示されるはず。
 ポートを指定したい場合は「lein ring server ポート番号」

あとで思ったんだけど、実はdependenciesにclojureとclojure-contribを入れておけば
そもそもインストールする必要ないのでは!?
つまり最低Liningenが入ってればrepl起動できるし、アプリ開発も出来ると。

0 件のコメント: