Skip to content

Instantly share code, notes, and snippets.

@vonox7
Created June 21, 2018 13:17
Show Gist options
  • Select an option

  • Save vonox7/2c61ca29cb059c67c101a4db092f5e03 to your computer and use it in GitHub Desktop.

Select an option

Save vonox7/2c61ca29cb059c67c101a4db092f5e03 to your computer and use it in GitHub Desktop.
import io.ktor.application.*
import io.ktor.http.HttpMethod
import io.ktor.pipeline.PipelinePhase
import io.ktor.response.respond
import io.ktor.routing.Routing
import io.ktor.routing.RoutingApplicationCall
import io.ktor.routing.route
import io.ktor.server.engine.embeddedServer
import io.ktor.server.jetty.Jetty
import io.ktor.util.AttributeKey
object CustomCallLoggingInterceptor : ApplicationFeature<Application, Unit, Unit> {
override val key: AttributeKey<Unit> = AttributeKey("Call Logging")
override fun install(pipeline: Application, configure: Unit.() -> Unit) {
val loggingPhase = PipelinePhase("Logging")
pipeline.insertPhaseBefore(ApplicationCallPipeline.Infrastructure, loggingPhase)
pipeline.intercept(loggingPhase) {
try {
proceed()
println((call as? RoutingApplicationCall)?.route)
} catch (e: Throwable) {
println((call as? RoutingApplicationCall)?.route)
}
}
}
}
object App {
@JvmStatic
fun main(args: Array<String>) {
embeddedServer(Jetty, port = 8080) {
install(CustomCallLoggingInterceptor)
install(Routing) {
route("route") {
route("one", HttpMethod.Get) {
handle {
this.call.respond("OK")
}
}
}
}
}.start()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment