This commit is contained in:
InsanusMokrassar 2020-11-27 20:35:09 +06:00
parent 3a8d29e739
commit b9f7d9e346
6 changed files with 17 additions and 107 deletions

View File

@ -1,27 +0,0 @@
package dev.inmo.postssystem.core.ktor.client.content
import dev.inmo.postssystem.core.content.api.*
import dev.inmo.postssystem.core.ktor.contentRootRoute
import io.ktor.client.HttpClient
import io.ktor.client.features.websocket.WebSockets
//class ContentRepoKtorClient private constructor(
// readContentRepo: ReadContentRepo,
// writeContentRepo: WriteContentRepo
//) : ContentRepo, ReadContentRepo by readContentRepo, WriteContentRepo by writeContentRepo {
// constructor(
// baseUrl: String,
// client: HttpClient = HttpClient {
// install(WebSockets)
// }
// ) : this(
// ReadContentRepoKtorClient(
// "$baseUrl/$contentRootRoute",
// client
// ),
// WriteContentRepoKtorClient(
// "$baseUrl/$contentRootRoute",
// client
// )
// )
//}

View File

@ -1,34 +0,0 @@
package dev.inmo.postssystem.core.ktor.client.content
import dev.inmo.postssystem.core.content.ContentId
import dev.inmo.postssystem.core.content.RegisteredContent
import dev.inmo.postssystem.core.content.api.ReadContentRepo
import dev.inmo.postssystem.core.ktor.*
import dev.inmo.micro_utils.ktor.client.uniget
import dev.inmo.micro_utils.ktor.common.includeQueryParams
import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat
import dev.inmo.micro_utils.pagination.*
import io.ktor.client.HttpClient
import io.ktor.client.request.get
import kotlinx.serialization.builtins.nullable
//class ReadContentRepoKtorClient(
// private val baseUrl: String,
// private val client: HttpClient = HttpClient()
//) : ReadContentRepo {
// override suspend fun getContentsIds(): Set<ContentId> = client.get<ByteArray>(
// "$baseUrl/$getContentsIdsRoute"
// ).let {
// standardKtorSerialFormat.decodeFromByteArray(contentIdsSerializer, it)
// }
//
// override suspend fun getContentById(id: ContentId): RegisteredContent? = client.uniget(
// "$baseUrl/$getContentByIdRoute/$id",
// RegisteredContent.serializer().nullable
// )
//
// override suspend fun getContentByPagination(pagination: Pagination): PaginationResult<RegisteredContent> = client.uniget(
// "$baseUrl/$getContentByPaginationRoute".includeQueryParams(pagination.asUrlQueryParts),
// registeredContentPaginationResultSerializer
// )
//}

View File

@ -1,37 +0,0 @@
package dev.inmo.postssystem.core.ktor.client.content
import dev.inmo.postssystem.core.content.*
import dev.inmo.postssystem.core.content.api.WriteContentRepo
import dev.inmo.postssystem.core.ktor.*
import dev.inmo.micro_utils.ktor.client.*
import io.ktor.client.HttpClient
import kotlinx.coroutines.flow.Flow
import kotlinx.serialization.builtins.nullable
import kotlinx.serialization.builtins.serializer
//class WriteContentRepoKtorClient(
// private val baseUrl: String,
// private val client: HttpClient = HttpClient()
//) : WriteContentRepo {
// override val contentCreatedFlow: Flow<RegisteredContent> = client.createStandardWebsocketFlow(
// "$baseUrl/$contentCreatedFlowRoute",
// deserializer = RegisteredContent.serializer()
// )
//
// override val contentDeletedFlow: Flow<RegisteredContent> = client.createStandardWebsocketFlow(
// "$baseUrl/$contentDeletedFlowRoute",
// deserializer = RegisteredContent.serializer()
// )
//
// override suspend fun registerContent(content: Content): RegisteredContent? = client.unipost(
// "$baseUrl/$registerContentRoute",
// BodyPair(Content.serializer(), content),
// RegisteredContent.serializer().nullable
// )
//
// override suspend fun deleteContent(id: ContentId): Boolean = client.unipost(
// "$baseUrl/$deleteContentRoute",
// BodyPair(ContentId.serializer(), id),
// Boolean.serializer()
// )
//}

View File

@ -11,16 +11,17 @@ class PostsRepoKtorClient private constructor(
) : PostsRepo, ReadPostsRepo by readPostsRepo, WritePostsRepo by writePostsRepo { ) : PostsRepo, ReadPostsRepo by readPostsRepo, WritePostsRepo by writePostsRepo {
constructor( constructor(
baseUrl: String, baseUrl: String,
rootRoute: String = postsRootRoute,
client: HttpClient = HttpClient { client: HttpClient = HttpClient {
install(WebSockets) install(WebSockets)
} }
) : this( ) : this(
ReadPostsRepoKtorClient( ReadPostsRepoKtorClient(
"${baseUrl}/$postsRootRoute", "${baseUrl}/$rootRoute",
client client
), ),
WritePostsRepoKtorClient( WritePostsRepoKtorClient(
"${baseUrl}/$postsRootRoute", "${baseUrl}/$rootRoute",
client client
) )
) )

View File

@ -1,6 +1,7 @@
package dev.inmo.postssystem.core.ktor package dev.inmo.postssystem.core.ktor
const val postsRootRoute = "post" const val postsRootRoute = "post"
const val publishedPostsSubRoute = "published"
const val getPostsIdsRoute = "getPostsIds" const val getPostsIdsRoute = "getPostsIds"
const val getPostByIdRoute = "getPostById" const val getPostByIdRoute = "getPostById"

View File

@ -6,19 +6,25 @@ import dev.inmo.micro_utils.ktor.server.configurators.ApplicationRoutingConfigur
import io.ktor.routing.Route import io.ktor.routing.Route
import io.ktor.routing.route import io.ktor.routing.route
private inline fun configurator(proxyTo: PostsRepo): Route.() -> Unit = {
configureReadPostsRepoRoutes(proxyTo)
configureWritePostsRepoRoutes(proxyTo)
}
fun Route.configurePostsRepoRoutes( fun Route.configurePostsRepoRoutes(
proxyTo: PostsRepo proxyTo: PostsRepo,
rootRoute: String? = postsRootRoute
) { ) {
route(postsRootRoute) { rootRoute ?.also {
configureReadPostsRepoRoutes(proxyTo) route(it, configurator(proxyTo))
configureWritePostsRepoRoutes(proxyTo) } ?: configurator(proxyTo).invoke(this)
}
} }
class PostsRepoRoutingConfigurator( class PostsRepoRoutingConfigurator(
private val proxyTo: PostsRepo private val proxyTo: PostsRepo,
private val rootRoute: String? = postsRootRoute
) : ApplicationRoutingConfigurator.Element { ) : ApplicationRoutingConfigurator.Element {
override fun Route.invoke() { override fun Route.invoke() {
configurePostsRepoRoutes(proxyTo) configurePostsRepoRoutes(proxyTo, rootRoute)
} }
} }