From 09e9459279bf43990721ee44e97602cb2e11c0d9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Sebastian=20K=C3=BCrten?= <sebastian@mobanisto.de>
Date: Wed, 29 Sep 2021 11:32:11 +0200
Subject: [PATCH] WIP

---
 .../org/briarproject/mailbox/core/server/Routing.kt | 13 ++++++++++++-
 .../mailbox/core/server/WebServerManager.kt         |  6 ++++--
 .../mailbox/core/server/WebServerModule.kt          |  9 +++++++--
 3 files changed, 23 insertions(+), 5 deletions(-)

diff --git a/mailbox-core/src/main/java/org/briarproject/mailbox/core/server/Routing.kt b/mailbox-core/src/main/java/org/briarproject/mailbox/core/server/Routing.kt
index f4c9829b..bbe6a8f4 100644
--- a/mailbox-core/src/main/java/org/briarproject/mailbox/core/server/Routing.kt
+++ b/mailbox-core/src/main/java/org/briarproject/mailbox/core/server/Routing.kt
@@ -6,9 +6,20 @@ import io.ktor.http.ContentType
 import io.ktor.response.respondText
 import io.ktor.routing.get
 import io.ktor.routing.routing
+import org.briarproject.mailbox.core.db.Database
+import java.sql.Connection
 
-internal fun Application.configureRouting() = routing {
+internal fun Application.configureRouting(database: Database<Connection>) = routing {
     get("/") {
         call.respondText("Hello world!", ContentType.Text.Plain)
     }
+    get("/contacts/{id}") {
+        val id = Integer.parseInt(call.parameters["id"])
+        val txn = database.startTransaction()
+
+        val contact = database.getContact(txn, id)
+        if (contact != null) {
+            call.respondText("${contact.id}", ContentType.Text.Plain)
+        }
+    }
 }
diff --git a/mailbox-core/src/main/java/org/briarproject/mailbox/core/server/WebServerManager.kt b/mailbox-core/src/main/java/org/briarproject/mailbox/core/server/WebServerManager.kt
index 56233b64..14f915ce 100644
--- a/mailbox-core/src/main/java/org/briarproject/mailbox/core/server/WebServerManager.kt
+++ b/mailbox-core/src/main/java/org/briarproject/mailbox/core/server/WebServerManager.kt
@@ -4,13 +4,15 @@ import io.ktor.application.install
 import io.ktor.features.CallLogging
 import io.ktor.server.engine.embeddedServer
 import io.ktor.server.netty.Netty
+import org.briarproject.mailbox.core.db.Database
 import org.briarproject.mailbox.core.lifecycle.Service
 import org.slf4j.LoggerFactory.getLogger
+import java.sql.Connection
 import javax.inject.Inject
 import javax.inject.Singleton
 
 @Singleton
-class WebServerManager @Inject constructor() : Service {
+class WebServerManager @Inject constructor(private val database: Database<Connection>) : Service {
 
     internal companion object {
         internal const val PORT = 8000
@@ -20,7 +22,7 @@ class WebServerManager @Inject constructor() : Service {
     private val server by lazy {
         embeddedServer(Netty, PORT, watchPaths = emptyList()) {
             install(CallLogging)
-            configureRouting()
+            configureRouting(database)
         }
     }
 
diff --git a/mailbox-core/src/main/java/org/briarproject/mailbox/core/server/WebServerModule.kt b/mailbox-core/src/main/java/org/briarproject/mailbox/core/server/WebServerModule.kt
index 37550a38..e769a841 100644
--- a/mailbox-core/src/main/java/org/briarproject/mailbox/core/server/WebServerModule.kt
+++ b/mailbox-core/src/main/java/org/briarproject/mailbox/core/server/WebServerModule.kt
@@ -4,7 +4,9 @@ import dagger.Module
 import dagger.Provides
 import dagger.hilt.InstallIn
 import dagger.hilt.components.SingletonComponent
+import org.briarproject.mailbox.core.db.Database
 import org.briarproject.mailbox.core.lifecycle.LifecycleManager
+import java.sql.Connection
 import javax.inject.Singleton
 
 @Module
@@ -13,8 +15,11 @@ internal class WebServerModule {
 
     @Provides
     @Singleton
-    fun provideWebServer(lifecycleManager: LifecycleManager): WebServerManager {
-        val webServerManager = WebServerManager()
+    fun provideWebServer(
+        lifecycleManager: LifecycleManager,
+        database: Database<Connection>,
+    ): WebServerManager {
+        val webServerManager = WebServerManager(database)
         lifecycleManager.registerService(webServerManager)
         return webServerManager
     }
-- 
GitLab