Skip to content

Latest commit

 

History

History
211 lines (161 loc) · 8.72 KB

File metadata and controls

211 lines (161 loc) · 8.72 KB

Tutorial 3 - Clojureベースなhttp-server

このチュートリアルは、tutorial 2で行った設定を、 ClojureのスタンダートなWebベースのアプリケーションを作るための 方法であるringcompojureを利用した外部http-serverに 取り替える方法を学ぶ。

Introduction

いままでClojureScriptコードは、JavaScriptに一回コンパイルし、それをブラウザ上で走らせる ためであって、Clojureがやることのできるhttp-serverまでは必要としていなかった。とはいえ、 Clojureが好きなわけだら、Clojureでのhttp-serverの立て方をもっと知りたいと思うはずだ。

Ring は、Webベースのアプリケーションを開発するための、Clojureベースのライブラリで あり、fundamentalなbuilding-blockの一つである。他のあらゆるhttp-serverを使うかわりに、 これを使おうと思う。

Preamble

前回のチュートリアルを終わらせて、今回のチュートリアルをやろうとしているならば、 gitをインストールして、下のように入力することをお薦めする。

git clone https://github.com/magomimmo/modern-cljs.git
cd modern-cljs
git checkout tutorial-02
git checkout -b tutorial-03-step-1

lein-ringプラグインをproject.cljに追加しよう

ClojureScriptを設定して走らせるために、ビルドのなかで如何にlein-cljsbuild が手助けをしてくれるかを見てきた。似たような方法として、ringの仕事を 自動的かつ管理してくれるlein-ringというプラグインを使おうと思う。

lein-ringをインストールするために、project.cljにプラグインとして lein-ringを追加しよう。lein-cljsbuildは、もし全てのプロジェクトで 使うことをお望みなら、グローバルなprofileにこいつを追加することもできる。 (例えば、~/.lein/profiles.cljの中に書くとかね)

lein-cljsbuildのように、lein-ringというプラグインは、:ringキーワードを project.cljの中に追加する設定を必要としてくるだろう。:ringの値は、 設定のオプションへのマップを含んでいるのだが、ここではただ:handlerという、 これから定義するであろう関数へのリファラーが必要となる。

先ほど話した、必要とされる設定と共にproject.cljを書き換えよう。

(defproject modern-cljs "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :license {:name "Eclipse Public License"
            :url "http://www.eclipse.org/legal/epl-v10.html"}
  ;; clojure source code pathname
  :source-paths ["src/clj"]

  :dependencies [[org.clojure/clojure "1.5.1"]
	             [org.clojure/clojurescript "0.0-1878"]]

  :plugins [;; cljsbuild plugin
            [lein-cljsbuild "0.3.3"]

            ;; ring plugin
            [lein-ring "0.8.7"]]

  ;; ring tasks configuration
  :ring {:handler modern-cljs.core/handler}

  ;; cljsbuild tasks configuration
  :cljsbuild {:builds
              [{;; clojurescript source code path
                :source-paths ["src/cljs"]

                ;; Google Closure Compiler options
                :compiler {;; the name of the emitted JS file
                           :output-to "resources/public/js/modern.js"

                           ;; use minimal optimization CLS directive
                           :optimizations :whitespace

                           ;; prettyfying emitted JS
                           :pretty-print true}}]})

handlerを作ろう

ringのhandlerは、リクエストを引数として受けり、そしてレスポンスを返す だけの関数だ。リクエストもレスポンスも、通常のClojureのマップだ。低レベル レイヤーのRing APIを使う代わりに、他の共通ライブラリをproject.cljに 追加しよう。その名も、compojureだ。

Webアプリケーションを小さく、そして独立したパートにcomposedし、Ring のhandlerを生成するためのDSL(ドメイン特化言語)をRingの Webアプリケーションで出来るようにするための、小さなルーティングライブラリだ。

このチュートリアルでのゴールは、resources/publicの中にセーブされた 静的なHtmlページ(simple.htmlだね!)を配信することができるように、http-serverを 立ち上げることだ。

src/clj/modern_cljsからcore.cljというファイルを開いて、下の内容に書き換えて みよう。

(ns modern-cljs.core
  (:use compojure.core)
  (:require [compojure.handler :as handler]
            [compojure.route :as route]))

;; defroutes macro defines a function that chains individual route
;; functions together. The request map is passed to each function in
;; turn, until a non-nil response is returned.
(defroutes app-routes
  ; to serve document root address
  (GET "/" [] "<p>Hello from compojure</p>")
  ; to serve static pages saved in resources/public directory
  (route/resources "/")
  ; if page is not found
  (route/not-found "Page not found"))

;; site function creates a handler suitable for a standard website,
;; adding a bunch of standard ring middleware to app-route:
(def handler
  (handler/site app-routes))

Add compojure to project.clj

Clojureベースのhttp-serverを立ち上げる前に、project.cljの 依存関係を記述しているセクションにcompojureを追加しておく必要がある。 書き換えたproject.cljは下のようになる。

(defproject modern-cljs "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :license {:name "Eclipse Public License"
            :url "http://www.eclipse.org/legal/epl-v10.html"}
  ;; clojure source code pathname
  :source-paths ["src/clj"]

  :dependencies [[org.clojure/clojure "1.5.1"]
	             [org.clojure/clojurescript "0.0-1878"]
                 [compojure "1.1.5"]]

  :plugins [;; cljsbuild plugin
            [lein-cljsbuild "0.3.3"]
            [lein-ring "0.8.7"]]

  ;; ring tasks configuration
  :ring {:handler modern-cljs.core/handler}

  ;; cljsbuild tasks configuration
  :cljsbuild {:builds
              [{;; clojurescript source code path
                :source-paths ["src/cljs"]

                ;; Google Closure Compiler options
                :compiler {;; the name of the emitted JS file
                           :output-to "resources/public/js/modern.js"

                           ;; minimum optimization
                           :optimizations :whitespace

                           ;; prettyfying emitted JS
                           :pretty-print true}}]})

http-serverを立ち上げろ!

全てを設定したら、下のようにサーバーを立ち上げることが出来るようになる。

lein ring server
2012-11-03 19:06:33.178:INFO:oejs.Server:jetty-7.6.1.v20120215
Started server on port 3000
2012-11-03 19:06:33.222:INFO:oejs.AbstractConnector:Started SelectChannelConnector@0.0.0.0:3000

"Hello form compojure"という文が表示されているページが見えているはずだ。 これを見ることができるのは、サーバーがデフォルトで動くポートである3000で ある。オプションとして、lein ring server 8888のように、違うポート番号を 指定することができる。

ブラウザコネクトベースであるReplを、lein trampoline cljsbuild repl-listenで 立ち上げることによって動いていることもチェックができる。これは新しいターミナル (/path/to/modern-cljsに移動することを思い出そう)でコマンドを叩こう。そして、 simple.htmlページに行こう。

このチュートリアルのpreambleで提案した、新しいブランチを作成しているなら、 下のように変更を加えることをお薦めする。

git commit -am "added ring and compojure"

In the next tutorial 4 we're going to have some fun introducing form validation in CLJS.

License

Copyright © Mimmo Cosenza, 2012-2013. Released under the Eclipse Public License, the same as Clojure.