Skip to content

Instantly share code, notes, and snippets.

@halcat0x15a
Last active November 18, 2015 07:56
Show Gist options
  • Select an option

  • Save halcat0x15a/65f2a05ca26597c188bf to your computer and use it in GitHub Desktop.

Select an option

Save halcat0x15a/65f2a05ca26597c188bf to your computer and use it in GitHub Desktop.

!SLIDE

ScalaとOpenCVとAkka HTTPとReact.jsで作る麻雀点数計算アプリケーション

halcat0x15a

!SLIDE

この資料について

Akka HTTPがメイン

React.jsとOpenCVの話はないです

scalaVersion := "2.11.7"
libraryDependencies += "com.typesafe.akka" %% "akka-http-spray-json-experimental" % "1.0"

!SLIDE

前回のあらすじ

  • Scalaで
  • OpenCV使って
  • 牌認識をした

!SLIDE

麻雀点数計算アプリケーションとは

手牌の写真を撮ると自動で点数計算してくれるWebアプリケーション

!SLIDE

なぜWebアプリケーションか

  • Androidがめんどい
  • 麻雀やる人「iPhoneから使えると嬉しいんだけど」
  • React.js使ってみたかった

!SLIDE

なぜAkka HTTPか

  • これからのPlayで使われるらしい
  • Sprayさわったことがある
  • スタンドアロンで動く
  • 必要最低限のものは揃っている感じ

!SLIDE

Minimalな例

import scala.concurrent.Await
import scala.concurrent.duration.Duration

import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import akka.http.scaladsl.Http
import akka.http.scaladsl.server.Directives._

object Main extends App {
  implicit val system = ActorSystem()
  implicit val materializer = ActorMaterializer()

  val route =
    path("hello") {
      get {
        complete {
          "Hello, world!"
        }
      }
    }

  Await.ready(Http().bindAndHandle(route, "localhost", 8080), Duration.Inf)
}

!SLIDE

今回気に入ったところ

静的なリソースを簡単に参照できる

path("") {
  redirect("public/index.html", StatusCodes.MovedPermanently)
} ~ pathPrefix("public") {
  encodeResponse {
    getFromResourceDirectory("public")
  }
}

!SLIDE

JSONの扱いが楽

import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
import spray.json._, DefaultJsonProtocol._

case class User(id: Long, name: String)
implicit val userFormat = jsonFormat2(User)

var users: List[User] = Nil

path("user") {
  get {
    complete {
      users
    }
  } ~ post {
    entity(as[User]) { user =>
      users ::= user
      complete {
        users
      }
    }
  }
}

!SLIDE

気に入らないところ

  • Sourceの扱いがめんどい
    • Source[ByteString, Any]くらい簡単に使えるようにしてほしい
  • akka-streamの変更が激しいのでAPIがどんどん変わる
    • マイナーバージョンが違うだけでもMinimal Exampleがコンパイルできない
  • pathの中に"/"入れてハマった
    • エラー出してほしい

!SLIDE

デモとかコード解説とか

!SLIDE

まとめ

  • akka-httpそこそこ使える
  • でもプロダクションでは使わない方がいいとおもいました
  • React.js最高だった
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment