53 lines
1.6 KiB
Kotlin
53 lines
1.6 KiB
Kotlin
package eventDemo.configuration
|
|
|
|
import io.ktor.http.HttpHeaders
|
|
import io.ktor.http.HttpMethod
|
|
import io.ktor.http.HttpStatusCode
|
|
import io.ktor.server.application.Application
|
|
import io.ktor.server.application.install
|
|
import io.ktor.server.plugins.autohead.AutoHeadResponse
|
|
import io.ktor.server.plugins.cors.routing.CORS
|
|
import io.ktor.server.plugins.statuspages.StatusPages
|
|
import io.ktor.server.resources.Resources
|
|
import io.ktor.server.response.respondText
|
|
|
|
fun Application.configureHttpRouting() {
|
|
install(CORS) {
|
|
allowMethod(HttpMethod.Options)
|
|
allowMethod(HttpMethod.Put)
|
|
allowMethod(HttpMethod.Post)
|
|
allowMethod(HttpMethod.Delete)
|
|
allowMethod(HttpMethod.Patch)
|
|
allowHeader(HttpHeaders.Authorization)
|
|
allowHeader("MyCustomHeader")
|
|
anyHost() // @TODO: Don't do this in production if possible. Try to limit it.
|
|
}
|
|
install(AutoHeadResponse)
|
|
install(Resources)
|
|
install(StatusPages) {
|
|
exception<BadRequestException> { call, cause ->
|
|
call.respondText(text = "400: $cause", status = HttpStatusCode.BadRequest)
|
|
}
|
|
exception<Throwable> { call, cause ->
|
|
call.respondText(text = "500: $cause", status = HttpStatusCode.InternalServerError)
|
|
}
|
|
}
|
|
}
|
|
|
|
class BadRequestException(
|
|
val httpError: HttpErrorBadRequest,
|
|
) : Exception()
|
|
|
|
class HttpErrorBadRequest(
|
|
statusCode: HttpStatusCode,
|
|
val title: String = statusCode.description,
|
|
val invalidParams: List<InvalidParam>,
|
|
) {
|
|
val statusCode: Int = statusCode.value
|
|
|
|
data class InvalidParam(
|
|
val name: String,
|
|
val reason: String,
|
|
)
|
|
}
|