Deploying your Ktor app to Railway

Based in India.
Search for a command to run...

Based in India.
No comments yet. Be the first to comment.
Understanding Coroutines and Flow Kotlin Coroutines and Flows are a powerful tool for managing asynchronous programming in your Android apps. This article is a beginner's guide to understanding Kotlin Flow. First, let's understand what coroutines are...

The problem When working with JVM (Java Virtual Machine), the Garbage Collector will take care of allocating and deallocating memory for us - Most of the time. In some cases, it fails to release the memory for some objects which are no longer in use,...

What are quick actions? Quick actions under the hood are App Shortcuts(in Android) and Home Screen Actions(in iOS) which allow users to perform certain app actions without having to open the app. Where have I seen this? You might have already seen an...

What are app shortcuts? App shortcuts (introduced in Android Nougat 7.1) allow users to perform certain app actions without having to open the app. Developers can allow users to pin certain app actions to home screen. Let's see how. Where have I seen...

I recently had to deploy a Ktor project to Railway for my hobby project. I'll share the how-to through this article.
Ktor is a framework that allows us to build web applications in Kotlin, built and backed by Jetbrains. It's open-source, lightweight, and flexible, making it an excellent choice for building web applications in the JVM ecosystem.
We'll be deploying a project that is generated for us by Ktor plugin, it is a simple Hello World! app. You can check it out live here or check GitHub repo here.
I've generated the project using the Ktor plugin but you can also generate it via Ktor Project Generator.

Routing plugin for handling incoming requests.
INFO ktor.application - Responding at http://127.0.0.1:8080
When you open the link it will open the browser and say Hello World!

I've added the Application.kt and Routing.kt file for reference below -
Application.kt -
package abhishekkumar.me
import io.ktor.server.application.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import abhishekkumar.me.plugins.*
fun main() {
embeddedServer(Netty, port = 8080, host = "0.0.0.0", module = Application::module)
.start(wait = true)
}
fun Application.module() {
configureRouting()
}
Note: By default the Port is 8080 and host is 0.0.0.0, you can change it in Application.kt file.
Routing.kt -
package abhishekkumar.me.plugins
import io.ktor.server.routing.*
import io.ktor.server.response.*
import io.ktor.server.application.*
fun Application.configureRouting() {
routing {
get("/") {
call.respondText("Hello World!")
}
}
}

All done from here, now let's head over to Railway dashboard.
Select New Project and Choose Deploy from Github repo
Select your GitHub repo, I have selected my repository imabhishekkumar/ktor-demo
Click on Add variables

We will add PORT variable that we have configured in the project. By default it's 8080.

In the Settings tab, we'll add the build command and deploy command for Ktor as per the documentation here
Build - gradle clean buildFatJar
Deploy- gradle runFatJar


Once that is done, we want to add a domain to make our service available to public internet. Railyway provides free domain for us. In the Settings tab, click on Generate Domain or use your custom domain.


Once that is done, our project is hosted and accessible via the domain.
Here's my dummy project - ktor-demo-production.up.railway.app
I hope this article was helpful. If you have any questions, feel free to reach out!
Follow for more articles about Ktor (as I explore) and Mobile development.
Connect with me -