the industrial

ブログと言うより自分のためのメモ以外の何モノでもないです。でも読んでくださってありがとうございます。

Play Framework / Scala 再入門 1日目

お題の通りです。

久しぶりに作りたいと思ったアイデアがあり個人プロジェクトを始動した中で、ScalaMatsuri の影響で今ものすごくScalaを書きたくなっていたのもあり、サーバーサイドを一番好きな言語「Scala」で作ろうと決めました。

こう見えてもともと独学2年、実務2年ほど Scala を書いていた経歴があり、さらに ScalaMatsuri のスタッフはやらさせてもらっていましたのでまあ余裕だろうと思っていたのですが、なんと Play Framework のプロジェクトの雛形を作るところから詰まってしまった次第。

ここ2年ほどは Rails と Nuxt を書いていたのもあって、Scala界隈のトレンディーな某についても分からず、Scalaの世界では完全に浦島太郎状態になっていました。 ~Activatorってなくなったんですかね...。~

なので、今一度イチからすべて勉強し直そうと思い、「Play Framework / Scala 再入門」と題してこのシリーズをはじめました。

Play/Scalaで作りたいもの

さて、まずはサーバーサイドだけでもどういったもの(要件)を作りたいのか整理してみます。

そして、これらをシリーズを通してブログにアウトプット出来たらなと思います。

  • Play/Scalaを利用する
  • REST API として JSON を返却する( Reads / Writes の使い方を思い出す)
  • CRUD処理が出来る
  • MySQLを利用する(一番使い慣れているRDBなので)
  • DBアクセスライブラリとして、ScalikeJDBCを利用する
  • 内部からHTTP通信を行う(WSで良いのかな?他にも良さげなのがあるか、あとで調べる)
  • CRON処理を実装する(昔実装した様な気がするけど、どうしてたかな・・・あとで調べる)
  • Eff触ってみたいので導入する... 参考

単語だけ覚えていますが詳細は忘れているコトだらけです(ツライ)。

とりあえず1日目の目標として、環境構築とPlayframeworkのHello, Worldまでやります

さて、取り急ぎ mac os の sbt を最新にしちゃいます。

$ brew upgrade sbt
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> Updated Formulae
argon2

==> Upgrading 1 outdated package:
sbt 1.2.6 -> 1.2.8
==> Upgrading sbt
==> Downloading https://github.com/sbt/sbt/releases/download/v1.2.8/sbt-1.2.8.tgz
==> Downloading from https://github-production-release-asset-2e65be.s3.amazonaws.com/279553/ae845a00-0be0-11e9-99d2-03a5afa9f1c7?X-Amz-
######################################################################## 100.0%
==> Caveats
You can use $SBT_OPTS to pass additional JVM options to sbt.
Project specific options should be placed in .sbtopts in the root of your project.
Global settings should be placed in /usr/local/etc/sbtopts
==> Summary
🍺  /usr/local/Cellar/sbt/1.2.8: 521 files, 50MB, built in 50 seconds
==> `brew cleanup` has not been run in 30 days, running now...
Removing: /Users/XXXXXX/Library/Caches/Homebrew/readline--8.0.0.mojave.bottle.tar.gz... (516.7KB)
Removing: /usr/local/Cellar/sbt/1.2.6... (521 files, 49.8MB)
Removing: /Users/XXXXXX/Library/Caches/Homebrew/libtiff--4.0.10_1.mojave.bottle.tar.gz... (1MB)
Removing: /Users/XXXXXX/Library/Caches/Homebrew/mysql@5.7--5.7.25.mojave.bottle.tar.gz... (72.4MB)

次に、公式サイトに行きPlayの最新版が2.7.xだということを突き止めました(そこからw)。

www.playframework.com

例に習ってPlayの雛形を作成します。

$ sbt new playframework/play-scala-seed.g8

This template generates a Play Scala project

name [play-scala-seed]: hoge-api
organization [com.example]: omiend

Template applied in /Users/hogehoge/api

次に、作成したディレクトリにcdして

$ sbt run

すると

$ cd api/
$ sbt run
[info] Loading settings for project global-plugins from metals.sbt ...
[info] Loading global plugins from /Users/hogehoge/.sbt/1.0/plugins
[info] Updating ProjectRef(uri("file:/Users/hogehoge/.sbt/1.0/plugins/"), "global-plugins")...
[info] Done updating.
[info] Loading settings for project api-build from plugins.sbt ...
[info] Loading project definition from /Users/hogehoge/my/GhostMoose/api/project
[info] Updating ProjectRef(uri("file:/Users/hogehoge/my/GhostMoose/api/project/"), "api-build")...
[info] Done updating.
[warn] There may be incompatibilities among your library dependencies; run 'evicted' to see detailed eviction warnings.
[info] Loading settings for project root from build.sbt ...
[info] Set current project to api (in build file:/Users/hogehoge/my/GhostMoose/api/)
[info] Updating ...
[info] downloading https://repo1.maven.org/maven2/com/typesafe/ssl-config-core_2.13/0.3.8/ssl-config-core_2.13-0.3.8.jar ...
[info]  [SUCCESSFUL ] com.typesafe#ssl-config-core_2.13;0.3.8!ssl-config-core_2.13.jar(bundle) (1742ms)
[info] Done updating.
[warn] There may be incompatibilities among your library dependencies; run 'evicted' to see detailed eviction warnings.

--- (Running the application, auto-reloading is enabled) ---

[info] p.c.s.AkkaHttpServer - Listening for HTTP on /0:0:0:0:0:0:0:0:9000

(Server started, use Enter to stop and go back to the console...)

みたいになるので、

http://localhost:9000/ にアクセスすれば、Play/Scala の Hello, World 完了です!

f:id:omiend:20190703230341p:plain

流石にここまでは余裕で行けて、ちょっと安心しましたw

その他

Metals

そう言えば先日の ScalaMatsuri で行われた「お絵かきで学ぶScala教室」にて Metals を紹介されていて、すでに入れていました!

.gitignore

project/target.gitignore しましょう。

project/target

続き

omiend.hatenablog.jp