CrystalでもRailsを
使いたいですか?
Masayuki Uchida @etmnk
2016. 1. 22
東京 Crystal 勉強会 #3 in 恵比寿
はい
いいえ
そんなことよりSinatraだ
はい
いいえ
そんなことよりSinatraだ →
Kemalはいいぞ
お前 is 誰?
twitter : @etmnk
github : @etmnk
Agenda
1. Crystal Awesome
2. WebFrameworksの大雑把な分類
3. Frostとは
4. Frostを試してみる
5. デモ
6. 取りあえず現時点で出来ること
7. Rackに関して
8. まとめ
Crystal Web Frameworks
https://github.com/veelenga/awesome-crystal
分類
Sinatra系
moonshine
kemal
Rails系
Amethyst
Frost
分類
Sinatra系
moonshine
kemal
Rails系
Amethyst
Frost
いいぞ
Frost
• 現時点でもある程度動く
• ver 0.2.0
• Crystal v0.10.2に対応
Frostを動かしてみる
$ git clone https://github.com/ysbaddaden/frost.git
$ crystal frost/src/cli.cr -- new frost_sample
$ cd frost_sample
$ crystal deps install
Frostを動かしてみる
$ git clone https://github.com/ysbaddaden/frost.git
$ crystal frost/src/cli.cr -- new frost_sample
$ cd frost_sample
$ crystal deps install
Oh..
その前に
https://github.com/ysbaddaden/frost/pull/10
パッチを当てる
$ git fetch origin pull/10/head:fix-project-generation
$ git checkout fix-project-generation
気を取り直して
$ crystal frost/src/cli.cr -- new frost_sample
Generated
create
create Makefile
create frost_sample.cr
create shard.yml
create .gitignore
create app
create app/controllers
create app/controllers/application_controller.cr
create app/models
create app/models/.keep
create app/views
create app/views/application_view.cr
create app/views/layouts_view.cr
create app/views/layouts
create app/views/layouts/application.html.ecr
create config
create config/routes.cr
create config/environment.cr
create config/application.cr
(中略)
インストール続き
$ cd frost_sample
$ crystal deps install
公式のサンプル参考
https://github.com/ysbaddaden/frost/tree/master/guides
補足1 DB作成に関して
$ createdb frost_sample_development
$ createdb frost_sample_test
$ cp config/database.yml{.example,}
補足1 DB作成に関して
development:
adapter: postgres
database: frost_sample_development
username: postgres
password:
test:
adapter: postgres
database: frost_sample_test
username: postgres
password:
config/database.yml
補足1 DB作成に関して
export DATABASE_URL=postgres://$USER@localhost/frost_sample_development
補足2
$make run
補足2
エラー(中略)
補足2 手修正
instantiating 'with_output_buffer()'
in ./app/views/layouts/application.html.ecr:5: instantiating 'name()'
<title><%= name %></title>
デモ
現時点でできること
• config/route.crの設定に関して
• resources :posts, only: %i(show index new
create)
• edit deleteは未対応
現時点でできること
• Modelの操作に関して(取り敢えずコード見たレベル)
• 検索処理
• #{ModelName}.find(id)
• #{ModelName}.find_by({column1: hoge ,
culumn2: huga })
• #{ModelName}.all.pluck( column1 ) #未確認
現時点でできること
• Modelの操作に関して(取り敢えずコード見たレベル)
• 追加
• hoge = Hoge.new
• hoge.column1 = aa
• hoge.column2 = bb
• hoge.save
現時点でできること
• Modelの操作に関して(取り敢えずコード見たレベ
ル)
• 削除処理 #全体的に未確認(更新も同様なので省略)
• #{ModelName}.delete_all
• hoge = Hoge.find(id)
• hoge.delete # 未確認
Rackに関して
https://groups.google.com/forum/?fromgroups#!searchin/crystal-lang/rack/crystal-lang/xO2eo7pbDVw/
Mp7zVw0PHgAJ
Rackに関して
自前ハンドラを作る場合
require "http/server"
class HogeHandler < HTTP::Handler
def call(request)
response = call_next(request) #次の階層に渡す
# 処理
response
end
end
Rackに関して
handlers = [
Frost::Server::LogHandler.new,
Frost::Server::HttpsEverywhereHandler.new(308),
HTTP::DeflateHandler.new,
Frost::Server::PublicFileHandler.new(File.join(Frost.root, "public"))
]
dispatcher = FrostSample::Dispatcher.new
server = HTTP::Server.new(host, port, handlers) do ¦request¦
dispatcher.call(request)
end
./#{appname}.cr
まとめ
• 少しでも変わった使い方したらエラー
• バグを見つけたら俺が直す、ぐらいの勢いが必要
• ただし全体的な枠組みは出来つつある
• それでも
• 普通にWebApp作りたい場合はどうする?
Kemalはいいぞ

CrystalでもRailsを使いたいですか?