wear backend/common stub
This commit is contained in:
+23
@@ -0,0 +1,23 @@
|
||||
package ru.shadowsparky.gigawear.backend
|
||||
import org.koin.core.annotation.ComponentScan
|
||||
import org.koin.core.annotation.Configuration
|
||||
import org.koin.core.annotation.Module
|
||||
import org.koin.core.annotation.Single
|
||||
import ru.shadowsparky.backend.data.EnvFetcher
|
||||
import ru.shadowsparky.backend.domain.JwtInfo
|
||||
|
||||
@Module
|
||||
@ComponentScan
|
||||
@Configuration
|
||||
class BackendModule {
|
||||
|
||||
@Single
|
||||
fun provideJwtInfo(envFetcher: EnvFetcher): JwtInfo {
|
||||
return JwtInfo(
|
||||
envFetcher.get("GIGA_WEAR_SECRET", "secret"),
|
||||
"https://shadowsparky.ru/",
|
||||
"https://wear.shadowsparky.ru/",
|
||||
"wear"
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package ru.shadowsparky.gigawear.backend
|
||||
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.server.application.Application
|
||||
import io.ktor.server.response.respond
|
||||
import io.ktor.server.routing.get
|
||||
import io.ktor.server.routing.routing
|
||||
import org.koin.core.annotation.KoinApplication
|
||||
import org.koin.core.component.KoinComponent
|
||||
import org.koin.plugin.module.dsl.startKoin
|
||||
|
||||
@KoinApplication
|
||||
object GigaWearApp
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
startKoin<GigaWearApp>()
|
||||
io.ktor.server.netty.EngineMain.main(args)
|
||||
}
|
||||
|
||||
fun Application.module() {
|
||||
val koin = object : KoinComponent {}
|
||||
this.routing {
|
||||
get("health") {
|
||||
this.call.respond(HttpStatusCode.OK)
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package ru.shadowsparky.gigawear.backend.data
|
||||
|
||||
import org.koin.core.annotation.Factory
|
||||
import ru.shadowsparky.chat.backend.domain.ChatService
|
||||
|
||||
@Factory
|
||||
class ClientIdProviderImpl : ChatService.ClientIdProvider {
|
||||
override suspend fun provide(userId: Long): String {
|
||||
return "gigawear-$userId"
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package ru.shadowsparky.gigawear.backend.data
|
||||
|
||||
import org.koin.core.annotation.Factory
|
||||
import ru.shadowsparky.backend.domain.LoginVerifier
|
||||
|
||||
@Factory
|
||||
class LoginVerifierImpl : LoginVerifier {
|
||||
override suspend fun verify(login: String) {
|
||||
}
|
||||
}
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
package ru.shadowsparky.gigawear.backend.data
|
||||
|
||||
import org.koin.core.annotation.Single
|
||||
import ru.shadowsparky.chat.backend.domain.MessageStorage
|
||||
import ru.shadowsparky.chat.domain.Message
|
||||
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import java.util.concurrent.atomic.AtomicLong
|
||||
|
||||
@Single
|
||||
class MessageStorageImpl : MessageStorage {
|
||||
private val storage = ConcurrentHashMap<Long, MutableList<Message>>()
|
||||
private val idGenerator = AtomicLong(1)
|
||||
|
||||
override suspend fun query(
|
||||
userId: Long,
|
||||
afterId: Long?,
|
||||
limit: Int
|
||||
): List<Message> {
|
||||
val userMessages = storage[userId] ?: return emptyList()
|
||||
synchronized(userMessages) {
|
||||
val filteredList = if (afterId != null) {
|
||||
val index = userMessages.indexOfFirst { it.id == afterId }
|
||||
if (index != -1 && index + 1 < userMessages.size) {
|
||||
userMessages.subList(index + 1, userMessages.size)
|
||||
} else {
|
||||
emptyList()
|
||||
}
|
||||
} else {
|
||||
userMessages
|
||||
}
|
||||
return filteredList.take(limit).toList()
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun insert(
|
||||
userId: Long,
|
||||
message: Message
|
||||
): Long {
|
||||
val userMessages = storage.computeIfAbsent(userId) { ArrayList() }
|
||||
val newId = idGenerator.getAndIncrement()
|
||||
val messageToInsert = message.copy(id = newId)
|
||||
synchronized(userMessages) {
|
||||
userMessages.add(messageToInsert)
|
||||
}
|
||||
return newId
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
ktor {
|
||||
deployment {
|
||||
port = 8181
|
||||
}
|
||||
application {
|
||||
modules = [ ru.shadowsparky.gigawear.backend.MainKt.module ]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{YYYY-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
<root level="debug">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
<logger name="org.eclipse.jetty" level="INFO" />
|
||||
<logger name="io.netty" level="INFO" />
|
||||
</configuration>
|
||||
Reference in New Issue
Block a user