Edit on GitHub
Report issue
Page history

Submitted by @{[ TutorialCtrl.tutorial.github_username ]} {[ TutorialCtrl.tutorial.date_published | date:'mediumDate' ]}

title: Kitura on Google App Engine Tutorial description: Learn how to build an app with Swift and Kitura in the Google App Engine flexible environment. author: mpmcdonald tags: App Engine, Swift, Kitura date_published: 2017-03-21


This tutorial shows a sample Swift app built with Kitura deployed to the Google App Engine flexible environment.

Kitura is "a high performance and simple to use web framework for building modern Swift applications." It is open source on GitHub.

This tutorial assumes basic familiarity with Swift programming.

Objectives

  • Create a Swift "Hello, world" app that uses the Kitura framework.
  • Deploy the app to the App Engine flexible environment.

Costs

This tutorial uses billable components of Google Cloud Platform, including:

  • Google App Engine flexible environment

Use the Pricing Calculator to generate a cost estimate based on your projected usage.

Before you begin

  1. Create a project in the Google Cloud Platform Console.
  2. Enable billing for your project.
  3. Install the Google Cloud SDK.

Handling dependencies

We'll use the Swift Package Manager to manage our app's dependencies.

  1. Create a package.swift file with the following contents:

    import PackageDescription
    
    let package = Package(
        name: "KituraGAE",
        targets: [
            Target(name: "KituraGAE", dependencies: [])
        ],
        dependencies: [
            .Package(url: "https://github.com/IBM-Swift/Kitura.git", majorVersion: 1, minor: 3),
        ]
    )
    

Writing the server

  1. Create a main.swift file with the following contents:

    import Foundation
    import Kitura
    
    // All apps need a Router instance
    let router = Router()
    
    // Respond to App Engine health check requests
    // TODO: see #2
    
    // Basic GET request
    // TODO: see #3
    
    // Start server on 8080
    Kitura.addHTTPServer(onPort: 8080, with: router)
    Kitura.run()
    
  2. Create a route to handle App Engine health-check requests (per the custom runtime docs):

    // Respond to App Engine health check requests
    router.all("/_ah/health") { request, response, _ in
        print("ALL - /_ah/health route handler...")
        try response.send("OK").end()
    }
    
  3. Create a route to handle GET requests to /hello:

    // Basic GET request
    router.get("/hello") { request, response, _ in
        print("GET - /hello route handler...")
        try response.status(.OK).send("Hello from Swift on Google App Engine flexible environment!").end()
    }
    

Creating the Dockerfile

Since Swift doesn't have an officially supported App Engine runtime, we'll create our own.

  1. Create a Dockerfile with the following contents:

    FROM ibmcom/swift-ubuntu:latest
    LABEL Description="Docker image for Swift + Kitura on Google App Engine flexible environment."
    
    # Expose default port for App Engine
    EXPOSE 8080
    
    # Copy sources
    RUN mkdir /root/KituraGAE
    ADD main.swift /root/KituraGAE
    ADD Package.swift /root/KituraGAE
    
    # Build the app
    RUN cd /root/KituraGAE && swift build
    
    # Run the app
    USER root
    CMD ["/root/KituraGAE/.build/debug/KituraGAE"]
    

Deploying the app

  1. Create an app.yaml file with the following contents:

    runtime: custom
    env: flex
    
  2. Run the following command to deploy your app. It might take several minutes:

    gcloud app deploy
    
  3. Visit http://[YOUR_PROJECT_ID].appspot.com/hello to see the deployed app.

        Replace `[YOUR_PROJECT_ID]` with your Google Cloud Platform project ID.
    

If all goes well, you should see "Hello from Swift on Google App Engine flexible environment!".

See more by @{[ TutorialCtrl.tutorial.github_username ]} and more tagged {[ tag ]}{[ $last ? '' : ', ' ]}

Submit a Tutorial

Share step-by-step guides

SUBMIT A TUTORIAL

Request a Tutorial

Ask for community help

SUBMIT A REQUEST

GCP Tutorials

Tutorials published by GCP

VIEW TUTORIALS

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see our Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Monitor your resources on the go

Get the Google Cloud Console app to help you manage your projects.