add installation of things in server part

This commit is contained in:
InsanusMokrassar 2019-10-17 21:56:47 +06:00
parent 3e7a2c1f0d
commit 125b66e237
4 changed files with 36 additions and 6 deletions

View File

@ -34,6 +34,7 @@ dependencies {
api "io.ktor:ktor-server:$ktor_version" api "io.ktor:ktor-server:$ktor_version"
api "io.ktor:ktor-server-core:$ktor_version" api "io.ktor:ktor-server-core:$ktor_version"
api "io.ktor:ktor-websockets:$ktor_version" api "io.ktor:ktor-websockets:$ktor_version"
api "io.ktor:ktor-serialization:$ktor_version"
} }
compileKotlin { compileKotlin {

View File

@ -0,0 +1,24 @@
package com.insanusmokrassar.postssystem.core.server
import io.ktor.application.*
import io.ktor.features.ContentNegotiation
import io.ktor.http.ContentType
import io.ktor.serialization.serialization
import io.ktor.websocket.WebSockets
import kotlinx.serialization.json.Json
fun Application.installCorePosts() {
featureOrNull(ContentNegotiation) ?: install(ContentNegotiation) {
serialization(
contentType = ContentType.Application.Json,
json = Json.plain
)
}
}
fun Application.installCorePostsWrite() {
installCorePosts()
featureOrNull(WebSockets) ?: install(WebSockets) {
maxFrameSize = Long.MAX_VALUE
}
}

View File

@ -7,7 +7,8 @@ import com.insanusmokrassar.postssystem.core.content.ContentId
import com.insanusmokrassar.postssystem.core.post.* import com.insanusmokrassar.postssystem.core.post.*
import com.insanusmokrassar.postssystem.core.utils.pagination.PaginationRequest import com.insanusmokrassar.postssystem.core.utils.pagination.PaginationRequest
import com.insanusmokrassar.postssystem.core.utils.pagination.PaginationResult import com.insanusmokrassar.postssystem.core.utils.pagination.PaginationResult
import io.ktor.application.call import io.ktor.application.*
import io.ktor.features.ContentNegotiation
import io.ktor.http.ContentType import io.ktor.http.ContentType
import io.ktor.http.HttpStatusCode import io.ktor.http.HttpStatusCode
import io.ktor.request.receiveOrNull import io.ktor.request.receiveOrNull
@ -15,17 +16,17 @@ import io.ktor.response.respond
import io.ktor.response.respondText import io.ktor.response.respondText
import io.ktor.routing.Route import io.ktor.routing.Route
import io.ktor.routing.post import io.ktor.routing.post
import io.ktor.serialization.serialization
import kotlinx.serialization.InternalSerializationApi
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import org.joda.time.DateTime import org.joda.time.DateTime
@InternalSerializationApi
fun Route.includePostsCoreReadModules(readPostsAPI: ReadPostsAPI) { fun Route.includePostsCoreReadModules(readPostsAPI: ReadPostsAPI) {
post(getPostByIdAddress) { post(getPostByIdAddress) {
call.receiveOrNull<PostId>() ?.also { id -> call.receiveOrNull<PostId>() ?.also { id ->
val post = readPostsAPI.getPostById(id) val post = readPostsAPI.getPostById(id)
post ?.let { call.answer(SimplePost.serializer(), post ?.asSimplePost)
val simplePost = post.asSimplePost
call.answer(SimplePost.serializer(), simplePost)
} ?: call.respond(HttpStatusCode.NotFound, "Not found")
} ?: call.answerBadRequest("Id of post") } ?: call.answerBadRequest("Id of post")
} }
post(getPostsByContentIdAddress) { post(getPostsByContentIdAddress) {

View File

@ -4,11 +4,15 @@ import com.insanusmokrassar.postssystem.core.api.WritePostsAPI
import com.insanusmokrassar.postssystem.core.clientserver.common.* import com.insanusmokrassar.postssystem.core.clientserver.common.*
import com.insanusmokrassar.postssystem.core.clientserver.common.models.UpdatePostRequest import com.insanusmokrassar.postssystem.core.clientserver.common.models.UpdatePostRequest
import com.insanusmokrassar.postssystem.core.post.* import com.insanusmokrassar.postssystem.core.post.*
import io.ktor.application.call import io.ktor.application.*
import io.ktor.features.ContentNegotiation
import io.ktor.http.ContentType
import io.ktor.http.cio.websocket.Frame import io.ktor.http.cio.websocket.Frame
import io.ktor.request.receiveOrNull import io.ktor.request.receiveOrNull
import io.ktor.routing.Route import io.ktor.routing.Route
import io.ktor.routing.post import io.ktor.routing.post
import io.ktor.serialization.serialization
import io.ktor.websocket.WebSockets
import io.ktor.websocket.webSocket import io.ktor.websocket.webSocket
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.collect