Sooey

2011-06-26 02:45:41 +0900

NoirというClojure用のシンプルなWebアプリケーションフレームワークを見つけたので、インストール手順のメモ。

(ns my-app
  (:use noir.core)
  (:require [noir.server :as server]))

(defpage "/welcome" []
    "Welcome to Noir!")

(server/start 8080 {})

↑Noirでは、こんな感じでアプリケーションが書ける。

Leiningenのインストール

まず、Clojure用ビルドツールとして定番のleiningenを導入します。ビルドツールといいつつ、依存するライブラリのインストールやClojureアプリケーションの起動など色々やってくれるみたい。

leiningenはleinというシェルスクリプトとjarファイルのセットで構成されており、leinスクリプトをダウンロードして実行すると自身でjarファイルのダウンロード&インストールを行うようになっています。

$ curl -L -O https://github.com/technomancy/leiningen/raw/stable/bin/lein
$ chmod +x lein
$ ./lein
Downloading Leiningen now...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 8003k  100 8003k    0     0  1563k      0  0:00:05  0:00:05 --:--:-- 2380k
Leiningen is a build tool for Clojure.

Several tasks are available:
classpath   Print the classpath of the current project.
clean       Remove compiled class files from project.
compile     Compile Clojure source into .class files.
deploy      Build and deploy jar to remote repository.
deps        Download all dependencies and put them in :library-path.
help        Display a list of tasks or help for a given task.
install     Install the current project or download the project specified.
interactive Enter interactive task shell.
jar         Package up all the project's files into a jar file.
javac       Compile Java source files.
new         Create a new project skeleton.
plugin      Manage user-level plugins.
pom         Write a pom.xml file to disk for Maven interop.
repl        Start a repl session either with the current project or standalone.
run         Run a -main function with optional command-line arguments.
test        Run the project's tests.
test!       Run a project's tests after cleaning and fetching dependencies.
uberjar     Package up the project files and deps into a jar file.
upgrade     Upgrade Leiningen to the latest stable release.
version     Print version for Leiningen and the current JVM.

Run lein help $TASK for details.
Also available: readme, tutorial, copying, sample, and news.

インストールが完了したら、leinコマンドはどこかパスの通ったところに置いておけばよさそうです。

Noirのインストール

leiningenでNoirプロジェクトの作成を行うためのlein-noirプラグインというものがあるので、これをインストールします。

$ lein plugin install lein-noir 1.0.0
Downloading: org/clojure/clojure/1.2.1/clojure-1.2.1.pom from central
Downloading: org/clojure/clojure/1.2.1/clojure-1.2.1.jar from central
Copying 1 file to /var/folders/U1/U1N91KLEHEyHpA-qb8SHhU+++TI/-Tmp-/lein-816281b5-5ee9-4f7d-ad84-6c24edd7ddaf/lib
Including lein-noir-1.0.0.jar
Including clojure-1.2.1.jar
Created lein-noir-1.0.0.jar

leiningenによってNoirが依存するClojureのjarファイルもダウンロードされました。

プロジェクトの作成

leinコマンドでNoirプロジェクトが作成できるようになりました。

$ lein noir new helloworld
Creating noir project:  helloworld
Creating new dirs at:  /Users/juno/helloworld
Adding files...
Project created!

作成されたプロジェクトディレクトリの内容はこんな感じになります。

$ tree helloworld
helloworld
├── README.md
├── project.clj
├── resources
│   └── public
│       ├── css
│       │   └── reset.css
│       ├── img
│       └── js
├── src
│   └── helloworld
│       ├── models
│       ├── server.clj
│       └── views
│           ├── common.clj
│           └── welcome.clj
└── test
    └── helloworld

11 directories, 6 files

とりあえずそのまま実行してみます。

$ cd helloworld
$ lein run

leiningenがproject.cljの内容を読んで必要なライブラリのダウンロードとインストールを行います。インストールがひと通り完了すると、以下のようにポート8080でサーバーが起動します。

Starting server...
2011-06-26 02:33:23.165:INFO::Logging to STDERR via org.mortbay.log.StdErrLog
2011-06-26 02:33:23.174:INFO::jetty-6.1.26
2011-06-26 02:33:23.241:INFO::Started SocketConnector@0.0.0.0:8080
Server started on port [8080].
You can view the site at http://localhost:8080

ブラウザでhttp://localhost:8080/にアクセスすると、Noirの簡単なチュートリアルページが表示されます。