Skip to content
This repository has been archived by the owner on Jan 29, 2019. It is now read-only.

Commit

Permalink
#3 add basic bitcoin block handler
Browse files Browse the repository at this point in the history
  • Loading branch information
hleb-albau committed Sep 21, 2017
1 parent 65cd29a commit ed959b9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions core/src/main/kotlin/fund/cyber/node/common/Util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ package fund.cyber.node.common
import java.util.*

fun Deque<String>.intValue(): Int? = first?.toIntOrNull()
fun Deque<String>.longValue(): Long? = first?.toLongOrNull()
fun Deque<String>.stringValue(): String? = first
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fund.cyber.search

import fund.cyber.search.handler.BitcoinBlockHandler
import fund.cyber.search.handler.SearchHandler
import io.undertow.Handlers
import io.undertow.Undertow
Expand All @@ -12,6 +13,7 @@ object SearchApiApplication {

val httpHandler = Handlers.path()
.addPrefixPath("/search", SearchHandler())
.addPrefixPath("/bitcoin/block/{blockNumber}", BitcoinBlockHandler())

Undertow.builder()
.addHttpListener(8085, "0.0.0.0")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package fund.cyber.search.handler

import com.fasterxml.jackson.databind.ObjectMapper
import fund.cyber.node.common.longValue
import fund.cyber.search.context.AppContext
import io.undertow.server.HttpHandler
import io.undertow.server.HttpServerExchange
import io.undertow.util.Headers

class BitcoinBlockHandler(
private val jsonSerializer: ObjectMapper = AppContext.jsonSerializer
) : HttpHandler {

override fun handleRequest(exchange: HttpServerExchange) {

val blockNumber = exchange.pathParameters["blockNumber"]?.longValue()

if (blockNumber == null || blockNumber < 0) {
exchange.statusCode = 400
return
}

val rawResponse = jsonSerializer.writeValueAsString("""{"block":"$blockNumber"}""")

exchange.responseHeaders.put(Headers.CONTENT_TYPE, "application/json")
exchange.responseSender.send(rawResponse)
}
}

0 comments on commit ed959b9

Please sign in to comment.