A sample Ktor project demonstrating how to create custom plugins in Ktor.
To run a sample, execute the following command in a repository's root directory:
./gradlew :server-custom-plugin:runSimplePlugin 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:8080To 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/indexCustomHeaderPlugin 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 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 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