LoginSignup
24
21

More than 5 years have passed since last update.

PHPerだが、Scalaのフレームワーク「Scalatra」を使ってみる

Last updated at Posted at 2013-07-27

Scalatra = Scalaで書かれたウェブマイクロフレームワーク

PHPでいうと、Silex的なもの。

1. インストール

Installation | Scalatra(英語) に書いてある内容に従ってやる

1.1. JDKをインストールする

java -versionjavac -version を実行して、自分の環境にJDKがインストールされているか確認する。

$ java -version
java version "1.6.0_24"
OpenJDK Runtime Environment (IcedTea6 1.11.1) (6b24-1.11.1-3)
OpenJDK 64-Bit Server VM (build 20.0-b12, mixed mode)
$ javac -version
javac 1.6.0_24

バージョン1.6以降が必要。

1.2. giter8をインストールする

Conscriptをインストールする

$ curl https://raw.github.com/n8han/conscript/master/setup.sh | sh
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   725  100   725    0     0    747      0 --:--:-- --:--:-- --:--:--  1139

Fetching current launch configuration...

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   242  100   242    0     0    261      0 --:--:-- --:--:-- --:--:--   313

Fetching launcher...

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 1079k  100 1079k    0     0   179k      0  0:00:06  0:00:06 --:--:--  224k

conscript installed to /Users/suin/bin/cs

giter8をインストールする

$ cs n8han/giter8
Getting net.databinder.conscript conscript_2.9.1 0.4.1 ...
downloading https://oss.sonatype.org/content/repositories/releases/net/databinder/conscript/conscript_2.9.1/0.4.1/

…いっぱいダウンロードされる

downloading http://repo1.maven.org/maven2/com/jcraft/jsch/0.1.44-1/jsch-0.1.44-1.jar ...
    [SUCCESSFUL ] com.jcraft#jsch;0.1.44-1!jsch.jar (243ms)
:: retrieving :: org.scala-sbt#boot-app
    confs: [default]
    25 artifacts copied, 0 already retrieved (4251kB/32ms)

giter8 0.5.3
Usage: g8 [TEMPLATE] [OPTION]...
Apply specified template.

OPTIONS
    -b, --branch
        Resolves a template within a given branch
    --paramname=paramvalue
        Set given parameter value and bypass interaction.


Apply template and interactively fulfill parameters.
    g8 n8han/giter8

Or
    g8 git://github.com/n8han/giter8.git

Apply template from a remote branch
    g8 n8han/giter8 -b some-branch

Apply template from a local repo
    g8 file://path/to/the/repo

Apply given name parameter and use defaults for all others.
    g8 n8han/giter8 --name=template-test




Conscripted n8han/giter8 to /Users/suin/bin/g8

2. 最初のプロジェクト

2.1 Scalatraプロジェクトを生成

インタラクティブシェルでプロジェクト名などを聞かれるので適当に入力していく

$ g8 scalatra/scalatra-sbt 
organization [com.example]: org.suin
package [com.example.app]: org.suin.helloworld
name [My Scalatra Web App]: My Scalatra Hello World
scalatra_version [2.2.1]: 
servlet_name [MyScalatraServlet]: 
scala_version [2.10.2]: 
version [0.1.0-SNAPSHOT]: 

Template applied in ./my-scalatra-hello-world

2.2 ビルド

$ cd my-scalatra-hello-world/
$ chmod u+x sbt 
$ ./sbt
…いろいろダウンロードされる

> container:start

って入力すると、
コンパイルされて、localhost:8080 でサーバが立ち上がる
> browse

と叩くとブラウザが立ち上がって localhost:8080 につなぎに行ってくれる。(Macでためした)

2.3 自動リロード

sbt のコンソールで ~ ;copy-resources;aux-compile と入力すると、ファイル更新時に自動でコンパイルしてくれるようになる。

$ ./sbt
> ~ ;copy-resources;aux-compile

ENTERを押すと自動リロードを中断できる。

3 プロジェクトの構造

3.1 パス

project
|_build.properties      <= どのバージョンのsbtを使うか指定する
|_build.scala           <= プロジェクトの設定と依存関係がここに入ってる
|_plugins.sbt           <= sbtのプラグインはここに入る

src
|_ main
|  |_ scala
|  |  |   |_ScalatraBootstrap.scala     <= mount servlets in here
|  |  |_org
|  |      |_ yourdomain
|  |         |_ projectname
|  |            |_ MyScalatraServlet.scala
|  |_ webapp
|     |_ WEB-INF
|        |_ views
|        |  |_ hello-scalate.scaml
|        |_ layouts
|        |  |_ default.scaml
|        |_ web.xml
|_ test
   |_ scala
      |_ org
         |_ yourdomain
            |_ projectname
               |_ MyScalatraServletSpec.scala

3.2 静的ファイルをあつかう

webapp ディレクトリに置くだけ。 ただし、WEB-INF の中身は公開されない。

$ echo 'test' > hoge.html

http://localhost:8080/hoge.html をブラウザで見ると test を表示される。

4. IDEをそろえる

公式サイトのドキュメント IDE Support (optional) ではEclipse、IntelliJ、ENSIMEについて解説してるが、PhpStromに慣れてるので姉妹アプリのIntelliJを選択した。

  • IntelliJ IDEAのcommunityエディションをダウンロードする
  • Scala pluginをJetBrainsからダウンロードする(IntelliJを起動して、Preference > Plugins > Install JetBrains plugins にて Scala で検索してインストールするほうが便利)
  • project/plugins.sbt に以下の行を追加して、sbt-ideaプラグインをインストールする
addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.5.2")

※上記記述はバージョン番号が古くなっている場合があります。最新のバージョンは sbt-idea で調べてください。

$ ./sbt
> gen-idea

5. サンプルを入手する

https://github.com/scalatra/scalatra-website-examples でScalatraのサンプルが配布されている。

hub clone scalatra/scalatra-website-examples
24
21
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
24
21