Created
August 12, 2019 09:08
-
-
Save lankydan/bb5fff5e4119164a8f84b41901b4a65c to your computer and use it in GitHub Desktop.
Corda + Ktor - endpoints
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
fun Routing.messages(proxy: CordaRPCOps) { | |
route("/messages") { | |
get("/") { | |
call.respond( | |
HttpStatusCode.OK, | |
proxy.vaultQueryBy<MessageState>().states.map { it.state.data }) | |
} | |
post("/") { | |
val received = call.receive<Message>() | |
try { | |
val message = proxy.startFlow( | |
::SendMessageFlow, | |
state(proxy, received, UUID.randomUUID()) | |
).returnValue.getOrThrow().coreTransaction.outputStates.first() as MessageState | |
call.respond(HttpStatusCode.Created, message) | |
} catch (e: Exception) { | |
call.respond(HttpStatusCode.InternalServerError, e.message ?: "Something went wrong") | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment