LoginSignup
12
4

More than 3 years have passed since last update.

Scala と Play flamework 入門

Last updated at Posted at 2019-02-20

対象読者

  • 何かしらWebフレームワークで一通りアプリを作ったことがあるけど Scala, Playflamework が気になる人。
  • コピペではなくドキュメントを遡って読んでバージョンの変化などに対応できる人
  • Scalaをオワコンだと思っていない人

環境

  • Play flamework 2.7
  • intellij IDEA
  • sbt
  • MySQL

上記の通りある程度基本的に知識はある人向けに、自分的にサクサク進む順序で進めていきます。

プロジェクトの作成

brewからsbtをインストールします

$ brew install sbt

intellijsbtscalaのプラグインを追加します。
再度intellijを開き、 create new project > (左)scala > (右)play
で 右下の create ポチで出来上がり。

DBまわり

ORM(Skinny)、DBの設定

ebeanよりSkinnyORMのほうがよさげなので後者を導入します。
ついでにしれっと MySQL の ドライバ も必要なので追加。

参照 :

conf/application.confbuild.sbt に設定を追加します。

conf/application.conf

db {
 default {
    driver="com.mysql.jdbc.Driver"
    url="jdbc:mysql://localhost:[port]/[dbname]"
    user="[username]"
    password="[password]"
    logSql=true
  }
}
build.sbt
libraryDependencies ++= Seq( jdbc , ehcache , ws , specs2 % Test , guice ,
  "org.skinny-framework" % "skinny-orm" % "3.0.0",
  "mysql" % "mysql-connector-java" % "5.1.27",
  "ch.qos.logback" %  "logback-classic" % "1.1.+"
)

migration

使い勝手がいいのがこいつ

flyway-play

./conf/db 配下に sql を置いていけば勝手に migration してくれる。

playapp
├── app
│   ├── controllers
│   ├── models
│   └── views
├── conf
│   ├── application.conf
│   ├── db
│   │   └── migration
│   │       ├── development
│   │       │   ├── migration.sql
│   │       │   └── seed.sql
│   │       └── deploy
│   │           ├── migration.sql
│   │           └── dump.sql
│   ├── play.plugins
│   └── routes

Authまわり (ユーザー管理)

これ良さげなんだけど。
参照 : https://github.com/joscha/play-authenticate

パスワードのハッシュ化

残念ながらScala向けのハッシュ化ライブラリがなかったので、(最近Springを触っている俺的に)安定している Spring Flamework の BCyptPasswordEncoder を使います。

ルーティング

app/conf直下の route ファイルに書いていきます。
ベタ書きで自由自在に定義できる神仕様。
これならクラスの構成を考えればアクションの所在が単純明快。

最高。

app/conf/routes.
# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~

# An example controller showing a sample home page
GET     /                           controllers.HomeController.index
# ログイン画面
GET     /login                      controllers.AuthController.loginPage
# ユーザー登録画面
GET     /signup                     controllers.AuthController.signUpPage

# ログインAPI
POST    /login                      controllers.AuthController.login

アクション

request => result 型の関数 Actionを定義するとcontrollerの役割を果たします。

echo.scala
class Echo @Inject()(cc: ControllerComponents) 
  extends AbstructController(cc)
{
  def echo = Action { request =>
    Ok("Got request [" + request + "]")
  }
}

ここで言う request は HTTPリクエスト、result は HTTPレスポンスと捉えて勝手にスッキリしています。厳密には こちら

requestの扱い (body parser)

参照:https://www.playframework.com/documentation/2.6.x/ScalaBodyParsers
大抵 API通信の場合asFormUrlEncodedを使ってリクエストの body を parse します。

12
4
0

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
12
4