Overview
In this article, you will learn Ktor application setup, project structure intro, and how to run our application.
Introduction
Ktor is developed by Jetbrains, the same open-source company that brought Kotlin for us. KTOR is a framework used to create connected applications, web applications, HTTP services as well as mobile and browser applications. The main goal of KTOR is to provide us with a multi-platform end-to-end framework for interlinked applications.
Ktor has great support for coroutines. That’s why we can create server-side applications with asynchronous programming.
Project Setup
We can create and configure a KTOR project using two ways. The first is with the dedicated plugin IntelliJ IDE ultimate and the second is using a web-based project generator. Let’s understand this thing via the below graph
Create a new Ktor application
In this step, you will learn project creation using IntelliJ IDEA Ultimate. If you are using IntelliJ IDEA Community Edition use this Ktor Project Generator instead.
Prerequisites:
– IntelliJ IDEA Ultimate installed
– Make sure KTOR plugin is installed and enabled
First of all open IntelliJ IDEA and follow the below steps:
- From the Welcome screen, click New Project.
- Now from the New Project wizard, choose Ktor from the left panel. Then you can see the wizard below.
Name : Add your project nameLocation : Add directory path to save the projectBuild System : Select your desired build system.Website : Define your domain which is used to generate a package name.Artifact : This field will show a generated artifact name.Ktor version : Select the required Ktor version.Engine : Select an engine used to run a server.Configuration in : Selects whether to add server parameters in code or in a HOCON file.Add sample code : Leave this option enabled to add sample code for plugins added on the next page. - After project setup click Next to redirect to the next step. Where you can see the wizard below.
Here you need to add the required plugins for the ktor application. For example authentication, serialisation and content-encoding, compression, cookie support, and many more. But for now, You just need the Routing plugin to handle incoming requests. You can also find that plugin by typing routing on the search box. After that click Add to add that plugin. - Now our ktor application is ready to create. Click on Create button then the project will generate and all dependencies will be installed.
Examine the Project
Now look at the structure of the project. Let’s check the Project View as below screen
- main/kotlin
This folder contains all the generated kotlin files. You can also add kotlin files as per our requirements.
- main/resources
This folder contains all the configuration files.
- build.gradle.kts
This folder contains all the dependencies required for the Ktor server and plugins.
Here let’s discuss all the above in short so that you can understand the flow of the Ktor application.
logback.xml defines the basic logging structure of our server.
application.conf is a configuration file which is in HOCON format that You have to define at the time of project creation. It also defines the entry point of the application.
ktor { deployment { port = 8080 port = ${?PORT} } application { modules = [ com.example.ApplicationKt.module ] } }
Here com.example.ApplicationKt.module shows that the application will start execution from the Application.kt file with their Application.module() function. Here is the code snippet as below
fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args) fun Application.module() { configureRouting() }
configureRouting() is a function, which is defined in plugins/Routing.kt. For the sample it shows a code snippet as below
fun Application.configureRouting() { routing { get("/") { call.respondText("Hello World!") } } }
Here get() defines the HTTP request type. And “/” defines the endpoint of the HTTP URL. You can also customize this as per your requirements. For example, post, delete, put, and many more. call.respondText(“Hello World!“) will return text as a response. You can also customize responses as per your requirements like JSON and object.
Run the Project
To run the application first, You need to open Application.Kt. Here, you will see a green play icon next to the main() function. On right-click on that icon, you can see a view like the below screen
Now click on the Run ApplicationKt. Now you need to wait until IntelliJ IDEA runs our application. Into the Run tool window, you will see below screen
The last line in the above screen shows that our server is ready to accept requests at http://0.0.0.0:8080 this address. So now you can click on the above address and this will open this address on the default browser. Or you can copy and paste this address into your browser. After running this address on the browser you can see below final output as below screen
So, now you are ready to boom with Ktor.
your BusinessGet Expert Assistance