Skip to content

Latest commit

 

History

History
50 lines (34 loc) · 2.17 KB

File metadata and controls

50 lines (34 loc) · 2.17 KB

Custom plugin

A sample Ktor project demonstrating how to create custom plugins in Ktor.

Running

To run a sample, execute the following command in a repository's root directory:

./gradlew :server-custom-plugin:run

SimplePlugin

SimplePlugin displays a greeting in the console output:

2021-10-14 14:54:08.269 [main] INFO  Application - Autoreload is disabled because the development mode is off.
SimplePlugin is installed!
2021-10-14 14:54:08.900 [main] INFO  Application - Responding at http://0.0.0.0:8080

RequestLoggingPlugin

To see RequestLoggingPlugin in action, visit several pages, for example, http://localhost:8080/ and http://localhost:8080/index. A console will show the requested URLs:

Request URL: http://localhost:8080/
Request URL: http://localhost:8080/index

CustomHeaderPlugin

CustomHeaderPlugin adds a custom header to each response. You can open a browser's network tools for http://localhost:8080/ and make sure that a header is added:

HTTP/1.1 200 OK
X-Custom-Header: Hello, world!

DataTransformationPlugin

DataTransformationPlugin transforms a received body to an integer value and adds 1 to it. Then, it adds 1 one more time when sending a response. To test a plugin, open post.http and make a request.

DataTransformationBenchmarkPlugin

DataTransformationBenchmarkPlugin uses attributes to calculate the time between receiving a request and reading a body. To see it in action, open post.http and make a request. Then, open a console to see a delay value:

Request URL: http://localhost:8080/transform-data
Read body delay (ms): 52