以下是我在 heroku 上部署 ktor 应用程序(rest api)时遇到的异常。它在heroku本地服务器上运行良好,但在服务器上出现问题。我在这里使用 postgreSql 数据库
here you can get the full source code
https://github.com/nameisjayant/Ktor-Todo-Api
> 2021-06-06T14:05:05.948824+00:00 app[web.1]: at > com.codingwithjks.repository.DatabaseFactory.hikari(DatabaseFactory.kt:26) > 2021-06-06T14:05:05.948898+00:00 app[web.1]: at com.codingwithjks.repository.DatabaseFactory.init(DatabaseFactory.kt:16) > 2021-06-06T14:05:05.948953+00:00 app[web.1]: at com.codingwithjks.ApplicationKt.module(Application.kt:32) > 2021-06-06T14:05:05.948994+00:00 app[web.1]: at com.codingwithjks.ApplicationKt.module$default(Application.kt:28) > 2021-06-06T14:05:05.949068+00:00 app[web.1]: ... 24 more > 2021-06-06T14:05:09.000000+00:00 app[api]: Build succeeded > 2021-06-06T14:05:12.624192+00:00 heroku[web.1]: Process exited with status 1 > 2021-06-06T14:05:12.701399+00:00 heroku[web.1]: State changed from starting to crashed > 2021-06-06T14:05:28.888453+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" > host=todo-rest-api-23242.herokuapp.com > request_id=36434e2c-f89b-4ff7-bfc3-89cc3b927211 fwd="103.198.173.200" > dyno= connect= service= status=503 bytes= protocol=https > 2021-06-06T14:05:29.482958+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" > host=todo-rest-api-23242.herokuapp.com > request_id=c8c0bff6-91c6-4838-a5d7-3e83cca5f1dd fwd="103.198.173.200" > dyno= connect= service= status=503 bytes= protocol=https > 2021-06-06T14:12:03.000000+00:00 app[api]: Build started by user jayntkumar99@gmail.com > 2021-06-06T14:13:02.973949+00:00 app[api]: Deploy d0fbe4c4 by user jayntkumar99@gmail.com > 2021-06-06T14:13:02.973949+00:00 app[api]: Release v10 created by user jayntkumar99@gmail.com > 2021-06-06T14:13:03.838082+00:00 heroku[web.1]: State changed from crashed to starting > 2021-06-06T14:13:07.347769+00:00 heroku[web.1]: Starting process with command `./build/install/example/bin/example` > 2021-06-06T14:13:09.742957+00:00 app[web.1]: Setting JAVA_TOOL_OPTIONS defaults based on dyno size. Custom settings will > override them. > 2021-06-06T14:13:09.743159+00:00 app[web.1]: bash: ./build/install/example/bin/example: No such file or directory > 2021-06-06T14:13:09.802293+00:00 heroku[web.1]: Process exited with status 127 > 2021-06-06T14:13:09.886995+00:00 heroku[web.1]: State changed from starting to crashed > 2021-06-06T14:13:30.000000+00:00 app[api]: Build succeeded > 2021-06-06T14:13:40.561950+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" > host=todo-rest-api-23242.herokuapp.com > request_id=3064eaba-777b-4b6d-bd79-855d23dfe140 fwd="103.198.173.200" > dyno= connect= service= status=503 bytes= protocol=https > 2021-06-06T14:13:41.107491+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" > host=todo-rest-api-23242.herokuapp.com > request_id=fe5be84d-6e79-4eda-aae4-c4b679ec2c62 fwd="103.198.173.200" > dyno= connect= service= status=503 bytes= protocol=https > 2021-06-06T14:14:03.555698+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" > host=todo-rest-api-23242.herokuapp.com > request_id=cc4d10b8-6650-400b-ad87-5f36b144ec44 fwd="103.198.173.200" > dyno= connect= service= status=503 bytes= protocol=http > 2021-06-06T14:14:04.222065+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" > host=todo-rest-api-23242.herokuapp.com > request_id=c83df707-fe4f-47a1-a6c9-a39a23c83cc5 fwd="103.198.173.200" > dyno= connect= service= status=503 bytes= protocol=http > 2021-06-06T14:14:07.733808+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" > host=todo-rest-api-23242.herokuapp.com > request_id=78efe78b-a180-4592-844e-eeb14e317ae2 fwd="103.198.173.200" > dyno= connect= service= status=503 bytes= protocol=http > 2021-06-06T14:14:08.394252+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" > host=todo-rest-api-23242.herokuapp.com > request_id=2d2e989c-f695-4311-b3be-644381a2ea9a fwd="103.198.173.200" > dyno= connect= service= status=503 bytes= protocol=http > 2021-06-06T14:20:13.438814+00:00 heroku[web.1]: State changed from crashed to starting > 2021-06-06T14:20:17.000000+00:00 app[api]: Build started by user jayntkumar99@gmail.com > 2021-06-06T14:20:17.780691+00:00 heroku[web.1]: Starting process with command `./build/install/example/bin/example` > 2021-06-06T14:20:21.761892+00:00 app[web.1]: Setting JAVA_TOOL_OPTIONS defaults based on dyno size. Custom settings will > override them. > 2021-06-06T14:20:21.762092+00:00 app[web.1]: bash: ./build/install/example/bin/example: No such file or directory > 2021-06-06T14:20:21.840577+00:00 heroku[web.1]: Process exited with status 127 > 2021-06-06T14:20:21.910863+00:00 heroku[web.1]: State changed from starting to crashed > 2021-06-06T14:21:14.241869+00:00 app[api]: Release v11 created by user jayntkumar99@gmail.com > 2021-06-06T14:21:14.241869+00:00 app[api]: Deploy 8bf5ea75 by user jayntkumar99@gmail.com > 2021-06-06T14:21:14.497150+00:00 heroku[web.1]: State changed from crashed to starting > 2021-06-06T14:21:17.982363+00:00 heroku[web.1]: Starting process with command `java -jar build/libs/example-0.0.1-all.jar` > 2021-06-06T14:21:20.424533+00:00 app[web.1]: Setting JAVA_TOOL_OPTIONS defaults based on dyno size. Custom settings will > override them. > 2021-06-06T14:21:20.428220+00:00 app[web.1]: Picked up JAVA_TOOL_OPTIONS: -Xmx300m -Xss512k -XX:CICompilerCount=2 > -Dfile.encoding=UTF-8 > 2021-06-06T14:21:20.989769+00:00 app[web.1]: 2021-06-06 14:21:20.986 [main] TRACE Application - { > 2021-06-06T14:21:20.989792+00:00 app[web.1]: # application.conf @
/application.conf: 2 > 2021-06-06T14:21:20.989797+00:00 app[web.1]: "deployment" : { > 2021-06-06T14:21:20.989797+00:00 app[web.1] 1]: # env var PORT > 2021-06-06T14:21:20.989798+00:00 app[web.1]: "port" : "44723" > 2021-06-06T14:21:20.989798+00:00 app [web.1]: }, > 2021-06-06T14:21:20.989799+00:00 app[web.1]: # 隐藏内容 > 2021-06-06T14:21:20.989799+00:00 app[web.1] 1]:“安全”:“***”> 2021-06-06T14:21:20.989799+00:00 应用程序[web.1]: } > 2021-06-06T14:21:20.989800+00:00 应用程序[ web.1]: > 2021-06-06T14:21:21.096772+00:00 app[web.1]: 2021-06-06 14:21:21.096 [main] INFO Application - Autoreload is disabled because > development mode已关闭。> 2021-06-06T14:21:21.599262+00:00 app[web.1]:线程“main”java 中的异常。
数据库工厂类
package com.codingwithjks.repository
import com.codingwithjks.data.table.TodoTable
import com.codingwithjks.data.table.UserTable
import com.zaxxer.hikari.HikariConfig
import com.zaxxer.hikari.HikariDataSource
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.SchemaUtils
import org.jetbrains.exposed.sql.transactions.transaction
object DatabaseFactory {
fun init(){
Database.connect(hikari())
transaction {
SchemaUtils.create(UserTable)
SchemaUtils.create(TodoTable)
}
}
private fun hikari():HikariDataSource {
val config = HikariConfig()
config.driverClassName = System.getenv("JDBC_DRIVER_2")
config.jdbcUrl = System.getenv("JDBC_DATABASE_URL_2")
config.maximumPoolSize = 3
config.isAutoCommit = true
config.transactionIsolation = "TRANSACTION_REPEATABLE_READ"
config.validate()
return HikariDataSource(config)
}
suspend fun <T> dbQuery(block : ()->T):T =
withContext(Dispatchers.IO){
transaction {
block()
}
}
}
应用程序.kt 文件
package com.codingwithjks
import com.codingwithjks.auth.JwtService
import com.codingwithjks.auth.MySession
import com.codingwithjks.auth.hash
import com.codingwithjks.repository.DatabaseFactory
import com.codingwithjks.repository.TodoRepository
import com.codingwithjks.repository.UserRepository
import com.codingwithjks.routes.todos
import com.codingwithjks.routes.users
import io.ktor.application.*
import io.ktor.response.*
import io.ktor.request.*
import io.ktor.routing.*
import io.ktor.http.*
import io.ktor.locations.*
import io.ktor.sessions.*
import io.ktor.auth.*
import io.ktor.auth.jwt.*
import io.ktor.gson.*
import io.ktor.features.*
fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)
@OptIn(KtorExperimentalLocationsAPI::class)
@Suppress("unused") // Referenced in application.conf
@kotlin.jvm.JvmOverloads
fun Application.module(testing: Boolean = false) {
install(Locations) {
}
DatabaseFactory.init()
val userDb = UserRepository()
val todoDb = TodoRepository()
val jwt = JwtService()
val hashFunction = {s:String -> hash(s)}
install(Sessions) {
cookie<MySession>("MY_SESSION") {
cookie.extensions["SameSite"] = "lax"
}
}
install(Authentication) {
jwt("jwt") {
verifier(jwt.verifier)
realm = "Todo Server"
validate {
val payload = it.payload
val claim = payload.getClaim("id")
val claimString = claim.asInt()
val user = userDb.findUser(claimString)
user
}
}
}
install(ContentNegotiation) {
gson {
setPrettyPrinting()
disableHtmlEscaping()
}
}
routing {
get("/"){
call.respondText("Hello world..")
}
users(userDb,todoDb,jwt,hashFunction)
todos(todoDb,userDb)
}
}
const val API_VERSION = "/v1"
请帮我 :)