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 f4c9829b087188893e7eba301f99a1ef89e3b441..bbe6a8f4c269c7413b8cf92a7b0de9fb4c3f6333 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 56233b647c0e98a08fbd3d157a8da0fa24880a99..14f915ced206c0d93afe393372e3202dad375bfb 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 37550a3823539a0ab420a0b060c9c0f6a9a6b0aa..e769a84119f3cfd827290d7c12fc3662be9e6125 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 }