mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 17:18:52 -06:00
Apply ktlint to uhabits-server
This commit is contained in:
@@ -23,9 +23,9 @@ plugins {
|
|||||||
application
|
application
|
||||||
id("kotlin")
|
id("kotlin")
|
||||||
id("com.github.johnrengelman.shadow") version "7.1.0"
|
id("com.github.johnrengelman.shadow") version "7.1.0"
|
||||||
|
id("org.jlleitschuh.gradle.ktlint")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
application {
|
application {
|
||||||
group = "org.isoron.uhabits"
|
group = "org.isoron.uhabits"
|
||||||
version = "0.0.1"
|
version = "0.0.1"
|
||||||
|
|||||||
@@ -19,7 +19,7 @@
|
|||||||
|
|
||||||
package org.isoron.uhabits.sync
|
package org.isoron.uhabits.sync
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.*
|
import com.fasterxml.jackson.databind.ObjectMapper
|
||||||
|
|
||||||
data class SyncData(
|
data class SyncData(
|
||||||
val version: Long,
|
val version: Long,
|
||||||
|
|||||||
@@ -19,15 +19,15 @@
|
|||||||
|
|
||||||
package org.isoron.uhabits.sync.app
|
package org.isoron.uhabits.sync.app
|
||||||
|
|
||||||
import io.ktor.application.*
|
import io.ktor.application.call
|
||||||
import io.ktor.http.*
|
import io.ktor.http.HttpStatusCode
|
||||||
import io.ktor.response.*
|
import io.ktor.response.respond
|
||||||
import io.ktor.routing.*
|
import io.ktor.routing.Routing
|
||||||
import io.prometheus.client.*
|
import io.ktor.routing.get
|
||||||
import io.prometheus.client.exporter.common.*
|
import io.prometheus.client.CollectorRegistry
|
||||||
import io.prometheus.client.hotspot.*
|
import io.prometheus.client.exporter.common.TextFormat
|
||||||
import java.io.*
|
import io.prometheus.client.hotspot.DefaultExports
|
||||||
|
import java.io.StringWriter
|
||||||
|
|
||||||
fun Routing.metrics(app: SyncApplication) {
|
fun Routing.metrics(app: SyncApplication) {
|
||||||
// Register JVM metrics
|
// Register JVM metrics
|
||||||
|
|||||||
@@ -19,11 +19,13 @@
|
|||||||
|
|
||||||
package org.isoron.uhabits.sync.app
|
package org.isoron.uhabits.sync.app
|
||||||
|
|
||||||
import io.ktor.application.*
|
import io.ktor.application.call
|
||||||
import io.ktor.http.*
|
import io.ktor.http.HttpStatusCode
|
||||||
import io.ktor.response.*
|
import io.ktor.response.respond
|
||||||
import io.ktor.routing.*
|
import io.ktor.routing.Routing
|
||||||
import org.isoron.uhabits.sync.*
|
import io.ktor.routing.post
|
||||||
|
import org.isoron.uhabits.sync.RegisterReponse
|
||||||
|
import org.isoron.uhabits.sync.ServiceUnavailable
|
||||||
|
|
||||||
fun Routing.registration(app: SyncApplication) {
|
fun Routing.registration(app: SyncApplication) {
|
||||||
post("/register") {
|
post("/register") {
|
||||||
|
|||||||
@@ -19,12 +19,18 @@
|
|||||||
|
|
||||||
package org.isoron.uhabits.sync.app
|
package org.isoron.uhabits.sync.app
|
||||||
|
|
||||||
import io.ktor.application.*
|
import io.ktor.application.call
|
||||||
import io.ktor.http.*
|
import io.ktor.http.HttpStatusCode
|
||||||
import io.ktor.request.*
|
import io.ktor.request.receive
|
||||||
import io.ktor.response.*
|
import io.ktor.response.respond
|
||||||
import io.ktor.routing.*
|
import io.ktor.routing.Routing
|
||||||
import org.isoron.uhabits.sync.*
|
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) {
|
fun Routing.storage(app: SyncApplication) {
|
||||||
route("/db/{key}") {
|
route("/db/{key}") {
|
||||||
|
|||||||
@@ -19,13 +19,18 @@
|
|||||||
|
|
||||||
package org.isoron.uhabits.sync.app
|
package org.isoron.uhabits.sync.app
|
||||||
|
|
||||||
import io.ktor.application.*
|
import io.ktor.application.Application
|
||||||
import io.ktor.features.*
|
import io.ktor.application.install
|
||||||
import io.ktor.jackson.*
|
import io.ktor.features.CallLogging
|
||||||
import io.ktor.routing.*
|
import io.ktor.features.ContentNegotiation
|
||||||
import org.isoron.uhabits.sync.repository.*
|
import io.ktor.features.DefaultHeaders
|
||||||
import org.isoron.uhabits.sync.server.*
|
import io.ktor.jackson.jackson
|
||||||
import java.nio.file.*
|
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() }
|
fun Application.main() = SyncApplication().apply { main() }
|
||||||
|
|
||||||
|
|||||||
@@ -19,8 +19,8 @@
|
|||||||
|
|
||||||
package org.isoron.uhabits.sync.links
|
package org.isoron.uhabits.sync.links
|
||||||
|
|
||||||
import org.isoron.uhabits.sync.*
|
import org.isoron.uhabits.sync.KeyNotFoundException
|
||||||
import org.isoron.uhabits.sync.utils.*
|
import org.isoron.uhabits.sync.utils.randomString
|
||||||
|
|
||||||
class LinkManager(
|
class LinkManager(
|
||||||
private val timeoutInMillis: Long = 900_000,
|
private val timeoutInMillis: Long = 900_000,
|
||||||
|
|||||||
@@ -19,9 +19,10 @@
|
|||||||
|
|
||||||
package org.isoron.uhabits.sync.repository
|
package org.isoron.uhabits.sync.repository
|
||||||
|
|
||||||
import org.isoron.uhabits.sync.*
|
import org.isoron.uhabits.sync.KeyNotFoundException
|
||||||
import java.io.*
|
import org.isoron.uhabits.sync.SyncData
|
||||||
import java.nio.file.*
|
import java.io.PrintWriter
|
||||||
|
import java.nio.file.Path
|
||||||
|
|
||||||
class FileRepository(
|
class FileRepository(
|
||||||
private val basepath: Path,
|
private val basepath: Path,
|
||||||
|
|||||||
@@ -43,4 +43,3 @@ interface Repository {
|
|||||||
*/
|
*/
|
||||||
suspend fun contains(key: String): Boolean
|
suspend fun contains(key: String): Boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -19,8 +19,11 @@
|
|||||||
|
|
||||||
package org.isoron.uhabits.sync.server
|
package org.isoron.uhabits.sync.server
|
||||||
|
|
||||||
import org.isoron.uhabits.sync.*
|
import org.isoron.uhabits.sync.EditConflictException
|
||||||
import org.isoron.uhabits.sync.links.*
|
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 {
|
interface AbstractSyncServer {
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -19,11 +19,14 @@
|
|||||||
|
|
||||||
package org.isoron.uhabits.sync.server
|
package org.isoron.uhabits.sync.server
|
||||||
|
|
||||||
import io.prometheus.client.*
|
import io.prometheus.client.Counter
|
||||||
import org.isoron.uhabits.sync.*
|
import org.isoron.uhabits.sync.EditConflictException
|
||||||
import org.isoron.uhabits.sync.links.*
|
import org.isoron.uhabits.sync.KeyNotFoundException
|
||||||
import org.isoron.uhabits.sync.repository.*
|
import org.isoron.uhabits.sync.SyncData
|
||||||
import org.isoron.uhabits.sync.utils.*
|
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].
|
* An AbstractSyncServer that stores all data in a [Repository].
|
||||||
|
|||||||
@@ -19,8 +19,8 @@
|
|||||||
|
|
||||||
package org.isoron.uhabits.sync.utils
|
package org.isoron.uhabits.sync.utils
|
||||||
|
|
||||||
import java.util.*
|
import java.util.Random
|
||||||
import kotlin.streams.*
|
import kotlin.streams.asSequence
|
||||||
|
|
||||||
fun randomString(length: Long): String {
|
fun randomString(length: Long): String {
|
||||||
val chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
val chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||||
|
|||||||
@@ -20,8 +20,8 @@
|
|||||||
package org.isoron.uhabits.sync.app
|
package org.isoron.uhabits.sync.app
|
||||||
|
|
||||||
import com.nhaarman.mockitokotlin2.mock
|
import com.nhaarman.mockitokotlin2.mock
|
||||||
import io.ktor.application.*
|
import io.ktor.application.Application
|
||||||
import org.isoron.uhabits.sync.server.*
|
import org.isoron.uhabits.sync.server.AbstractSyncServer
|
||||||
|
|
||||||
open class BaseApplicationTest {
|
open class BaseApplicationTest {
|
||||||
|
|
||||||
|
|||||||
@@ -21,12 +21,23 @@ package org.isoron.uhabits.sync.app
|
|||||||
|
|
||||||
import com.nhaarman.mockitokotlin2.verify
|
import com.nhaarman.mockitokotlin2.verify
|
||||||
import com.nhaarman.mockitokotlin2.whenever
|
import com.nhaarman.mockitokotlin2.whenever
|
||||||
import io.ktor.http.*
|
import io.ktor.http.ContentType
|
||||||
import io.ktor.server.testing.*
|
import io.ktor.http.HttpHeaders
|
||||||
import kotlinx.coroutines.*
|
import io.ktor.http.HttpMethod
|
||||||
import org.isoron.uhabits.sync.*
|
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 org.junit.Test
|
||||||
import kotlin.test.*
|
import kotlin.test.assertEquals
|
||||||
|
|
||||||
class StorageModuleTest : BaseApplicationTest() {
|
class StorageModuleTest : BaseApplicationTest() {
|
||||||
private val data1 = SyncData(1, "Hello world")
|
private val data1 = SyncData(1, "Hello world")
|
||||||
@@ -64,7 +75,6 @@ class StorageModuleTest : BaseApplicationTest() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `when put succeeds should return OK`(): Unit = runBlocking {
|
fun `when put succeeds should return OK`(): Unit = runBlocking {
|
||||||
withTestApplication(app()) {
|
withTestApplication(app()) {
|
||||||
|
|||||||
@@ -17,17 +17,18 @@
|
|||||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
@file:Suppress("BlockingMethodInNonBlockingContext")
|
@file:Suppress("BlockingMethodInNonBlockingContext")
|
||||||
|
|
||||||
package org.isoron.uhabits.sync.repository
|
package org.isoron.uhabits.sync.repository
|
||||||
|
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.runBlocking
|
||||||
import org.hamcrest.CoreMatchers.*
|
import org.hamcrest.CoreMatchers.equalTo
|
||||||
import org.isoron.uhabits.sync.*
|
import org.isoron.uhabits.sync.SyncData
|
||||||
import org.junit.*
|
import org.junit.Assert.assertEquals
|
||||||
import org.junit.Assert.*
|
import org.junit.Assert.assertThat
|
||||||
import java.nio.file.*
|
import org.junit.Assert.assertTrue
|
||||||
|
import org.junit.Test
|
||||||
|
import java.nio.file.Files
|
||||||
|
|
||||||
class FileRepositoryTest {
|
class FileRepositoryTest {
|
||||||
|
|
||||||
|
|||||||
@@ -19,12 +19,15 @@
|
|||||||
|
|
||||||
package org.isoron.uhabits.sync.server
|
package org.isoron.uhabits.sync.server
|
||||||
|
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.runBlocking
|
||||||
import org.isoron.uhabits.sync.*
|
import org.isoron.uhabits.sync.EditConflictException
|
||||||
import org.isoron.uhabits.sync.repository.*
|
import org.isoron.uhabits.sync.KeyNotFoundException
|
||||||
|
import org.isoron.uhabits.sync.SyncData
|
||||||
|
import org.isoron.uhabits.sync.repository.FileRepository
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
import java.nio.file.*
|
import java.nio.file.Files
|
||||||
import kotlin.test.*
|
import kotlin.test.assertEquals
|
||||||
|
import kotlin.test.assertFailsWith
|
||||||
|
|
||||||
class RepositorySyncServerTest {
|
class RepositorySyncServerTest {
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user