<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Marc Tatham on Medium]]></title>
        <description><![CDATA[Stories by Marc Tatham on Medium]]></description>
        <link>https://medium.com/@marctatham?source=rss-d805d4d71ae9------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*AKNSfGQ8bBcNZWgGrjGLRQ.jpeg</url>
            <title>Stories by Marc Tatham on Medium</title>
            <link>https://medium.com/@marctatham?source=rss-d805d4d71ae9------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 18:37:20 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@marctatham/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[A Ktor API on Google App Engine’s Java 11 Standard Environment]]></title>
            <link>https://medium.com/@marctatham/a-ktor-api-on-google-app-engines-java-11-standard-environment-bbc5f4be56d1?source=rss-d805d4d71ae9------2</link>
            <guid isPermaLink="false">https://medium.com/p/bbc5f4be56d1</guid>
            <category><![CDATA[app-engine]]></category>
            <category><![CDATA[ktor]]></category>
            <category><![CDATA[gradle-kotlin-dsl]]></category>
            <category><![CDATA[google-app-engine]]></category>
            <category><![CDATA[google-cloud-platform]]></category>
            <dc:creator><![CDATA[Marc Tatham]]></dc:creator>
            <pubDate>Wed, 15 Apr 2020 11:30:08 GMT</pubDate>
            <atom:updated>2020-04-16T14:25:51.445Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Google App Engine" src="https://cdn-images-1.medium.com/max/1024/1*UTFqa8-LzImCXnZBUvdB7A.jpeg" /></figure><h3>Context</h3><p>I’ve recently been interested in putting together a simple backend API for a mobile application I’ve been working on. In getting this project setup and running I encountered a number of issues, largely to do with tools not working quite as expected, outdated documentation, etc. And I believe that a short walk-through like this to help get things setup might be of value to others.</p><p>I’ve always believed that if you’re going to do something, you may as well do it <em>right</em>. So going into this endeavor, I already know that I want the following…</p><p>• I want to follow a microservice architecture<br>• I want to use a familiar language and tooling (Kotlin, Gradle, IntelliJ)<br>• I want to be able to scale my backend from 0 to N<br>• I want as little hassle putting all of this together so that I can focus on the logic of my backend API</p><p>With the objective clear, I set about choosing some of the fundamental elements of my Tech stack.</p><h3><strong>The Tech Stack</strong></h3><ul><li><a href="https://ktor.io/"><strong>Ktor</strong></a> (version 1.3.2)<br>A simple, lightweight framework for developing web applications, written in Kotlin &amp; comes with complete support for rich Kotlin features like <a href="https://kotlinlang.org/docs/reference/coroutines-overview.html">coroutines</a></li><li><strong>Gradle </strong>(version 6.3)<strong><br></strong>And we’re <a href="https://docs.gradle.org/current/userguide/kotlin_dsl.html">specifically going to be using the Kotlin DSL</a> because… Kotlin ❤</li><li><strong>Google’s App Engine <br></strong>App Engine supports microservice architecture via multiple services for a single project (or alternatively via multiple projects). I’m going specifically the <a href="https://cloud.google.com/appengine/docs/standard/java11">Java 11 Standard Environment</a> because there are some <a href="https://storage.googleapis.com/gweb-cloudblog-publish/images/memory_limits.max-1200x1200.png">gains over the java 8 standard environment</a> coupled with the fact that this is a side project so 🤷‍♂ ️why use old tech?</li></ul><h3><strong>TL</strong>;<strong>DR</strong></h3><p>If you don’t care much to read through the walk-through, you can jump straight into the GitHub repository found right here:<br><a href="https://github.com/marctatham/ktor-appengine-java11-standardenv">https://github.com/marctatham/ktor-appengine-java11-standardenv</a></p><h3><strong>Creating the project</strong></h3><figure><img alt="IntelliJ plugin for Ktor — project generation" src="https://cdn-images-1.medium.com/max/741/1*6Kplg3xgR9yIdsliVr6YUQ.png" /></figure><p>Let’s start out by creating a new Ktor project. If you’re using IntelliJ you can grab the <a href="https://plugins.jetbrains.com/plugin/10823-ktor">official Ktor plugin</a> or you can use the <a href="https://ktor.io/quickstart/generator.html">Ktor project generator</a>. Either way, this tutorial isn’t focused around on the intricacies of Ktor since that’s been <a href="https://ktor.io/quickstart/guides.html">documented very well</a>, so I’ve opted to tweak only some stock standard basics:</p><ul><li>GradleKotlinDSL</li><li>Jetty server</li><li>Ktor’s current latest version 1.3.2</li></ul><p>Complete the rest of the wizard choosing your desired file location, etc. &amp; presto! We have a really basic project that we can use to fire up the jetty server hosting our incredibly simple Ktor based application.</p><p>Running the project locally won’t be all that exciting, the HTTP Server will be spun up and start listening on port 8080 (as is configured in the application.conf) but we haven’t configured our Ktor app to reply to any request yet. So let’s update the Application.kt to respond with a simple “Hello World”</p><pre>fun Application.module(testing: Boolean = false) {<br><br>    routing <strong>{<br>        </strong>get(&quot;/&quot;) <strong>{<br>            </strong><em>call</em>.respondText(&quot;hello world&quot;, contentType = ContentType.Text.Plain)<br>        <strong>}<br>    }<br></strong>}</pre><p>Running the application now should give you something a little more exciting and Intellij makes this nice and easy for us to run locally:</p><figure><img alt="Play ▶️" src="https://cdn-images-1.medium.com/max/653/1*Dv5YERyrfZcmcSYBx1Lq7Q.png" /></figure><p>And you should see get your response.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/502/1*nX8aTp501Roy7xn-a2iZdg.png" /></figure><h3>Google App Engine setup &amp; configuration</h3><p>In order to continue we’re going to need to setup and configure some google bits. It’s best to simply follow the steps listed in the links provided, but at a high level you’re going to be:</p><ul><li>Creating a <a href="https://cloud.google.com/appengine/docs/standard/java11/console">Google App Engine Project</a></li><li><a href="https://console.cloud.google.com/projectselector/billing?_ga=2.239519235.122054097.1586616918-1169350006.1584793046">Enabling Billing</a> <br>Be sure to make use of your <a href="https://cloud.google.com/free/docs/gcp-free-tier">free quota</a>, especially while you’re still exploring the platform.</li><li>Installing <a href="https://cloud.google.com/sdk/docs">Google Cloud SDK</a></li><li><a href="https://cloud.google.com/sdk/gcloud/reference/auth/login">Signing in to Google Cloud CLI</a></li></ul><h3>Creating the UberJar</h3><p>Because the intention is to host our application on App Engine’s Java 11 Standard Environment, A look <a href="https://cloud.google.com/appengine/docs/standard/java11/runtime">at their documentation</a> says:</p><blockquote>If your Maven or Gradle build file produces an executable Uber JAR, the runtime starts your application by running an Uber JAR application<br>…<br>Where the example YOUR-ARTIFACT.jar application jar must:<br>Contain a Main-Class entry in its META-INF/MANIFEST.MF metadata file</blockquote><p>OK. So we’re going to need to construct an Uber Jar that contains all of our dependencies and has the main class specified in the manifest. No problem, because there’s great tooling in place for this, thanks to John Engelman’s gradle plugin, <a href="https://plugins.gradle.org/plugin/com.github.johnrengelman.shadow">Shadow</a>. A brief overview of the addition to your gradle configuration:</p><pre>plugins <strong>{<br>    <em>...</em></strong><br><br>    // Shadow plugin - enable support for building our UberJar<br>    id(&quot;com.github.johnrengelman.shadow&quot;) <em>version </em>&quot;5.2.0&quot;<br><strong>}</strong></pre><pre>... </pre><pre><em>tasks </em><strong>{<br>    </strong><em>named</em>&lt;ShadowJar&gt;(&quot;shadowJar&quot;) <strong>{<br>        </strong><br>        // required - see PR for more details <br>        mergeServiceFiles()<br><br>        // Jar must contain a Main-Class entry in its <br>        // META-INF/MANIFEST.MF metadata file<br>        manifest <strong>{<br>            </strong>attributes(<em>mapOf</em>(&quot;Main-Class&quot; <em>to application</em>.<em>mainClassName</em>))<br>        <strong>}<br>    }</strong></pre><p>Constructing the UberJar is as simple as running the new gradle task brought in by the shadow plugin:<br>./gradlew clean shadowJar</p><p>And to test locally, simply run the UberJar which should of course spin up the HTTP Server and start listening on the configured port.<br>java -jar build\libs\ktor-appengine-java11-standardenv-0.0.1-all.jar</p><p>To see how I’ve gone about adding the plugin and configuring it to properly construct my UberJar, take a look at the following pull request:<br><a href="https://github.com/marctatham/ktor-appengine-java11-standardenv/pull/1">https://github.com/marctatham/ktor-appengine-java11-standardenv/pull/1</a></p><h3>Predictable Deployments to App Engine via Gradle</h3><p>Because we’re now at a point where we’ve got an UberJar that works locally, we are pretty confident that this will work in our Java 11 standard environment. To deploy to App Engine we need some configuration to help instruct App Engine which environment our app is intended to run in. So in my project’s root directory I’ve created a new directory called <strong>appengine </strong>to house all the App Engine configuration for my application. There is more config, but in its simplest form it contains just one <a href="https://cloud.google.com/appengine/docs/standard/java11/config/appref">app.yaml</a> file that contains the following minimum configuration to specify our desired runtime environment.</p><pre><em>#More detail regarding the app.yaml can be found here:<br>#https://cloud.google.com/appengine/docs/standard/java11/config/appref<br></em>runtime: java11</pre><p>Now we’re going to need to add the <strong>Google App Engine Gradle plugin </strong>to our <em>build.gradle.kts</em>. Unfortunately the App Engine plugin <a href="https://github.com/GoogleCloudPlatform/app-gradle-plugin/pull/343">is not available on Gradle’s Plugin Repository yet</a>, so we need to apply the plugin the old fashioned way (unlike in the case of the shadow gradle plugin)</p><pre>buildscript <strong>{<br>    </strong><em>repositories </em><strong>{ </strong>jcenter() <strong>}<br>    </strong>dependencies <strong>{ </strong><em>classpath</em>(&quot;com.google.cloud.tools:appengine-gradle-plugin:2.2.0&quot;) <strong>}<br>}<br></strong>apply <strong>{<br>    </strong>plugin(&quot;com.google.cloud.tools.appengine&quot;)<br><strong>}</strong></pre><p>Now that the plugin has been added we’re going to configure it to</p><ul><li>use our <em>GCLOUD_CONFIG </em><br>Because we’ve already signed into our GCloud CLI and specified the project to use (note you can specify your project ID if you like instead)</li><li>point to our App Engine configuration</li><li>upload our deterministically built UberJar</li><li>use our explicitly defined <strong>version</strong><br>Deployments of services to AppEngine require a version and will be generated if not provided, this helps with deterministic and meaningful versions numbers that follow whatever your desired versioning system is.</li></ul><pre><em>configure</em>&lt;AppEngineAppYamlExtension&gt; <strong>{<br>    </strong>tools <strong>{<br>        </strong>// configure the Cloud Sdk tooling<br>    <strong>}<br><br>    </strong>stage <strong>{</strong><br><strong>        </strong>// configure staging for deployment<br><strong>        </strong>setAppEngineDirectory(&quot;appengine&quot;)<br>        setArtifact(&quot;build/libs/$uberJarFileName&quot;)<br>    <strong>}<br><br>    </strong>deploy <strong>{<br>        </strong>// configure deployment to app engine<br><strong>        </strong><em>projectId </em>= &quot;GCLOUD_CONFIG&quot;<br>        <em>version </em>= versionApp        <br>        <em>stopPreviousVersion </em>= true  // stop the old version<br>        <em>promote </em>= true              // use this the version<br>    <strong>}<br>}</strong></pre><p>With the above we’re able to deploy the UberJar artifact that we built with the <em>shadowJar </em>task by running the <em>appengineDeploy </em>task. <strong>But </strong>we must still remember to first build the UberJar and then deploy it: <br>./gradlew clean shadowJar<br>./gradlew appengineDeploy</p><p>We can remove this element of human error by making a minor tweak to the gradle tasks to and ensure that the <em>assemble </em>task results in the construction of the UberJar (instead of or as well as the regular Jar built without its dependecies by the <em>jar </em>task)</p><pre><em>tasks </em><strong>{<br>    val shadowJarTask =</strong> <em>named</em>&lt;ShadowJar&gt;(&quot;shadowJar&quot;) <strong>{<br>        // ... as defined above <br>    }<br><br>    </strong>// we want the UberJar, so this task has limited value<br>    named(&quot;jar&quot;) <strong>{<br>        </strong><em>enabled </em>= false<br>    <strong>}<br><br>    </strong>// update the `assemble` task to ensure the creation<br><strong>    </strong>// of a brand new UberJar using the shadowJar task<br>    named(&quot;assemble&quot;) <strong>{<br>        </strong>dependsOn(<strong>shadowJarTask</strong>)<br>    <strong>}<br>}</strong></pre><p>And with that, we have ensured that to deploy a new version of our service to Google App Engine is as a simple as running the <em>appengineDeploy </em>task.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gwVHHbjap986d64EGkVL1w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/502/1*BwL1AeUz2ghmruvRMuPaLg.png" /></figure><p>To see how I’ve gone about adding the ability to predictably deploy to Google App Engine via gradle, simply take a look at the PR:<br><a href="https://github.com/marctatham/ktor-appengine-java11-standardenv/pull/2">https://github.com/marctatham/ktor-appengine-java11-standardenv/pull/2</a></p><h3>That’s all for now</h3><p>That helps get your first Ktor service up and running &amp; this is where this walk-through ends. There are of course still key decisions and challenges when embracing a microservices architecure on Google’s App Engine, things like <a href="https://cloud.google.com/appengine/docs/standard/java/microservices-on-app-engine">service isolation vs project isolation</a>, or <a href="https://cloud.google.com/appengine/docs/standard/java11/communicating-between-services">how to communicate between services</a>, <a href="https://cloud.google.com/appengine/docs/standard/java11/how-requests-are-routed">request routing</a>, configuring different environments for Production or Testing and of course so much more!</p><p>If you have any interest in a follow up write up regarding microservices on Google’s App Engine, please do give the article a clap or let me know in the comments. Of course any suggestions, comments or clarifications and improvements are always welcome on the GitHub repository over at: <br><a href="https://github.com/marctatham/ktor-appengine-java11-standardenv">https://github.com/marctatham/ktor-appengine-java11-standardenv</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bbc5f4be56d1" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>