diff --git a/uhabits-server/build.gradle.kts b/uhabits-server/build.gradle.kts index 197fd4c30..bfe94836a 100644 --- a/uhabits-server/build.gradle.kts +++ b/uhabits-server/build.gradle.kts @@ -23,9 +23,9 @@ plugins { application id("kotlin") id("com.github.johnrengelman.shadow") version "7.1.0" + id("org.jlleitschuh.gradle.ktlint") } - application { group = "org.isoron.uhabits" version = "0.0.1" diff --git a/uhabits-server/src/main/kotlin/org/isoron/uhabits/sync/SyncData.kt b/uhabits-server/src/main/kotlin/org/isoron/uhabits/sync/SyncData.kt index d60319d6e..65fcd1ae9 100644 --- a/uhabits-server/src/main/kotlin/org/isoron/uhabits/sync/SyncData.kt +++ b/uhabits-server/src/main/kotlin/org/isoron/uhabits/sync/SyncData.kt @@ -19,7 +19,7 @@ package org.isoron.uhabits.sync -import com.fasterxml.jackson.databind.* +import com.fasterxml.jackson.databind.ObjectMapper data class SyncData( val version: Long, @@ -32,4 +32,4 @@ data class GetDataVersionResponse(val version: Long) val defaultMapper = ObjectMapper() fun SyncData.toJson(): String = defaultMapper.writeValueAsString(this) -fun GetDataVersionResponse.toJson(): String = defaultMapper.writeValueAsString(this) \ No newline at end of file +fun GetDataVersionResponse.toJson(): String = defaultMapper.writeValueAsString(this) diff --git a/uhabits-server/src/main/kotlin/org/isoron/uhabits/sync/SyncException.kt b/uhabits-server/src/main/kotlin/org/isoron/uhabits/sync/SyncException.kt index 30689ad86..7a8d615f3 100644 --- a/uhabits-server/src/main/kotlin/org/isoron/uhabits/sync/SyncException.kt +++ b/uhabits-server/src/main/kotlin/org/isoron/uhabits/sync/SyncException.kt @@ -19,10 +19,10 @@ package org.isoron.uhabits.sync -open class SyncException: RuntimeException() +open class SyncException : RuntimeException() -class KeyNotFoundException: SyncException() +class KeyNotFoundException : SyncException() -class ServiceUnavailable: SyncException() +class ServiceUnavailable : SyncException() -class EditConflictException: SyncException() \ No newline at end of file +class EditConflictException : SyncException() diff --git a/uhabits-server/src/main/kotlin/org/isoron/uhabits/sync/app/MetricsModule.kt b/uhabits-server/src/main/kotlin/org/isoron/uhabits/sync/app/MetricsModule.kt index f367dd552..379f96ad8 100644 --- a/uhabits-server/src/main/kotlin/org/isoron/uhabits/sync/app/MetricsModule.kt +++ b/uhabits-server/src/main/kotlin/org/isoron/uhabits/sync/app/MetricsModule.kt @@ -19,15 +19,15 @@ package org.isoron.uhabits.sync.app -import io.ktor.application.* -import io.ktor.http.* -import io.ktor.response.* -import io.ktor.routing.* -import io.prometheus.client.* -import io.prometheus.client.exporter.common.* -import io.prometheus.client.hotspot.* -import java.io.* - +import io.ktor.application.call +import io.ktor.http.HttpStatusCode +import io.ktor.response.respond +import io.ktor.routing.Routing +import io.ktor.routing.get +import io.prometheus.client.CollectorRegistry +import io.prometheus.client.exporter.common.TextFormat +import io.prometheus.client.hotspot.DefaultExports +import java.io.StringWriter fun Routing.metrics(app: SyncApplication) { // Register JVM metrics diff --git a/uhabits-server/src/main/kotlin/org/isoron/uhabits/sync/app/RegistrationModule.kt b/uhabits-server/src/main/kotlin/org/isoron/uhabits/sync/app/RegistrationModule.kt index 1501b243a..2f9d3ef54 100644 --- a/uhabits-server/src/main/kotlin/org/isoron/uhabits/sync/app/RegistrationModule.kt +++ b/uhabits-server/src/main/kotlin/org/isoron/uhabits/sync/app/RegistrationModule.kt @@ -19,11 +19,13 @@ package org.isoron.uhabits.sync.app -import io.ktor.application.* -import io.ktor.http.* -import io.ktor.response.* -import io.ktor.routing.* -import org.isoron.uhabits.sync.* +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.sync.RegisterReponse +import org.isoron.uhabits.sync.ServiceUnavailable fun Routing.registration(app: SyncApplication) { post("/register") { diff --git a/uhabits-server/src/main/kotlin/org/isoron/uhabits/sync/app/StorageModule.kt b/uhabits-server/src/main/kotlin/org/isoron/uhabits/sync/app/StorageModule.kt index 3e67d359c..aa2689513 100644 --- a/uhabits-server/src/main/kotlin/org/isoron/uhabits/sync/app/StorageModule.kt +++ b/uhabits-server/src/main/kotlin/org/isoron/uhabits/sync/app/StorageModule.kt @@ -19,12 +19,18 @@ package org.isoron.uhabits.sync.app -import io.ktor.application.* -import io.ktor.http.* -import io.ktor.request.* -import io.ktor.response.* -import io.ktor.routing.* -import org.isoron.uhabits.sync.* +import io.ktor.application.call +import io.ktor.http.HttpStatusCode +import io.ktor.request.receive +import io.ktor.response.respond +import io.ktor.routing.Routing +import io.ktor.routing.get +import io.ktor.routing.put +import io.ktor.routing.route +import org.isoron.uhabits.sync.EditConflictException +import org.isoron.uhabits.sync.GetDataVersionResponse +import org.isoron.uhabits.sync.KeyNotFoundException +import org.isoron.uhabits.sync.SyncData fun Routing.storage(app: SyncApplication) { route("/db/{key}") { @@ -33,7 +39,7 @@ fun Routing.storage(app: SyncApplication) { try { val data = app.server.getData(key) call.respond(HttpStatusCode.OK, data) - } catch(e: KeyNotFoundException) { + } catch (e: KeyNotFoundException) { call.respond(HttpStatusCode.NotFound) } } @@ -54,9 +60,9 @@ fun Routing.storage(app: SyncApplication) { try { val version = app.server.getDataVersion(key) call.respond(HttpStatusCode.OK, GetDataVersionResponse(version)) - } catch(e: KeyNotFoundException) { + } catch (e: KeyNotFoundException) { call.respond(HttpStatusCode.NotFound) } } } -} \ No newline at end of file +} diff --git a/uhabits-server/src/main/kotlin/org/isoron/uhabits/sync/app/SyncApplication.kt b/uhabits-server/src/main/kotlin/org/isoron/uhabits/sync/app/SyncApplication.kt index 1da1f3a71..2e49f15e5 100644 --- a/uhabits-server/src/main/kotlin/org/isoron/uhabits/sync/app/SyncApplication.kt +++ b/uhabits-server/src/main/kotlin/org/isoron/uhabits/sync/app/SyncApplication.kt @@ -19,13 +19,18 @@ package org.isoron.uhabits.sync.app -import io.ktor.application.* -import io.ktor.features.* -import io.ktor.jackson.* -import io.ktor.routing.* -import org.isoron.uhabits.sync.repository.* -import org.isoron.uhabits.sync.server.* -import java.nio.file.* +import io.ktor.application.Application +import io.ktor.application.install +import io.ktor.features.CallLogging +import io.ktor.features.ContentNegotiation +import io.ktor.features.DefaultHeaders +import io.ktor.jackson.jackson +import io.ktor.routing.routing +import org.isoron.uhabits.sync.repository.FileRepository +import org.isoron.uhabits.sync.server.AbstractSyncServer +import org.isoron.uhabits.sync.server.RepositorySyncServer +import java.nio.file.Path +import java.nio.file.Paths fun Application.main() = SyncApplication().apply { main() } diff --git a/uhabits-server/src/main/kotlin/org/isoron/uhabits/sync/links/LinkManager.kt b/uhabits-server/src/main/kotlin/org/isoron/uhabits/sync/links/LinkManager.kt index 4b153861c..db3daf25f 100644 --- a/uhabits-server/src/main/kotlin/org/isoron/uhabits/sync/links/LinkManager.kt +++ b/uhabits-server/src/main/kotlin/org/isoron/uhabits/sync/links/LinkManager.kt @@ -19,8 +19,8 @@ package org.isoron.uhabits.sync.links -import org.isoron.uhabits.sync.* -import org.isoron.uhabits.sync.utils.* +import org.isoron.uhabits.sync.KeyNotFoundException +import org.isoron.uhabits.sync.utils.randomString class LinkManager( private val timeoutInMillis: Long = 900_000, @@ -46,4 +46,4 @@ class LinkManager( } return link } -} \ No newline at end of file +} diff --git a/uhabits-server/src/main/kotlin/org/isoron/uhabits/sync/repository/FileRepository.kt b/uhabits-server/src/main/kotlin/org/isoron/uhabits/sync/repository/FileRepository.kt index 1faf93fc7..72ec57c5b 100644 --- a/uhabits-server/src/main/kotlin/org/isoron/uhabits/sync/repository/FileRepository.kt +++ b/uhabits-server/src/main/kotlin/org/isoron/uhabits/sync/repository/FileRepository.kt @@ -19,9 +19,10 @@ package org.isoron.uhabits.sync.repository -import org.isoron.uhabits.sync.* -import java.io.* -import java.nio.file.* +import org.isoron.uhabits.sync.KeyNotFoundException +import org.isoron.uhabits.sync.SyncData +import java.io.PrintWriter +import java.nio.file.Path class FileRepository( private val basepath: Path, @@ -70,4 +71,4 @@ class FileRepository( private fun String.toDataPath(): Path { return basepath.resolve("${this[0]}/${this[1]}/${this[2]}/${this[3]}/$this") } -} \ No newline at end of file +} diff --git a/uhabits-server/src/main/kotlin/org/isoron/uhabits/sync/repository/Repository.kt b/uhabits-server/src/main/kotlin/org/isoron/uhabits/sync/repository/Repository.kt index 651ca679f..1e9817ad3 100644 --- a/uhabits-server/src/main/kotlin/org/isoron/uhabits/sync/repository/Repository.kt +++ b/uhabits-server/src/main/kotlin/org/isoron/uhabits/sync/repository/Repository.kt @@ -43,4 +43,3 @@ interface Repository { */ suspend fun contains(key: String): Boolean } - diff --git a/uhabits-server/src/main/kotlin/org/isoron/uhabits/sync/server/AbstractSyncServer.kt b/uhabits-server/src/main/kotlin/org/isoron/uhabits/sync/server/AbstractSyncServer.kt index bd18c7b30..2c7153f4e 100644 --- a/uhabits-server/src/main/kotlin/org/isoron/uhabits/sync/server/AbstractSyncServer.kt +++ b/uhabits-server/src/main/kotlin/org/isoron/uhabits/sync/server/AbstractSyncServer.kt @@ -19,8 +19,11 @@ package org.isoron.uhabits.sync.server -import org.isoron.uhabits.sync.* -import org.isoron.uhabits.sync.links.* +import org.isoron.uhabits.sync.EditConflictException +import org.isoron.uhabits.sync.KeyNotFoundException +import org.isoron.uhabits.sync.ServiceUnavailable +import org.isoron.uhabits.sync.SyncData +import org.isoron.uhabits.sync.links.Link interface AbstractSyncServer { /** diff --git a/uhabits-server/src/main/kotlin/org/isoron/uhabits/sync/server/RepositorySyncServer.kt b/uhabits-server/src/main/kotlin/org/isoron/uhabits/sync/server/RepositorySyncServer.kt index cdde98d91..9dbf0cbe3 100644 --- a/uhabits-server/src/main/kotlin/org/isoron/uhabits/sync/server/RepositorySyncServer.kt +++ b/uhabits-server/src/main/kotlin/org/isoron/uhabits/sync/server/RepositorySyncServer.kt @@ -19,11 +19,14 @@ package org.isoron.uhabits.sync.server -import io.prometheus.client.* -import org.isoron.uhabits.sync.* -import org.isoron.uhabits.sync.links.* -import org.isoron.uhabits.sync.repository.* -import org.isoron.uhabits.sync.utils.* +import io.prometheus.client.Counter +import org.isoron.uhabits.sync.EditConflictException +import org.isoron.uhabits.sync.KeyNotFoundException +import org.isoron.uhabits.sync.SyncData +import org.isoron.uhabits.sync.links.Link +import org.isoron.uhabits.sync.links.LinkManager +import org.isoron.uhabits.sync.repository.Repository +import org.isoron.uhabits.sync.utils.randomString /** * An AbstractSyncServer that stores all data in a [Repository]. diff --git a/uhabits-server/src/main/kotlin/org/isoron/uhabits/sync/utils/String.kt b/uhabits-server/src/main/kotlin/org/isoron/uhabits/sync/utils/String.kt index da043764f..6427b326b 100644 --- a/uhabits-server/src/main/kotlin/org/isoron/uhabits/sync/utils/String.kt +++ b/uhabits-server/src/main/kotlin/org/isoron/uhabits/sync/utils/String.kt @@ -19,8 +19,8 @@ package org.isoron.uhabits.sync.utils -import java.util.* -import kotlin.streams.* +import java.util.Random +import kotlin.streams.asSequence fun randomString(length: Long): String { val chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" @@ -28,4 +28,4 @@ fun randomString(length: Long): String { .asSequence() .map(chars::get) .joinToString("") -} \ No newline at end of file +} diff --git a/uhabits-server/src/test/kotlin/org/isoron/uhabits/sync/app/BaseApplicationTest.kt b/uhabits-server/src/test/kotlin/org/isoron/uhabits/sync/app/BaseApplicationTest.kt index 7b5c6d27b..07111720d 100644 --- a/uhabits-server/src/test/kotlin/org/isoron/uhabits/sync/app/BaseApplicationTest.kt +++ b/uhabits-server/src/test/kotlin/org/isoron/uhabits/sync/app/BaseApplicationTest.kt @@ -20,8 +20,8 @@ package org.isoron.uhabits.sync.app import com.nhaarman.mockitokotlin2.mock -import io.ktor.application.* -import org.isoron.uhabits.sync.server.* +import io.ktor.application.Application +import org.isoron.uhabits.sync.server.AbstractSyncServer open class BaseApplicationTest { diff --git a/uhabits-server/src/test/kotlin/org/isoron/uhabits/sync/app/StorageModuleTest.kt b/uhabits-server/src/test/kotlin/org/isoron/uhabits/sync/app/StorageModuleTest.kt index 7609587e0..5d6d69321 100644 --- a/uhabits-server/src/test/kotlin/org/isoron/uhabits/sync/app/StorageModuleTest.kt +++ b/uhabits-server/src/test/kotlin/org/isoron/uhabits/sync/app/StorageModuleTest.kt @@ -21,12 +21,23 @@ package org.isoron.uhabits.sync.app import com.nhaarman.mockitokotlin2.verify import com.nhaarman.mockitokotlin2.whenever -import io.ktor.http.* -import io.ktor.server.testing.* -import kotlinx.coroutines.* -import org.isoron.uhabits.sync.* +import io.ktor.http.ContentType +import io.ktor.http.HttpHeaders +import io.ktor.http.HttpMethod +import io.ktor.http.HttpStatusCode +import io.ktor.server.testing.TestApplicationCall +import io.ktor.server.testing.TestApplicationEngine +import io.ktor.server.testing.handleRequest +import io.ktor.server.testing.setBody +import io.ktor.server.testing.withTestApplication +import kotlinx.coroutines.runBlocking +import org.isoron.uhabits.sync.EditConflictException +import org.isoron.uhabits.sync.GetDataVersionResponse +import org.isoron.uhabits.sync.KeyNotFoundException +import org.isoron.uhabits.sync.SyncData +import org.isoron.uhabits.sync.toJson import org.junit.Test -import kotlin.test.* +import kotlin.test.assertEquals class StorageModuleTest : BaseApplicationTest() { private val data1 = SyncData(1, "Hello world") @@ -64,7 +75,6 @@ class StorageModuleTest : BaseApplicationTest() { } } - @Test fun `when put succeeds should return OK`(): Unit = runBlocking { withTestApplication(app()) { diff --git a/uhabits-server/src/test/kotlin/org/isoron/uhabits/sync/repository/FileRepositoryTest.kt b/uhabits-server/src/test/kotlin/org/isoron/uhabits/sync/repository/FileRepositoryTest.kt index 53ce3a8c6..589d56b2a 100644 --- a/uhabits-server/src/test/kotlin/org/isoron/uhabits/sync/repository/FileRepositoryTest.kt +++ b/uhabits-server/src/test/kotlin/org/isoron/uhabits/sync/repository/FileRepositoryTest.kt @@ -17,17 +17,18 @@ * with this program. If not, see . */ - @file:Suppress("BlockingMethodInNonBlockingContext") package org.isoron.uhabits.sync.repository -import kotlinx.coroutines.* -import org.hamcrest.CoreMatchers.* -import org.isoron.uhabits.sync.* -import org.junit.* -import org.junit.Assert.* -import java.nio.file.* +import kotlinx.coroutines.runBlocking +import org.hamcrest.CoreMatchers.equalTo +import org.isoron.uhabits.sync.SyncData +import org.junit.Assert.assertEquals +import org.junit.Assert.assertThat +import org.junit.Assert.assertTrue +import org.junit.Test +import java.nio.file.Files class FileRepositoryTest { @@ -50,4 +51,4 @@ class FileRepositoryTest { val retrieved = repo.get("abcdefg") assertThat(retrieved, equalTo(original)) } -} \ No newline at end of file +} diff --git a/uhabits-server/src/test/kotlin/org/isoron/uhabits/sync/server/RepositorySyncServerTest.kt b/uhabits-server/src/test/kotlin/org/isoron/uhabits/sync/server/RepositorySyncServerTest.kt index ca1576bda..cbe71e027 100644 --- a/uhabits-server/src/test/kotlin/org/isoron/uhabits/sync/server/RepositorySyncServerTest.kt +++ b/uhabits-server/src/test/kotlin/org/isoron/uhabits/sync/server/RepositorySyncServerTest.kt @@ -19,12 +19,15 @@ package org.isoron.uhabits.sync.server -import kotlinx.coroutines.* -import org.isoron.uhabits.sync.* -import org.isoron.uhabits.sync.repository.* +import kotlinx.coroutines.runBlocking +import org.isoron.uhabits.sync.EditConflictException +import org.isoron.uhabits.sync.KeyNotFoundException +import org.isoron.uhabits.sync.SyncData +import org.isoron.uhabits.sync.repository.FileRepository import org.junit.Test -import java.nio.file.* -import kotlin.test.* +import java.nio.file.Files +import kotlin.test.assertEquals +import kotlin.test.assertFailsWith class RepositorySyncServerTest { @@ -57,4 +60,4 @@ class RepositorySyncServerTest { server.put("INVALID", data0) } } -} \ No newline at end of file +}