Skip to content

Instantly share code, notes, and snippets.

@daviddenton
Forked from scap1784/example.kt
Last active November 19, 2017 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daviddenton/7bfeea6347b1d0e925e9ef04ac94c2a0 to your computer and use it in GitHub Desktop.
Save daviddenton/7bfeea6347b1d0e925e9ef04ac94c2a0 to your computer and use it in GitHub Desktop.
meta fails because to generates a tuple.
import org.http4k.contract.ApiInfo
import org.http4k.contract.ApiKey
import org.http4k.contract.OpenApi
import org.http4k.contract.RouteMeta
import org.http4k.contract.bindContract
import org.http4k.contract.contract
import org.http4k.contract.meta
import org.http4k.core.Body
import org.http4k.core.ContentType.Companion.TEXT_PLAIN
import org.http4k.core.Filter
import org.http4k.core.HttpHandler
import org.http4k.core.Method.GET
import org.http4k.core.Request
import org.http4k.core.Response
import org.http4k.core.Status
import org.http4k.core.Status.Companion.OK
import org.http4k.core.then
import org.http4k.core.with
import org.http4k.filter.ResponseFilters
import org.http4k.format.Jackson
import org.http4k.lens.Query
import org.http4k.lens.int
import org.http4k.lens.string
import org.http4k.routing.RoutingHttpHandler
import java.time.Clock
import java.time.Duration
fun main(args: Array<String>) {
fun provideServer(): RoutingHttpHandler {
fun ping(): HttpHandler = {
Response(Status.OK).with(
Body.string(TEXT_PLAIN).toLens() of "pong"
)
}
val filter: Filter = ResponseFilters.ReportRouteLatency(Clock.systemUTC(), { name: String, latency: Duration ->
println(name + " took " + latency)
})
val security = ApiKey(Query.int().required("apiKey"), {
println("foo" + (it == 42))
it == 42
})
val contract = contract(OpenApi(ApiInfo("the greatest api", "v1.0"), Jackson), "/docs/swagger.json", security,
"/ping" meta {
summary = "ping"
description = "Are we still there?"
returning("Pong" to OK)
} bindContract GET to ping()
)
return filter.then(contract)
}
val app = provideServer()
val response = app(Request(GET, "/ping").query("apiKey", "42"))
println(response)
println(app(Request(GET, "/docs/swagger.json").query("apiKey", "42")))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment