Creating Your First Application
Table of contents:
Ktor is split up into several groups of artifacts,allowing you to include only the functionality that you will need. And thus reducing the size of a fat-jar containing all the code, and the startup time.
In this case, you only need to include the artifact ktor-server-netty
.For a list of all the artifacts available, please check the page.
Release versions of these dependencies are available at jcenter and maven central.For pre-releases we host them on Bintray kotlin/ktor.
Ktor allows applications to run within an Application Server compatible with Servlets, such as Tomcat,or as an embedded application, using Jetty, Netty or CIO.
In this tutorial, we are going to create a self-hosted application using Netty.
You can start by calling the embeddedServer
function, passing in the engine factory as the first argument,the port as the second argument and the actual application code as the fourth argument (third argumentis the host which is 0.0.0.0
by default).
The code below defines a single route that responds to the verb on the URL /
withthe text Hello, world!
Main.kt
If your server is just listening for HTTP requests and do not want to do anything else after that in the setup,you will normally call the server.start with .
Given that the entry point of your application is the standard Kotlin main
function, you can simply run it, effectively starting the server and listening on the specified port.
Checking the localhost:8080
page in your browser, you should see the Hello, world!
text.