Last active
May 27, 2022 16:15
-
-
Save MWhyte/6109ad3f4c51726666994cb15056f039 to your computer and use it in GitHub Desktop.
A Post: Creating Google Cloud Functions with Kotlin (https://github.com/mwhyte-dev/kotlin-google-cloud-function)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
A Post: Creating Google Cloud Functions with Kotlin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@file:Suppress("unused") | |
package dev.mwhyte.function | |
import com.google.cloud.functions.HttpFunction | |
import com.google.cloud.functions.HttpRequest | |
import com.google.cloud.functions.HttpResponse | |
import mu.KotlinLogging | |
import java.io.IOException | |
class App : HttpFunction { | |
private val logger = KotlinLogging.logger {} | |
@Throws(IOException::class) | |
override fun service(request: HttpRequest, response: HttpResponse) { | |
logger.info { "hello world" } | |
response.writer.write("FUNCTION COMPLETE") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.invoke.MethodHandles.invoker | |
val invoker by configurations.creating | |
plugins { | |
id("org.jetbrains.kotlin.jvm") version "1.3.72" | |
id("com.github.johnrengelman.shadow") version "6.0.0" | |
application | |
} | |
repositories { | |
jcenter() | |
} | |
dependencies { | |
implementation(platform("org.jetbrains.kotlin:kotlin-bom")) | |
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") | |
implementation("io.github.microutils:kotlin-logging:1.11.5") | |
implementation("com.google.cloud.functions:functions-framework-api:1.0.1") | |
invoker("com.google.cloud.functions.invoker:java-function-invoker:1.0.0-alpha-2-rc5") | |
testImplementation("org.jetbrains.kotlin:kotlin-test") | |
testImplementation("org.jetbrains.kotlin:kotlin-test-junit") | |
testImplementation("org.mockito:mockito-core:3.5.10") | |
testImplementation("com.google.truth:truth:1.0.1") | |
testImplementation("com.google.guava:guava-testlib:29.0-jre") | |
} | |
application { | |
mainClassName = "dev.mwhyte.function.AppKt" | |
} | |
task<JavaExec>("runFunction") { | |
main = "com.google.cloud.functions.invoker.runner.Invoker" | |
classpath(invoker) | |
inputs.files(configurations.runtimeClasspath, sourceSets["main"].output) | |
args( | |
"--target", project.findProperty("runFunction.target") ?: "dev.mwhyte.function.App", | |
"--port", project.findProperty("runFunction.port") ?: 8080 | |
) | |
doFirst { | |
args("--classpath", files(configurations.runtimeClasspath, sourceSets["main"].output).asPath) | |
} | |
} | |
tasks.named("build") { | |
dependsOn(":shadowJar") | |
} | |
task("buildFunction") { | |
dependsOn("build") | |
copy { | |
from("build/libs/" + rootProject.name + "-all.jar") | |
into("build/deploy") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
❯ ./gradlew buildFunction | |
BUILD SUCCESSFUL in 1s | |
10 actionable tasks: 10 up-to-date |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
❯ gcloud functions deploy my-test-function \ | |
--entry-point=dev.mwhyte.function.App \ | |
--source=build/deploy --runtime=java11 --trigger-http \ | |
--allow-unauthenticated | |
Deploying function (may take a while - up to 2 minutes)...⠹ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
❯ ./gradlew runFunction | |
> Task :runFunction | |
2021-07-13 20:21:00.608:INFO:oejs.Server:main: jetty-9.4.26.v20200117; built: 2020-01-17T12:35:33.676Z; git: 7b38981d25d14afb4a12ff1f2596756144edf695; jvm 15.0.1+9 | |
2021-07-13 20:21:00.646:INFO:oejsh.ContextHandler:main: Started o.e.j.s.ServletContextHandler@467aecef{/,null,AVAILABLE} | |
2021-07-13 20:21:00.671:INFO:oejs.AbstractConnector:main: Started ServerConnector@3d24753a{HTTP/1.1,[http/1.1]}{0.0.0.0:8080} | |
2021-07-13 20:21:00.672:INFO:oejs.Server:main: Started @485ms | |
Jul 13, 2021 8:21:00 PM com.google.cloud.functions.invoker.runner.Invoker logServerInfo | |
INFO: Serving function... | |
Jul 13, 2021 8:21:00 PM com.google.cloud.functions.invoker.runner.Invoker logServerInfo | |
INFO: Function: dev.mwhyte.function.App | |
Jul 13, 2021 8:21:00 PM com.google.cloud.functions.invoker.runner.Invoker logServerInfo | |
INFO: URL: http://localhost:8080/ | |
<==========---> 80% EXECUTING [15s] | |
> :runFunction |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
❯ ./gradlew runFunction -PrunFunction.target=dev.mwhyte.function.App -PrunFunction.port=8080 | |
> Task :runFunction | |
INFO: Function: dev.mwhyte.function.App | |
Jul 13, 2021 8:24:24 PM com.google.cloud.functions.invoker.runner.Invoker logServerInfo | |
INFO: URL: http://localhost:8080/ | |
<==========---> 80% EXECUTING [8s] | |
> :runFunction |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment