mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
server: Make registration implicit
This commit is contained in:
@@ -20,15 +20,6 @@
|
|||||||
package org.isoron.uhabits.core.sync
|
package org.isoron.uhabits.core.sync
|
||||||
|
|
||||||
interface AbstractSyncServer {
|
interface AbstractSyncServer {
|
||||||
/**
|
|
||||||
* Generates and returns a new sync key, which can be used to store and retrive
|
|
||||||
* data.
|
|
||||||
*
|
|
||||||
* @throws ServiceUnavailable If key cannot be generated at this time, for example,
|
|
||||||
* due to insufficient server resources, temporary server maintenance or network problems.
|
|
||||||
*/
|
|
||||||
suspend fun register(): String
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Replaces data for a given sync key.
|
* Replaces data for a given sync key.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -1,39 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2016-2021 Álinson Santos Xavier <git@axavier.org>
|
|
||||||
*
|
|
||||||
* This file is part of Loop Habit Tracker.
|
|
||||||
*
|
|
||||||
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by the
|
|
||||||
* Free Software Foundation, either version 3 of the License, or (at your
|
|
||||||
* option) any later version.
|
|
||||||
*
|
|
||||||
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
|
||||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
||||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
||||||
* more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License along
|
|
||||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.isoron.uhabits.server.app
|
|
||||||
|
|
||||||
import io.ktor.application.call
|
|
||||||
import io.ktor.http.HttpStatusCode
|
|
||||||
import io.ktor.response.respond
|
|
||||||
import io.ktor.routing.Routing
|
|
||||||
import io.ktor.routing.post
|
|
||||||
import org.isoron.uhabits.core.sync.RegisterReponse
|
|
||||||
import org.isoron.uhabits.core.sync.ServiceUnavailable
|
|
||||||
|
|
||||||
fun Routing.registration(app: SyncApplication) {
|
|
||||||
post("/register") {
|
|
||||||
try {
|
|
||||||
val key = app.server.register()
|
|
||||||
call.respond(HttpStatusCode.OK, RegisterReponse(key))
|
|
||||||
} catch (e: ServiceUnavailable) {
|
|
||||||
call.respond(HttpStatusCode.ServiceUnavailable)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -49,8 +49,6 @@ fun Routing.storage(app: SyncApplication) {
|
|||||||
try {
|
try {
|
||||||
app.server.put(key, data)
|
app.server.put(key, data)
|
||||||
call.respond(HttpStatusCode.OK)
|
call.respond(HttpStatusCode.OK)
|
||||||
} catch (e: KeyNotFoundException) {
|
|
||||||
call.respond(HttpStatusCode.NotFound)
|
|
||||||
} catch (e: EditConflictException) {
|
} catch (e: EditConflictException) {
|
||||||
call.respond(HttpStatusCode.Conflict)
|
call.respond(HttpStatusCode.Conflict)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,8 +26,8 @@ import io.ktor.features.ContentNegotiation
|
|||||||
import io.ktor.features.DefaultHeaders
|
import io.ktor.features.DefaultHeaders
|
||||||
import io.ktor.jackson.jackson
|
import io.ktor.jackson.jackson
|
||||||
import io.ktor.routing.routing
|
import io.ktor.routing.routing
|
||||||
import org.isoron.uhabits.server.sync.Repository
|
|
||||||
import org.isoron.uhabits.core.sync.AbstractSyncServer
|
import org.isoron.uhabits.core.sync.AbstractSyncServer
|
||||||
|
import org.isoron.uhabits.server.sync.Repository
|
||||||
import org.isoron.uhabits.server.sync.RepositorySyncServer
|
import org.isoron.uhabits.server.sync.RepositorySyncServer
|
||||||
import java.nio.file.Path
|
import java.nio.file.Path
|
||||||
import java.nio.file.Paths
|
import java.nio.file.Paths
|
||||||
@@ -48,7 +48,6 @@ class SyncApplication(
|
|||||||
jackson { }
|
jackson { }
|
||||||
}
|
}
|
||||||
routing {
|
routing {
|
||||||
registration(this@SyncApplication)
|
|
||||||
storage(this@SyncApplication)
|
storage(this@SyncApplication)
|
||||||
metrics(this@SyncApplication)
|
metrics(this@SyncApplication)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,8 +24,6 @@ import org.isoron.uhabits.core.sync.AbstractSyncServer
|
|||||||
import org.isoron.uhabits.core.sync.EditConflictException
|
import org.isoron.uhabits.core.sync.EditConflictException
|
||||||
import org.isoron.uhabits.core.sync.KeyNotFoundException
|
import org.isoron.uhabits.core.sync.KeyNotFoundException
|
||||||
import org.isoron.uhabits.core.sync.SyncData
|
import org.isoron.uhabits.core.sync.SyncData
|
||||||
import java.util.Random
|
|
||||||
import kotlin.streams.asSequence
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An AbstractSyncServer that stores all data in a [Repository].
|
* An AbstractSyncServer that stores all data in a [Repository].
|
||||||
@@ -40,21 +38,13 @@ class RepositorySyncServer(
|
|||||||
.labelNames("method")
|
.labelNames("method")
|
||||||
.register()
|
.register()
|
||||||
|
|
||||||
override suspend fun register(): String {
|
|
||||||
requestsCounter.labels("register").inc()
|
|
||||||
val key = generateKey()
|
|
||||||
repo.put(key, SyncData(0, ""))
|
|
||||||
return key
|
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun put(key: String, newData: SyncData) {
|
override suspend fun put(key: String, newData: SyncData) {
|
||||||
requestsCounter.labels("put").inc()
|
requestsCounter.labels("put").inc()
|
||||||
if (!repo.contains(key)) {
|
if (repo.contains(key)) {
|
||||||
throw KeyNotFoundException()
|
val prevData = repo.get(key)
|
||||||
}
|
if (newData.version != prevData.version + 1) {
|
||||||
val prevData = repo.get(key)
|
throw EditConflictException()
|
||||||
if (newData.version != prevData.version + 1) {
|
}
|
||||||
throw EditConflictException()
|
|
||||||
}
|
}
|
||||||
repo.put(key, newData)
|
repo.put(key, newData)
|
||||||
}
|
}
|
||||||
@@ -74,20 +64,4 @@ class RepositorySyncServer(
|
|||||||
}
|
}
|
||||||
return repo.get(key).version
|
return repo.get(key).version
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun generateKey(): String {
|
|
||||||
while (true) {
|
|
||||||
val key = randomString(64)
|
|
||||||
if (!repo.contains(key))
|
|
||||||
return key
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun randomString(length: Long): String {
|
|
||||||
val chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
||||||
return Random().ints(length, 0, chars.length)
|
|
||||||
.asSequence()
|
|
||||||
.map(chars::get)
|
|
||||||
.joinToString("")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,51 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2016-2021 Álinson Santos Xavier <git@axavier.org>
|
|
||||||
*
|
|
||||||
* This file is part of Loop Habit Tracker.
|
|
||||||
*
|
|
||||||
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by the
|
|
||||||
* Free Software Foundation, either version 3 of the License, or (at your
|
|
||||||
* option) any later version.
|
|
||||||
*
|
|
||||||
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
|
||||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
||||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
||||||
* more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License along
|
|
||||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.isoron.uhabits.server.app
|
|
||||||
|
|
||||||
import com.nhaarman.mockitokotlin2.whenever
|
|
||||||
import io.ktor.http.HttpMethod
|
|
||||||
import io.ktor.http.HttpStatusCode
|
|
||||||
import io.ktor.server.testing.handleRequest
|
|
||||||
import io.ktor.server.testing.withTestApplication
|
|
||||||
import kotlinx.coroutines.runBlocking
|
|
||||||
import org.isoron.uhabits.core.sync.ServiceUnavailable
|
|
||||||
import org.junit.Test
|
|
||||||
import kotlin.test.assertEquals
|
|
||||||
|
|
||||||
class RegistrationModuleTest : BaseApplicationTest() {
|
|
||||||
@Test
|
|
||||||
fun `when register succeeds should return generated key`(): Unit = runBlocking {
|
|
||||||
whenever(server.register()).thenReturn("ABCDEF")
|
|
||||||
withTestApplication(app()) {
|
|
||||||
val call = handleRequest(HttpMethod.Post, "/register")
|
|
||||||
assertEquals(HttpStatusCode.OK, call.response.status())
|
|
||||||
assertEquals("{\"key\":\"ABCDEF\"}", call.response.content)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun `when registration is unavailable should return 503`(): Unit = runBlocking {
|
|
||||||
whenever(server.register()).thenThrow(ServiceUnavailable())
|
|
||||||
withTestApplication(app()) {
|
|
||||||
val call = handleRequest(HttpMethod.Post, "/register")
|
|
||||||
assertEquals(HttpStatusCode.ServiceUnavailable, call.response.status())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -87,16 +87,6 @@ class StorageModuleTest : BaseApplicationTest() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
fun `when put with invalid key should return 404`(): Unit = runBlocking {
|
|
||||||
whenever(server.put("k1", data1)).thenThrow(KeyNotFoundException())
|
|
||||||
withTestApplication(app()) {
|
|
||||||
handlePut("/db/k1", data1).apply {
|
|
||||||
assertEquals(HttpStatusCode.NotFound, response.status())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `when put with invalid version should return 409 and current data`(): Unit = runBlocking {
|
fun `when put with invalid version should return 409 and current data`(): Unit = runBlocking {
|
||||||
whenever(server.put("k1", data1)).thenThrow(EditConflictException())
|
whenever(server.put("k1", data1)).thenThrow(EditConflictException())
|
||||||
|
|||||||
@@ -32,13 +32,10 @@ class RepositorySyncServerTest {
|
|||||||
|
|
||||||
private val tempdir = Files.createTempDirectory("db")
|
private val tempdir = Files.createTempDirectory("db")
|
||||||
private val server = RepositorySyncServer(Repository(tempdir))
|
private val server = RepositorySyncServer(Repository(tempdir))
|
||||||
private val key = runBlocking { server.register() }
|
private val key = "abcdefgh"
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testUsage(): Unit = runBlocking {
|
fun testUsage(): Unit = runBlocking {
|
||||||
val data0 = SyncData(0, "")
|
|
||||||
assertEquals(server.getData(key), data0)
|
|
||||||
|
|
||||||
val data1 = SyncData(1, "Hello world")
|
val data1 = SyncData(1, "Hello world")
|
||||||
server.put(key, data1)
|
server.put(key, data1)
|
||||||
assertEquals(server.getData(key), data1)
|
assertEquals(server.getData(key), data1)
|
||||||
@@ -54,9 +51,5 @@ class RepositorySyncServerTest {
|
|||||||
assertFailsWith<KeyNotFoundException> {
|
assertFailsWith<KeyNotFoundException> {
|
||||||
server.getData("INVALID")
|
server.getData("INVALID")
|
||||||
}
|
}
|
||||||
|
|
||||||
assertFailsWith<KeyNotFoundException> {
|
|
||||||
server.put("INVALID", data0)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user