#目的
Trotフレームワークを利用しHello Trot!!を表示する。#実施環境
OS: Windows8.1Erlang: Eshell V6.4 OTP v17.5
Elixir: v1.0.4
Trot Framework: v0.5
#目次
1. プロジェクト作成2. 問題発生!!
3. コンフィグ設定
4. router作成
#参考文献
https://github.com/hexedpackets/trot_example#始める前に
はい、前回の記事で翻訳を上げたTrotフレームワークを試してみます。動かすまでにちょっと四苦八苦しました。
Twitterでは醜態を晒すTweetをしてました。すいません。
さて、簡単にTrotの説明をします。
Rubyで開発されたSinatraのように記述ができるElixir版のSinatraみたいなものです。
なので、ソースコード自体も少なくWebフレームワークの勉強や書き捨てに良いと思います。
非常に小さいのでreadmeでは、マイクロフレームワークと紹介されていました。
前回のreadmeを翻訳した記事: TrotのREADME.mdを翻訳してみた。
さて、説明はこんなところでやっていきましょう。
##1. プロジェクト作成
以下の通りプロジェクトを作成しました。>cd プロジェクト作成ディレクトリ
>mix new trot_sample (以降、プロジェクト名はtrot_sampleとする)
>mix test
mix.exsを開き、以下の通り編集して下さい。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule TrotSample.Mixfile do | |
use Mix.Project | |
def project do | |
[app: :trot_sample, | |
version: "0.0.1", | |
elixir: "~> 1.0", | |
build_embedded: Mix.env == :prod, | |
start_permanent: Mix.env == :prod, | |
deps: deps] | |
end | |
# Configuration for the OTP application | |
# | |
# Type `mix help compile.app` for more information | |
def application do | |
[applications: [:logger, :trot]] | |
end | |
# Dependencies can be Hex packages: | |
# | |
# {:mydep, "~> 0.3.0"} | |
# | |
# Or git/path repositories: | |
# | |
# {:mydep, git: "https://github.com/elixir-lang/mydep.git", tag: "0.1.0"} | |
# | |
# Type `mix help deps` for more examples and options | |
defp deps do | |
[ | |
{:trot, github: "hexedpackets/trot"} | |
] | |
end | |
end |
さて、本来ならdeps.getで依存関係の解決をするところですが、
このまま実施すると、ちょっと問題が発生します。
それは次の項で・・・
##2. 問題発生!!
2015/06/17時点で修正されていることを確認しました。今後は、この作業は必要ないです。
1.で記述しましたが、あのままdeps.getをするとコンパイル時に問題が出ます。
なので、先に回避するための方法を記述します。
発生する問題について知りたい方は以下のissueを参考にするとよいです。
https://github.com/hexedpackets/trot/issues/16
mix.lockを開き以下の通りに修正して下さい。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%{"calliope": {:hex, :calliope, "0.3.0"}, | |
"cowboy": {:hex, :cowboy, "1.0.0"}, | |
"cowlib": {:hex, :cowlib, "1.0.1"}, | |
"plug": {:hex, :plug, "0.12.1"}, | |
"plug_heartbeat": {:hex, :plug_heartbeat, "0.1.0"}, | |
"poison": {:hex, :poison, "1.4.0"}, | |
"ranch": {:hex, :ranch, "1.0.0"}, | |
"trot": {:git, "git://github.com/hexedpackets/trot.git", "6d4ddebfa714cc6b4b0b17f43002b3da9c68d6a2", []}} |
何が変わったのかと言うと、plugのバージョンを"0.13.0"から"0.12.1"に変えています。
ここまで、できたら依存関係を解決しましょう。
>mix deps.get
>mix deps.compile
では続きと行きましょう!!
##3. コンフィグ設定
/config/config.exsを開き、以下の2行を追加して下さい。----
config :trot, :port, 4000
config :trot, :router, TrotSample.Router
----
詳しくは翻訳したreadmeの記事に書いてあります。
##4. router作成
/libのディレクトリへ以下の名称でディレクトリを追加して下さいディレクトリ: trot_sample
/libにあったファイル(trot_sample.ex)は上記の作成したディレクトリへ移動して下さい。
また、以下のようにリネームして下さい。
リネーム: trot_sample.ex → router.ex
/lib/trot_sample/router.exを開き以下の通りソースコードを記述して下さい。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule TrotSample.Router do | |
use Trot.Router | |
get "/", do: 200 | |
get "/hello" do | |
"Hello Trot!!" | |
end | |
import_routes Trot.NotFound | |
end |
では、実行しましょう。
>iex -S mix
以下の二つのURLへアクセスしてみて下さい。
URL1: http://localhost:4000
結果1: 画面には何も表示されない。
URL2: http://localhost:4000/hello
結果2: テキストのみで"Hello Trot!!"と表示される。
また、コマンドプロンプトを確認してみるとログが出ています。
----
iex(1)>
18:59:41.065 [info] GET /
iex(1)>
18:59:41.069 [info] Sent 200 in 4ms
iex(1)>
18:59:46.036 [info] GET /hello
iex(1)>
18:59:46.036 [info] Sent 200 in 0ツオs
----
Tips:
configの設定を行わないと、
どんなページを開いてもデフォルトのNotFoundルータが、not foundと表示しています。
以上で終わりです。
#管理人の独り言~
issue見つけるまで発狂してました。
ってか、記事なさすぎ・・・Phoenix以上に起動で苦労した・・・