Hello All, This is my first time deploying a ktor ...
# ktor
d
Hello All, This is my first time deploying a ktor ap ion heroku, and i'm suck with an error. I have been tryting to go though documentation and other stackoverflow pages for help but could not figure what i wrong. Adding my build.gradle file and the error that i'm facing
Copy code
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import org.jetbrains.kotlin.com.intellij.openapi.vfs.StandardFileSystems.jar

val ktor_version: String by project
val kotlin_version: String by project
val logback_version: String by project
val exposed_version: String by project
val postgres_version: String by project
val hikari_version: String by project

plugins {
    kotlin("jvm") version "1.9.22"
    id("io.ktor.plugin") version "2.3.8"
    kotlin("plugin.serialization") version "1.9.22"
    id("com.github.johnrengelman.shadow") version "7.1.2"
}

group = "com.schoolcircle"
version = "0.0.1"

application {
    mainClass.set("com.schoolcircle.ApplicationKt")
}
tasks {
    withType<Jar> {
        manifest {
            attributes["Main-Class"] = "com.schoolcircle.ApplicationKt"
        }
    }
}
//tasks.shadowJar {
//    manifest {
//        attributes["Main-Class"] = application.mainClass.get()
//    }
//}


repositories {
    mavenCentral()
    maven {
        url = uri("<https://dl.bintray.com/kotlin/exposed>")
    }
    maven { url = uri("<https://kotlin.bintray.com/ktor>") }
    maven {
        url = uri("<https://repo.spring.io/release>")
    }
    google()
}

tasks {
    create("stage").dependsOn("installDist")
}
tasks.test {
    enabled = false
}
gradle.taskGraph.whenReady {
    if (this.hasTask("stage")) {
        tasks.test {
            enabled = false
        }
    }
}

dependencies {
    implementation("io.ktor:ktor-server-auth-jvm")
    implementation("io.ktor:ktor-server-core-jvm")
    implementation("io.ktor:ktor-server-auth-jwt-jvm")
    implementation("io.ktor:ktor-server-sessions-jvm")
    implementation("io.ktor:ktor-server-netty-jvm")
    implementation("io.ktor:ktor-server-locations:$kotlin_version")
    implementation("ch.qos.logback:logback-classic:$logback_version")
    implementation("io.ktor:ktor-server-content-negotiation:$ktor_version")
    implementation("io.ktor:ktor-serialization-jackson:$ktor_version")
    testImplementation("io.ktor:ktor-server-tests-jvm")
    testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version")
    testImplementation ("io.ktor:ktor-server-tests:$ktor_version")

    implementation("org.jetbrains.exposed:exposed-core:$exposed_version")
    implementation("org.jetbrains.exposed:exposed-dao:$exposed_version")
    implementation("org.jetbrains.exposed:exposed-jdbc:$exposed_version")
    implementation("org.postgresql:postgresql:$postgres_version")
    implementation("com.zaxxer:HikariCP:$hikari_version")

}
this is my application class
Copy code
package com.schoolcircle

import com.schoolcircle.authentication.JwtService
import com.schoolcircle.authentication.hash
import com.schoolcircle.data.services.adminUtils.AdminServicesImpl
import com.schoolcircle.data.services.student.StudentServiceImpl
import com.schoolcircle.data.services.tenant.TenantServiceImpl
import com.schoolcircle.data.services.user.UserServiceImpl
import com.schoolcircle.data.services.userManagement.UserManagementServiceImpl
import com.schoolcircle.plugins.*
import com.schoolcircle.repository.DatabaseFactory
import io.ktor.serialization.jackson.*
import io.ktor.server.application.*
import io.ktor.server.engine.*
import io.ktor.server.locations.*
import io.ktor.server.netty.*
import io.ktor.server.plugins.contentnegotiation.*

fun main(args: Array<String>): Unit = EngineMain.main(args)

@OptIn(KtorExperimentalLocationsAPI::class)
fun Application.module() {
    DatabaseFactory.init()
    install(Locations)
    install(ContentNegotiation) {
        jackson()
    }
    val userDb = UserServiceImpl()
    val tenantDb = TenantServiceImpl()
    val adminDb = AdminServicesImpl()
    val userManagementDb = UserManagementServiceImpl()
    val studentDb = StudentServiceImpl()
    val jwtService = JwtService()
    val hashFunction = {s: String -> hash(s) }
    configureSecurity()
    configureRouting(
        tenantDb = tenantDb,
        userDb = userDb,
        jwtService = jwtService,
        adminDb = adminDb,
        studentDb = studentDb,
        userManagementDb = userManagementDb,
        hashFunction = hashFunction
    )
}
Procfile
Copy code
web: java -jar ./build/libs/school-backend-0.0.1.jar
🧵 3
c
Please post long code snippets in the thread.
Also don’t split you post. It’s hard to answer with context.
Where do you have the repositieries from? Ktor is available from maven central.
d
there were some errors and while solving them i added them, I can remove them. if they are not needed
I changed the Procfile
Copy code
web: java -jar ./build/libs/school-backend-all.jar
the build is successful on Heroku but it is still giving an error Error: Unable to access jarfile ./build/libs/school-backend-all.jar
Heroku has the file but not sure why it is not able to access it
c
Well, that you should ask in a he heroku forum. It’s not a problem with ktor nor kotlin.
d
Also one thing to mention, when i run the app locally using "heroku local:start" i get a different error
Copy code
2024-03-17 15:31:37.051 [main] INFO  Application - Application started in 11.178 seconds.
3:31:41 PM web.1 |  Exception in thread "main" 
3:31:41 PM web.1 |  java.net.BindException: Address already in use
3:31:41 PM web.1 |      at java.base/sun.nio.ch.Net.bind0(Native Method)
3:31:41 PM web.1 |      at java.base/sun.nio.ch.Net.bind(Net.java:555)
3:31:41 PM web.1 |      at java.base/sun.nio.ch.ServerSocketChannelImpl.netBind(ServerSocketChannelImpl.java:337)
3:31:41 PM web.1 |      at java.base/sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:294)
3:31:41 PM web.1 |      at io.netty.channel.socket.nio.NioServerSocketChannel.doBind(NioServerSocketChannel.java:141)
3:31:41 PM web.1 |      at io.netty.channel.AbstractChannel$AbstractUnsafe.bind(AbstractChannel.java:562)
3:31:41 PM web.1 |      at io.netty.channel.DefaultChannelPipeline$HeadContext.bind(DefaultChannelPipeline.java:1334)
3:31:41 PM web.1 |      at io.netty.channel.AbstractChannelHandlerContext.invokeBind(AbstractChannelHandlerContext.java:600)
3:31:41 PM web.1 |      at io.netty.channel.AbstractChannelHandlerContext.bind(AbstractChannelHandlerContext.java:579)
3:31:41 PM web.1 |      at io.netty.channel.DefaultChannelPipeline.bind(DefaultChannelPipeline.java:973)
3:31:41 PM web.1 |      at io.netty.channel.AbstractChannel.bind(AbstractChannel.java:260)
3:31:41 PM web.1 |      at io.netty.bootstrap.AbstractBootstrap$2.run(AbstractBootstrap.java:380)
3:31:41 PM web.1 |      at io.netty.util.concurrent.AbstractEventExecutor.runTask(AbstractEventExecutor.java:173)
3:31:41 PM web.1 |      at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:166)
3:31:41 PM web.1 |      at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:470)
3:31:41 PM web.1 |      at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:569)
3:31:41 PM web.1 |      at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997)
3:31:41 PM web.1 |      at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
3:31:41 PM web.1 |      at io.ktor.server.netty.EventLoopGroupProxy$Companion.create$lambda$1$lambda$0(NettyApplicationEngine.kt:296)
3:31:41 PM web.1 |      at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
3:31:41 PM web.1 |      at java.base/java.lang.Thread.run(Thread.java:842)
I will try that thank you for your help
v
You can have a look at https://github.com/studoverse/campus-qr/ (and especially the
server
folder). This is a project that runs on heroku/scalingo.
👀 1