Created
July 10, 2018 11:35
-
-
Save dave08/b6df4b5d18ca9dff5530fa2d35a99f82 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ContextDsl | |
fun Route.coGet(path: String, block: suspend ApplicationCall.() -> ServerResponse): Route = get("/") { | |
val result = call.block() | |
when (result) { | |
is ServerResponse.Empty -> | |
call.respondText { "" } | |
is ServerResponse.Json -> | |
call.respondText(result.jsonString, ContentType.Application.Json) | |
is ServerResponse.Error -> | |
call.respondText(ContentType.Application.Json, HttpStatusCode.fromValue(result.statusCode)) { | |
logger.error{ "${result.message}: caused by ${result.exception}" } | |
result.message | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Another possible alternative (I guess):