-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate to AppEngine gen2 on java17 with Micronaut and Handlebars (#7)
- Loading branch information
1 parent
ed13f11
commit 1e73990
Showing
36 changed files
with
696 additions
and
575 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
## Running a datastore locally | ||
|
||
* Docs: https://cloud.google.com/datastore/docs/tools/datastore-emulator#windows | ||
* Old docs: https://cloud.google.com/appengine/docs/legacy/standard/java/tools/using-local-server#datastore | ||
|
||
1. Install the [Google Cloud SDK](https://cloud.google.com/sdk/). | ||
2. Install the emulator: | ||
```shell | ||
gcloud components install cloud-datastore-emulator | ||
gcloud components install beta | ||
``` | ||
3. Run the datastore emulator in the background: | ||
``` | ||
gcloud --project twisterrob-travel beta emulators datastore start --no-store-on-disk | ||
``` | ||
4. In a separate window run: | ||
``` | ||
gcloud beta emulators datastore env-init | ||
``` | ||
5. Set the output environment variables before running `gradlew :AppEngine:run`. | ||
|
||
Note: step 4 and 5 are automated in `build.gradle`. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,87 +1,109 @@ | ||
import org.gradle.nativeplatform.platform.internal.DefaultNativePlatform | ||
|
||
apply plugin: 'java' | ||
apply plugin: 'war' | ||
apply plugin: 'com.google.cloud.tools.appengine-appenginewebxml' | ||
//apply plugin: 'com.google.cloud.tools.endpoints-framework-server' | ||
apply plugin: 'io.micronaut.minimal.application' | ||
apply plugin: 'com.google.cloud.tools.appengine-appyaml' | ||
apply plugin: 'idea' | ||
|
||
java { | ||
sourceCompatibility = JavaVersion.VERSION_1_8 | ||
targetCompatibility = JavaVersion.VERSION_1_8 | ||
} | ||
|
||
configurations { | ||
dev | ||
} | ||
idea { | ||
module { | ||
scopes.PROVIDED.plus += [ configurations.dev ] | ||
} | ||
sourceCompatibility = JavaVersion.VERSION_17 | ||
targetCompatibility = JavaVersion.VERSION_17 | ||
} | ||
|
||
dependencies { | ||
// com.google.appengine.api.datastore | ||
implementation("com.google.appengine:appengine-api-1.0-sdk:${VERSION_APPENGINE}") | ||
// com.google.api.server.spi.EndpointsServlet | ||
implementation("com.google.endpoints:endpoints-framework:${VERSION_GCLOUD_ENDPOINTS}") | ||
implementation("com.google.appengine:appengine-api-1.0-sdk:2.0.12") | ||
implementation(platform("com.google.cloud:libraries-bom:${VERSION_GCLOUD}")) | ||
implementation("com.google.cloud:google-cloud-datastore") | ||
|
||
providedCompile 'javax.servlet:javax.servlet-api:3.1.0' | ||
dev("org.apache.taglibs:taglibs-standard-impl:1.2.5") | ||
dev("javax.servlet.jsp.jstl:jstl-api:1.2") | ||
runtimeOnly("org.yaml:snakeyaml") | ||
runtimeOnly("io.micronaut.serde:micronaut-serde-jackson") | ||
implementation("io.micronaut.views:micronaut-views-handlebars") | ||
|
||
implementation project(':Shared') | ||
|
||
// See java.util.logging.properties and log4j2.xml | ||
implementation("org.slf4j:slf4j-api:${VERSION_SLF4J}") | ||
// route apps SLF4J logging to JUL | ||
implementation("org.slf4j:slf4j-jdk14:${VERSION_SLF4J}") | ||
dev("org.slf4j:jul-to-slf4j:${VERSION_SLF4J}") | ||
dev("org.apache.logging.log4j:log4j-api:${VERSION_LOG4J}") | ||
dev("org.apache.logging.log4j:log4j-core:${VERSION_LOG4J}") | ||
dev("org.apache.logging.log4j:log4j-slf4j-impl:${VERSION_LOG4J}") | ||
|
||
runtimeOnly("org.apache.logging.log4j:log4j-slf4j2-impl:${VERSION_LOG4J}") | ||
// for org.apache.tools.ant.filters.StringInputStream | ||
implementation("ant:ant:1.6.5") | ||
|
||
apply from: "${rootDir}/gradle/testCompile.gradle", to: project | ||
testImplementation("com.google.appengine:appengine-testing:${VERSION_APPENGINE}") | ||
testImplementation("com.google.appengine:appengine-api-stubs:${VERSION_APPENGINE}") | ||
testImplementation("com.google.appengine:appengine-api-labs:${VERSION_APPENGINE}") | ||
testImplementation("io.micronaut:micronaut-http-client") | ||
testImplementation("com.github.jtidy:jtidy:${VERSION_JTIDY}") | ||
} | ||
|
||
sourceSets { | ||
main { | ||
java.srcDir 'src/main/diff' | ||
compileClasspath += configurations.dev | ||
} | ||
test { | ||
java.srcDir 'src/test/diff' | ||
compileClasspath += configurations.dev | ||
} | ||
} | ||
|
||
appengine { com.google.cloud.tools.gradle.appengine.standard.AppEngineStandardExtension ext -> | ||
//noinspection UnnecessaryQualifiedReference | ||
appengine { com.google.cloud.tools.gradle.appengine.appyaml.AppEngineAppYamlExtension ext -> | ||
stage { | ||
artifact = file("src/main/appengine/placeholder.txt") | ||
// AppEngine plugin can't handle laziness, wire manually. | ||
tasks.appengineStage.dependsOn(tasks.installDist) | ||
extraFilesDirectories = tasks.installDist | ||
} | ||
deploy { | ||
// Live: https://twisterrob-london.appspot.com/ | ||
projectId = "twisterrob-london" | ||
version = "GCLOUD_CONFIG" | ||
} | ||
run { | ||
port = 8888 | ||
automaticRestart = true | ||
|
||
def logDependencyFilter = { Dependency d -> d.group?.contains('slf4j') || d.group?.contains('log4j') }; | ||
def deps = configurations.dev.dependencies.findAll logDependencyFilter | ||
def flags = configurations.dev.files(deps as Dependency[]) \ | ||
.collect { "-Xbootclasspath/p:${it.absolutePath}".toString() } | ||
jvmFlags = (jvmFlags?: [ ]) + flags | ||
// Test deployment: | ||
//version = "test" | ||
//stopPreviousVersion = false | ||
//promote = false | ||
} | ||
tools { | ||
cloudSdkHome = System.getenv("GCLOUD_HOME")?: file("build/downloaded/gcloud-sdk") | ||
System.getenv("GCLOUD_HOME")?.tap { cloudSdkHome = it } | ||
cloudSdkVersion = VERSION_GCLOUD_SDK as String | ||
// https://cloud.google.com/sdk/gcloud/reference#--verbosity | ||
verbosity = "info" | ||
} | ||
} | ||
|
||
//endpointsServer { | ||
// hostname = "twisterrob-london.appspot.com" | ||
//} | ||
application { | ||
mainClass.set("net.twisterrob.blt.gapp.Application") | ||
} | ||
|
||
micronaut { | ||
version = VERSION_MICRONAUT as String | ||
runtime("jetty") | ||
testRuntime("junit4") | ||
processing { | ||
incremental(true) | ||
annotations("net.twisterrob.blt.gapp.*") | ||
} | ||
} | ||
|
||
tasks.named("run").configure { JavaExec task -> | ||
if (DefaultNativePlatform.getCurrentOperatingSystem().isWindows()) { | ||
task.systemProperty("log4j.skipJansi", false) | ||
} | ||
if (gradle.startParameter.continuous) { | ||
task.systemProperties( | ||
// TODO use https://docs.micronaut.io/latest/guide/index.html#environments to create overrides for debug. | ||
"micronaut.io.watch.enabled": true, | ||
"micronaut.io.watch.restart": true, | ||
"micronaut.io.watch.paths": "src/main", | ||
) | ||
} | ||
doFirst { | ||
def stream = new ByteArrayOutputStream() | ||
exec { | ||
commandLine("gcloud.cmd", "beta", "emulators", "datastore", "env-init") | ||
standardOutput = stream | ||
} | ||
Map<String, String> env = stream.toString().split("\r?\n").collectEntries { line -> | ||
if (!line.startsWith("set ")) { | ||
throw new IllegalStateException("Unexpected line: $line in \n${stream.toString()}") | ||
} | ||
def (key, value) = line.substring(4).split("=", 2) | ||
[ key, value ] | ||
} | ||
task.environment(env) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# https://cloud.google.com/appengine/docs/standard/reference/app-yaml?tab=java | ||
runtime: java17 | ||
entrypoint: bin/AppEngine | ||
|
||
instance_class: F1 | ||
|
||
automatic_scaling: | ||
min_instances: 0 | ||
max_instances: 1 | ||
|
||
handlers: | ||
- url: /.* | ||
secure: always | ||
redirect_http_response_code: 301 | ||
script: auto |
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
This file exists to replace the mandatory "artifact" in AppEngine YAML plugin. | ||
The real files come from the `org.gradle.application` plugin's `installDist` task (build/install/AppEngine). |
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
17 changes: 17 additions & 0 deletions
17
AppEngine/src/main/java/net/twisterrob/blt/gapp/Application.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package net.twisterrob.blt.gapp; | ||
|
||
import io.micronaut.context.ApplicationContext; | ||
import io.micronaut.runtime.Micronaut; | ||
|
||
public class Application { | ||
public static void main(String... args) { | ||
Micronaut micronaut = Micronaut | ||
.build(args) | ||
.classes(Application.class) | ||
.banner(false) | ||
; | ||
try (ApplicationContext context = micronaut.start()) { | ||
// Nothing yet, wrap in try-with-resources to ensure teardown. | ||
} | ||
} | ||
} |
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
35 changes: 35 additions & 0 deletions
35
AppEngine/src/main/java/net/twisterrob/blt/gapp/IndexController.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package net.twisterrob.blt.gapp; | ||
|
||
import io.micronaut.http.HttpResponse; | ||
import io.micronaut.http.MediaType; | ||
import io.micronaut.http.MutableHttpResponse; | ||
import io.micronaut.http.annotation.Controller; | ||
import io.micronaut.http.annotation.Get; | ||
import io.micronaut.http.server.types.files.StreamedFile; | ||
import io.micronaut.views.View; | ||
|
||
import net.twisterrob.blt.gapp.viewmodel.Versions; | ||
|
||
@Controller | ||
public class IndexController { | ||
|
||
@Get("/") | ||
@View("index") | ||
public MutableHttpResponse<?> index() { | ||
return HttpResponse.ok(new IndexModel(new Versions())); | ||
} | ||
|
||
@Get("/favicon.ico") | ||
public StreamedFile favicon() { | ||
return new StreamedFile( | ||
IndexController.class.getClassLoader().getResourceAsStream("public/favicon.ico"), | ||
MediaType.IMAGE_PNG_TYPE | ||
); | ||
} | ||
|
||
private record IndexModel( | ||
Versions versions | ||
) { | ||
|
||
} | ||
} |
Oops, something went wrong.