You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.9 KiB
56 lines
1.9 KiB
package net.aiterp.git.ykonsole2.application.plugins
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper
|
|
import com.fasterxml.jackson.databind.SerializationFeature
|
|
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
|
|
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
|
|
import io.ktor.http.*
|
|
import io.ktor.serialization.jackson.*
|
|
import io.ktor.server.application.*
|
|
import io.ktor.server.plugins.*
|
|
import io.ktor.server.plugins.contentnegotiation.*
|
|
import io.ktor.server.plugins.statuspages.*
|
|
import io.ktor.server.websocket.*
|
|
import net.aiterp.git.ykonsole2.application.logging.log
|
|
import net.aiterp.git.ykonsole2.infrastructure.ktor.YKonsoleResponseException
|
|
import net.aiterp.git.ykonsole2.infrastructure.ktor.ykStatusResponse
|
|
import java.time.Duration
|
|
|
|
internal object YKonsoleErrorHandler
|
|
|
|
val ykObjectMapper: ObjectMapper = jacksonObjectMapper().apply { setupObjectMapper() }
|
|
|
|
private fun ObjectMapper.setupObjectMapper() {
|
|
disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
|
|
registerModule(JavaTimeModule())
|
|
}
|
|
|
|
fun Application.installPlugins() {
|
|
install(ContentNegotiation) {
|
|
jackson { setupObjectMapper() }
|
|
}
|
|
|
|
install(StatusPages) {
|
|
exception<YKonsoleResponseException> { _, ex ->
|
|
if (ex.worthReporting) {
|
|
YKonsoleErrorHandler.log.warn("Caught exception: ${ex.message}")
|
|
}
|
|
ex.respond()
|
|
}
|
|
|
|
exception<BadRequestException> { call, ex ->
|
|
YKonsoleErrorHandler.log.warn("JsonMappingException from request: ${ex.message}", ex)
|
|
call.ykStatusResponse(HttpStatusCode.BadRequest)
|
|
}
|
|
|
|
exception<Throwable> { call, ex ->
|
|
YKonsoleErrorHandler.log.error("Uncaught exception: ${ex.message}", ex)
|
|
call.ykStatusResponse(HttpStatusCode.InternalServerError)
|
|
}
|
|
}
|
|
|
|
install(WebSockets) {
|
|
contentConverter = JacksonWebsocketContentConverter(ykObjectMapper)
|
|
pingPeriod = Duration.ofSeconds(15)
|
|
}
|
|
}
|