65
Server Side Kotlin Kotlin Web フフフフフフフフフフ フ 3 フ フフフフ Kotlin フフフ #jkug @yy_yank

Server Side Kotlin

  • Upload
    yy-yank

  • View
    1.709

  • Download
    2

Embed Size (px)

Citation preview

Page 1: Server Side Kotlin

Server Side Kotlin Kotlin Web フレームワークの現状

第 3 回 かわいい Kotlin 勉強会 #jkug @yy_yank

Page 2: Server Side Kotlin

自己紹介

          こいつです          ・ vimmer なのに          サクラエディタアイコン          ・小物 SIer          ・ Java と Kotlin が好きヤンク (@yy_yank)

Page 3: Server Side Kotlin

・関西Kotlin勉強会参加 (2014/09)・Kotlinアドベントカレンダー2014とか書いた・逆引きKotlinというサイトを作った・ twitter で Kotlin で検索して巡回 ( 毎日 )

僕の Kotlin 活動

Page 4: Server Side Kotlin

What is Server Side?

Page 5: Server Side Kotlin

・ Client Side <-> Server Side・ Http (Get,Post,Put,Delete...)・ MVC 2.0・ Component・ REST・ Servlet・ Apache Stru…

What is Server Side ?

Page 6: Server Side Kotlin
Page 7: Server Side Kotlin

Web Framework の話をしよう

Page 8: Server Side Kotlin

・ Kotlin の Web フレームワーク

・ Java の Web フレームワークとかも考える

・まとめ

Agenda

Page 9: Server Side Kotlin

Kotlin の Web フレームワーク、いくつ知ってますか?

Page 10: Server Side Kotlin

・ Kara・ wasabi・ Node.kt・ Kool・ vertx-kotlin

Kotlin 製の Web フレームワーク

Page 11: Server Side Kotlin
Page 12: Server Side Kotlin

・ Kara・ wasabi・ Node.kt・ Kool・ vertx-kotlin

Kotlin 製の Web フレームワーク

Page 13: Server Side Kotlin

・ Kara・ wasabi・ Node.kt 6 months ago・ Kool 3 years ago・ vertx-kotlin 3 years ago

Kotlin 製の Web フレームワーク

Page 14: Server Side Kotlin

誰も保守しないオレオレフレームワークやないか!!!

Page 15: Server Side Kotlin

・ GitHub で Kotlin を検索すると、3 years ago とか 2 years ago が多い

・我々は今を生きている

Kotlin 製の Web フレームワーク

Page 16: Server Side Kotlin

・ Kara・ wasabi・ Node.kt

実質はこの 3 つ

Page 17: Server Side Kotlin

・ Kara・ wasabi・ Node.kt

Kotlin 製の Web フレームワーク①

Page 18: Server Side Kotlin

・名前通り Node.js の模倣的なもの・ベースは Netty・ Node.kt は割と Active な方だが大体 wasabi で同等の機能が網羅されている・独自にベーシック認証や暗号化復号化機能を持っているぐらい

Node.kt は正直微妙

Page 19: Server Side Kotlin

何が微妙かってソースが汚い

Page 20: Server Side Kotlin

Node.kt は正直微妙

fun listen(port: Int? = null) {

var aPort = port;

if (aPort == null) {

aPort = get("port") as Int

}

if (aPort == null) {

      throw IllegalStateException("hogehoge")

………(略)

Page 21: Server Side Kotlin

Node.kt は正直微妙

fun listen(port: Int? = null) {

var aPort = port;

if (aPort == null) {

aPort = get("port") as Int

}

if (aPort == null) {

      throw IllegalStateException("hogehoge")

………(略)

デフォルト値が null

2回目の nullチェック

変数使いまわして代入

Page 22: Server Side Kotlin

しぶい

Page 23: Server Side Kotlin

せめてこうとか…

fun listen(port: Int?) {

var aPort = port ?: get("port")?.toInt()

if (aPort == null)

      throw IllegalStateException("hogehoge")

}

Page 24: Server Side Kotlin

・ Kara・ wasabi・ Node.kt

Kotlin 製の Web フレームワーク②

Page 25: Server Side Kotlin

・ JetBrains の Hadi Hariri のプロジェクト・ Http FrameWork( らしい )・ベースは Netty・ Interceptor が優秀・ Model や View は含まれず、かなり軽量・ WebSocket にも対応したっぽい?

wasabi

Page 26: Server Side Kotlin

・ Netty のアップ / ダウンストリームに合わせてライフ・サイクルが形成されているPreRequestPreExecusionPostExecusionPostRequestError

wasabi

Page 27: Server Side Kotlin

server.get("/", { val log = Log() log.info("URI requested is${request.uri}") next() },{ response.send("Hello World!") })

wasabi

Page 28: Server Side Kotlin

server.get("/", { val log = Log() log.info("URI requested is${request.uri}") next() },{ response.send("Hello World!") })

wasabi

ルーティング

Page 29: Server Side Kotlin

server.get("/", { val log = Log() log.info("URI requested is${request.uri}") next() },{ response.send("Hello World!") })

wasabiRequestの処理

Page 30: Server Side Kotlin

server.get("/", { val log = Log() log.info("URI requested is${request.uri}") next() },{ response.send("Hello World!") })

wasabi

Responseの処理

Page 31: Server Side Kotlin

・逆引き Kotlin を wasabi で作ってみました( 宣伝 )

Html + bootstrap + wasabi という感じhttps://github.com/yyYank/kotlin-rev-solution-wasabi

wasabi

Page 32: Server Side Kotlin

・ Kara・ wasabi・ Node.kt

Kotlin 製の Web フレームワーク③

Page 33: Server Side Kotlin

・ TinyMission というアメリカのミネソタ州の Web サービスとモバイル系の会社が開発開始・ (M)VC フレームワーク・全てが .kt になる (html も css もロジックも )・ Jetty + kootstrap(bootstrap の Kotlin 版 ) +Kotlin 製 Html テンプレートエンジン

Kara

Page 34: Server Side Kotlin
Page 35: Server Side Kotlin

( ˘⊖˘) 。 o( ん、待てよ…? )

Page 36: Server Side Kotlin
Page 37: Server Side Kotlin

JetBrains

JetBrains

JetBrains

JetBrains

JetBrains

TinyMission

Page 38: Server Side Kotlin

_人人人人人人人人人人_ > ほとんど JetBrains <  ̄ Y^Y^Y^Y^Y^Y^Y^Y^Y  ̄

Page 39: Server Side Kotlin

‘’Currently, it is actively developed by JetBrains,

the company behind Kotlin.‘’

Kara

・現在 Kara を引っ張っているのはJetBrains

Page 40: Server Side Kotlin

( ˘⊖˘) 。 o( あんしん )

Page 41: Server Side Kotlin

Get("/test")

class Test() : Request({

TextResult("This is a test action, yo")

})

Get("/crash")

class Crash() : Request({

error("This resource crashes")

})

Kara のルーティング

Page 42: Server Side Kotlin

Get("/test")

class Test() : Request({

TextResult("This is a test action, yo")

})

Get("/crash")

class Crash() : Request({

error("This resource crashes")

})

Kara のルーティング

ルーティング

Page 43: Server Side Kotlin

Get("/test")

class Test() : Request({

TextResult("This is a test action, yo")

})

Get("/crash")

class Crash() : Request({

error("This resource crashes")

})

Kara のルーティング

Responseの処理

Page 44: Server Side Kotlin

Get("/test")

class Test() : Request({

TextResult("This is a test action, yo")

})

Get("/crash")

class Crash() : Request({

error("This resource crashes")

})

Kara のルーティング

View のクラス

Page 45: Server Side Kotlin

table {

setClass(fields)

tr {

td {

setClass(cLabel)

labelFor("title")

}

td {

submitButton("Submit")

}

}

}

}

Kara の View

Page 46: Server Side Kotlin

table {

setClass(fields)

tr {

td {

setClass(cLabel)

labelFor("title")

}

td {

submitButton("Submit")

}

}

}

}

Kara の View のメリット

・タグの対応が簡単

・もちろん静的型付け→ 書き間違いなし!!

Page 47: Server Side Kotlin

table {

setClass(fields)

tr {

td {

setClass(cLabel)

labelFor("title")

}

td {

submitButton("Submit")

}

}

}

}

Kara の View のデメリット

デザイナーさんとの分業とかだとちょっと覚えてもらう必要

…がある 。

Page 48: Server Side Kotlin

table(fields) {

td {

padding = box(6.px, 3.px)

}

td.c(cLabel) {

textAlign = TextAlign.right

}

td(cLabel, top) {

verticalAlign = VerticalAlign.top

}

}

Kara の CSS

Page 49: Server Side Kotlin

なぜ、 Kotlin ライブラリとして、 vert.xや node.js ライクにやろうとするのか

Page 50: Server Side Kotlin

・ルーティング処理などの関数の引数として関数渡しをするようなシグネチャが作りやすい-> 必然的に node.js のような書き方になる

関数リテラルと相性が良い

Page 51: Server Side Kotlin

inline fun HtmlBodyTag.table(contents:

TABLE.() -> Unit) = contentTag(TABLE(this),

contents)

大体こういう感じ

Page 52: Server Side Kotlin

inline fun HtmlBodyTag.table(contents:

TABLE.() -> Unit) = contentTag(TABLE(this),

contents)

大体こういう感じ

Page 53: Server Side Kotlin

・ライブラリ実装

TABLE.() -> Unit・使う側

table {

}

大体こういう感じ

Page 54: Server Side Kotlin

その他も考える

Page 55: Server Side Kotlin

・ Spring Boot・ JAX-RS(JSR339)・ MVC 1.0(JSR371)

Java の Web フレームワーク

Page 56: Server Side Kotlin

・ Spring Boot・ JAX-RS(JSR339)・ MVC 1.0(JSR371)

Java の Web フレームワーク①

Page 57: Server Side Kotlin

・やってみた【Kotlin】KotlinでもJava EE 8 のMVC1.0(Ozark)は動くよ

・結構辛さあった

MVC1.0(JSR371)

Page 58: Server Side Kotlin

・ Spring Boot・ JAX-RS(JSR339)・ MVC 1.0(JSR371)

Java の Web フレームワーク②

Page 59: Server Side Kotlin

・うらがみ (backpaper0) さんがやってた

KotlinでJAX-RS + おまけ

・あんまり Java と変わらなかった

JAX-RS(JSR339)

Page 60: Server Side Kotlin

・ Spring Boot・ JAX-RS(JSR339)・ MVC 1.0(JSR371)

Java の Web フレームワーク③

Page 61: Server Side Kotlin

・しおしお (@_siosio_) さんがやってたKotlinでSpring Bootしてみた・ Hadi Hariri もやってたCreating a RESTful Web Service with Spring Boot

・地味に辛さあるけどまあまあ

Spring Boot

Page 62: Server Side Kotlin

・ Java EE は Java で使ったほうが良い・使うとしたら Spring Boot かも知れない

Java の WebFW with Kotlin?

Page 63: Server Side Kotlin

・ Kara の低レイヤな部分が参考になりやすいのでそれをベースに作ってみるのも良いかも-> HttpServlet を継承してその上にApplication クラスを上乗せするような形情報の受け渡しは ApplicationContext がやっている様子自分でつくるから安心!負担は増える!

オレオレ Web フレームワーク

Page 64: Server Side Kotlin

・一番手厚くて今後見込みありそうなのはKara・ wasabi もすでに完成形なので使えるけどフルスタックというよりは軽量なもの・ Spring Boot もまあまあアリ・更なる Kotlin らしさを求めるならプルリクか自作だ

まとめ

Page 65: Server Side Kotlin