Browse Source

http4k - tidy, adding virtual hosting to apache server implementation (#5980)

* tidy imports and code
adding virtual hosting to apache server implementation

* upgrade http4k

* readded missed project directory
David Denton 5 years ago
parent
commit
f5045eb26b

+ 39 - 4
frameworks/Kotlin/http4k/apache/src/main/kotlin/Http4kApacheServer.kt

@@ -1,6 +1,41 @@
-
-import org.http4k.server.ApacheServer
+import org.apache.hc.core5.http.impl.bootstrap.ServerBootstrap
+import org.apache.hc.core5.http.io.SocketConfig
+import org.http4k.core.HttpHandler
+import org.http4k.server.Http4kRequestHandler
+import org.http4k.server.Http4kServer
+import org.http4k.server.ServerConfig
 
 fun main() {
-    Http4kBenchmarkServer(PostgresDatabase("tfb-database")).start(ApacheServer(9000, canonicalHostname = "tfb-server"))
-}
+    /**
+     * we need a custom config here because of how virtual hosting is required in the TFB
+     * environment. Normally we would just call the inbuilt ApacheServer(9000) function
+     */
+    val config = TfbApacheServer(9000)
+    Http4kBenchmarkServer(PostgresDatabase("tfb-database")).start(config)
+}
+
+private class TfbApacheServer(val port: Int) : ServerConfig {
+    override fun toServer(httpHandler: HttpHandler): Http4kServer = object : Http4kServer {
+        val handler = Http4kRequestHandler(httpHandler)
+
+        val server = ServerBootstrap.bootstrap()
+            .setListenerPort(port)
+            .setSocketConfig(SocketConfig.custom()
+                .setTcpNoDelay(true)
+                .setSoKeepAlive(true)
+                .setSoReuseAddress(true)
+                .setBacklogSize(1000)
+                .build())
+            .apply {
+                register("*", handler) // standard hosting
+                registerVirtual("10.0.0.1", "*", handler) // for virtual hosting
+                setCanonicalHostName("tfb-server")
+            }.create()
+
+        override fun start() = apply { server.start() }
+
+        override fun stop() = apply { server.stop() }
+
+        override fun port(): Int = port
+    }
+}

+ 0 - 1
frameworks/Kotlin/http4k/apache4/src/main/kotlin/Http4kApache4Server.kt

@@ -1,4 +1,3 @@
-
 import org.http4k.server.Apache4Server
 
 fun main() {

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

@@ -1,6 +1,6 @@
 buildscript {
     ext.kotlin_version = "1.3.72"
-    ext.http4k_version = "3.259.0"
+    ext.http4k_version = "3.260.0"
 
     repositories {
         mavenCentral()

+ 13 - 13
frameworks/Kotlin/http4k/core/src/main/kotlin/Http4kBenchmarkServer.kt

@@ -14,26 +14,26 @@ object Http4kBenchmarkServer {
         {
             next(it).let {
                 it.headers(listOf(
-                        "Server" to "http4k",
-                        "Content-Length" to it.body.length.toString(),
-                        "Date" to if (addDate) dateFormat.format(System.currentTimeMillis()) else null
+                    "Server" to "http4k",
+                    "Content-Length" to it.body.length.toString(),
+                    "Date" to if (addDate) dateFormat.format(System.currentTimeMillis()) else null
                 ))
             }
         }
     }
 
     operator fun invoke(database: Database, addDateHeader: Boolean = true) =
-            headers(addDateHeader).then(
-                    routes(
-                            JsonRoute(),
-                            PlainTextRoute(),
-                            FortunesRoute(database),
-                            WorldRoutes.queryRoute(database),
-                            WorldRoutes.updateRoute(database),
-                            WorldRoutes.multipleRoute(database),
-                            WorldRoutes.cachedRoute(database)
-                    )
+        headers(addDateHeader).then(
+            routes(
+                JsonRoute(),
+                PlainTextRoute(),
+                FortunesRoute(database),
+                WorldRoutes.queryRoute(database),
+                WorldRoutes.updateRoute(database),
+                WorldRoutes.multipleRoute(database),
+                WorldRoutes.cachedRoute(database)
             )
+        )
 }
 
 fun HttpHandler.start(config: ServerConfig) = asServer(config).start().block()

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

@@ -1,11 +1,9 @@
-import org.http4k.core.Body
 import org.http4k.core.ContentType.Companion.TEXT_PLAIN
 import org.http4k.core.Method.GET
 import org.http4k.core.Response
 import org.http4k.core.Status.Companion.OK
 import org.http4k.core.with
 import org.http4k.lens.Header.CONTENT_TYPE
-import org.http4k.lens.binary
 import org.http4k.routing.bind
 
 object PlainTextRoute {

+ 0 - 1
frameworks/Kotlin/http4k/jetty/src/main/kotlin/Http4kJettyServer.kt

@@ -1,4 +1,3 @@
-
 import org.http4k.server.Jetty
 
 fun main() {

+ 0 - 1
frameworks/Kotlin/http4k/netty/src/main/kotlin/Http4kNettyServer.kt

@@ -1,4 +1,3 @@
-
 import org.http4k.server.Netty
 
 fun main() {

+ 0 - 1
frameworks/Kotlin/http4k/ratpack/src/main/kotlin/Http4kRatpackServer.kt

@@ -1,4 +1,3 @@
-
 import org.http4k.server.Ratpack
 
 fun main() {

+ 0 - 1
frameworks/Kotlin/http4k/sunhttp/src/main/kotlin/Http4kSunHttpServer.kt

@@ -1,4 +1,3 @@
-
 import org.http4k.server.SunHttp
 
 fun main() {

+ 0 - 1
frameworks/Kotlin/http4k/undertow/src/main/kotlin/Http4kUndertowServer.kt

@@ -1,4 +1,3 @@
-
 import org.http4k.server.Undertow
 
 fun main() {