Created
June 21, 2018 13:17
-
-
Save vonox7/2c61ca29cb059c67c101a4db092f5e03 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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