如何在 Google App Engine 上部署 Ktor 应用程序?

How to deploy Ktor app on Google App Engine?

Ktor.io的官方教程不行,我试了下。 这是我的第一次部署。感谢您的帮助。

我的 gradle 文件 (kts):

import com.google.cloud.tools.gradle.appengine.appyaml.AppEngineAppYamlExtension

buildscript {
    dependencies {
        classpath("com.google.cloud.tools:appengine-gradle-plugin:2.2.0")
    }
}

apply {
    plugin("com.google.cloud.tools.appengine")
}

plugins {
    // Support for Kotlin
    id("org.jetbrains.kotlin.jvm") version "1.3.61"
    // Support for building a CLI application
    application
    // Documentation
    id("org.jetbrains.dokka") version "0.10.1"

    war
//    id("com.improve_future.harmonica") version "1.1.24"
}

application {
    mainClassName = "com.easythings.parkkometr.AppKt"
    group = "com.easythings"
    version = "0.0.1"
}

sourceSets {
    main {
        java.srcDir("app/main/src")
        resources.srcDir("app/main/resources")
    }
    test {
        java.srcDir("app/test/src/")
        resources.srcDir("app/test/resources/")
    }
}

repositories {
    jcenter()
    maven { setUrl("https://kotlin.bintray.com/ktor") }
}

dependencies {
    val ktorVersion: String by project
    val logbackVersion: String by project
    val exposedVersion: String by project
    val pgVersion: String by project
    val spekVersion: String by project
    val sendGridVersion: String by project
    val assertJVersion: String by project

    // Kotlin ============================================================================
    implementation(platform("org.jetbrains.kotlin:kotlin-bom"))
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")

    // Libs ============================================================================
    // Ktor - framework
    implementation("io.ktor", "ktor-server-jetty", ktorVersion)
    implementation("io.ktor", "ktor-server-core", ktorVersion)
    implementation("io.ktor", "ktor-server-host-common", ktorVersion)
    implementation("io.ktor", "ktor-auth", ktorVersion)
    implementation("io.ktor", "ktor-auth-jwt", ktorVersion)
    implementation("io.ktor", "ktor-gson", ktorVersion)
    implementation("io.ktor", "ktor-network-tls-certificates", ktorVersion)

    // Logback - application logger
    implementation("ch.qos.logback", "logback-classic", logbackVersion)

    // Exposed - orm
    implementation("org.jetbrains.exposed", "exposed-core", exposedVersion)
    implementation("org.jetbrains.exposed", "exposed-dao", exposedVersion)
    implementation("org.jetbrains.exposed", "exposed-jdbc", exposedVersion)
    implementation("org.jetbrains.exposed", "exposed-jodatime", exposedVersion)

    // Postgresql - database driver
    implementation("org.postgresql", "postgresql", pgVersion)

    // SendGrid - mailer
    implementation("com.sendgrid", "sendgrid-java", sendGridVersion)

    // Deploy ============================================================================
    providedCompile("com.google.appengine", "appengine", "1.9.60")

    // Tests ============================================================================
}

tasks {
    dokka {
        outputDirectory = "$buildDir/docs/dokka"
        outputFormat = "html"
    }

    test {
        useJUnitPlatform {
            includeEngines("spek2")
        }
    }
}

configure<AppEngineAppYamlExtension> {
    deploy {
        setAppEngineDirectory(".")
        version = "1"
        projectId = "XYZ-placeholder"
        stopPreviousVersion = true  // default - stop the current version
        promote = true              // default - & make this the current version
    }
    stage {
        setAppEngineDirectory(".")
    }
}

我的 app.yaml 文件:

runtime: java
env: flex

handlers:
  - url: /.*
    script: this field is required, but ignored

我得到部署已成功完成的信息,但是当我调用 gcloud app browse 时收到 403 错误。

HTTP ERROR 403 Problem accessing /. Reason:

Forbidden

我认为我的应用程序无法启动,这是来自 Jetty 的错误,但我不知道如何 check/confirm 并修复它。

此版本 GAE 中不存在任务 appengineRun

首先,为App Engine标准环境提供的Ktor教程,但是你在app.yaml文件中有"flex"env。 也推荐大家关注官方多多了解Google Cloud documentation for Ktor