Kaynağa Gözat

Upgrade HTTP4K version with associated API changes (#2856)

* adding undertow implementation

* fixes to setup scripts

* fixes to setup scripts

* fixes to setup scripts

* fixes to setup scripts

* fixes to setup scripts

* fixes to setup scripts

* upgrade version of http4k

* adding fortunes and worlds tests

* some fixes for database driver

* fix number of records returned

* set content type on view

* set content type on json

* fix max and min

* adding new fortune to list and sorting result

* upgrade http4k and add content length header

* upgrade http4k and associated API changes

* upgrade http4k and associated API changes

* upgrade http4k and associated API changes
David Denton 8 yıl önce
ebeveyn
işleme
2acc793341

+ 1 - 1
frameworks/Kotlin/http4k/build.gradle

@@ -1,6 +1,6 @@
 buildscript {
     ext.kotlin_version = "1.1.2-4"
-    ext.http4k_version = "1.23.0"
+    ext.http4k_version = "2.2.1"
     repositories {
         mavenCentral()
         jcenter()

+ 2 - 2
frameworks/Kotlin/http4k/src/main/kotlin/FortunesRoute.kt

@@ -5,7 +5,7 @@ import org.http4k.core.Response
 import org.http4k.core.Status.Companion.OK
 import org.http4k.core.with
 import org.http4k.routing.Route
-import org.http4k.routing.by
+import org.http4k.routing.bind
 import org.http4k.template.HandlebarsTemplates
 import org.http4k.template.ViewModel
 import org.http4k.template.view
@@ -18,7 +18,7 @@ object FortunesRoute {
 
     private val viewBody = Body.view(HandlebarsTemplates().CachingClasspath(), TEXT_HTML)
 
-    operator fun invoke(database: Database): Route = GET to "fortunes" by {
+    operator fun invoke(database: Database): Route = "/fortunes" to GET bind {
         val items = database.withConnection {
             it.prepareStatement("select * from fortune").executeQuery().toList {
                 Fortune(it.getInt(1), it.getString(2))

+ 8 - 5
frameworks/Kotlin/http4k/src/main/kotlin/Http4kBenchmarkServer.kt

@@ -9,12 +9,15 @@ import java.util.TimeZone.getTimeZone
 object Http4kBenchmarkServer {
     private val dateFormat = getInstance("EEE, d MMM yyyy HH:mm:ss 'GMT'", getTimeZone("GMT"))
 
-    private val dateAndServer = Filter {
+    private val headers = Filter {
         next ->
         {
-            next(it)
-                .header("Server", "Example")
-                .header("Date", dateFormat.format(System.currentTimeMillis()))
+            next(it).let {
+                it
+                    .header("Server", "Example")
+                    .header("Date", dateFormat.format(System.currentTimeMillis()))
+                    .header("Content-Length", it.body.payload.remaining().toString())
+            }
         }
     }
 
@@ -25,5 +28,5 @@ object Http4kBenchmarkServer {
         FortunesRoute(database)
     ).plus(WorldRoutes(database))
 
-    fun start(config: ServerConfig) = dateAndServer.then(routes(*routes.toTypedArray())).asServer(config).start().block()
+    fun start(config: ServerConfig) = headers.then(routes(*routes.toTypedArray())).asServer(config).start().block()
 }

+ 2 - 2
frameworks/Kotlin/http4k/src/main/kotlin/JsonRoute.kt

@@ -6,10 +6,10 @@ import org.http4k.core.with
 import org.http4k.format.Jackson.json
 import org.http4k.format.Jackson.obj
 import org.http4k.format.Jackson.string
-import org.http4k.routing.by
+import org.http4k.routing.bind
 
 object JsonRoute {
     private val jsonBody = Body.json().toLens()
 
-    operator fun invoke() = GET to "/json" by { Response(OK).with(jsonBody of obj("message" to string("Hello, World!"))) }
+    operator fun invoke() = "/json" to GET bind { Response(OK).with(jsonBody of obj("message" to string("Hello, World!"))) }
 }

+ 2 - 3
frameworks/Kotlin/http4k/src/main/kotlin/PlainTextRoute.kt

@@ -1,4 +1,3 @@
-
 import org.http4k.asByteBuffer
 import org.http4k.core.Body
 import org.http4k.core.ContentType.Companion.TEXT_PLAIN
@@ -7,12 +6,12 @@ import org.http4k.core.Response
 import org.http4k.core.Status.Companion.OK
 import org.http4k.core.with
 import org.http4k.lens.binary
-import org.http4k.routing.by
+import org.http4k.routing.bind
 
 object PlainTextRoute {
     private val preAllocatedHelloWorldText = "Hello, World!".asByteBuffer()
 
     private val plainTextBody = Body.binary(TEXT_PLAIN).toLens()
 
-    operator fun invoke() = GET to "/plaintext" by { Response(OK).with(plainTextBody of preAllocatedHelloWorldText) }
+    operator fun invoke() = "/plaintext" to GET bind { Response(OK).with(plainTextBody of preAllocatedHelloWorldText) }
 }

+ 4 - 5
frameworks/Kotlin/http4k/src/main/kotlin/WorldRoutes.kt

@@ -1,4 +1,3 @@
-
 import com.fasterxml.jackson.databind.JsonNode
 import org.http4k.core.Body
 import org.http4k.core.Method.GET
@@ -12,7 +11,7 @@ import org.http4k.format.Jackson.number
 import org.http4k.format.Jackson.obj
 import org.http4k.lens.Query
 import org.http4k.routing.Route
-import org.http4k.routing.by
+import org.http4k.routing.bind
 import java.lang.Math.max
 import java.lang.Math.min
 import java.sql.Connection
@@ -42,13 +41,13 @@ object WorldRoutes {
             updateRoute(database)
         )
 
-    private fun queryRoute(database: Database): Route = GET to "db" by {
+    private fun queryRoute(database: Database): Route = "/db" to GET bind {
         database.withConnection {
             findWorld(it, randomWorld())
         }?.let { Response(OK).with(jsonBody of it) } ?: Response(NOT_FOUND)
     }
 
-    private fun multipleRoute(database: Database): Route = GET to "queries" by {
+    private fun multipleRoute(database: Database): Route = "/queries" to GET bind {
         val worlds = database.withConnection {
             con ->
             (1..numberOfQueries(it)).mapNotNull { findWorld(con, randomWorld()) }
@@ -56,7 +55,7 @@ object WorldRoutes {
         Response(OK).with(jsonBody of array(worlds))
     }
 
-    private fun updateRoute(database: Database): Route = GET to "updates" by {
+    private fun updateRoute(database: Database): Route = "/updates" to GET bind {
         val worlds = database.withConnection {
             con ->
             (1..numberOfQueries(it)).mapNotNull {