Perfect on App Engine
Contributed by the Google Cloud community. Not official Google documentation.
This tutorial shows a sample Swift app built with Perfect deployed to the App Engine flexible environment.
Perfect is "a complete and powerful toolbox, framework, and application server for Linux, iOS, and macOS (OS X)." It is open source on GitHub.
This tutorial assumes basic familiarity with Swift programming.
Objectives
- Create a Swift "Hello, world" app that uses the Perfect framework.
- Deploy the app to the App Engine flexible environment.
Costs
This tutorial uses billable components of Google Cloud, including App Engine flexible environment.
Use the Pricing Calculator to generate a cost estimate based on your projected usage.
Before you begin
- Create a project in the Cloud Console.
- Enable billing for your project.
- Install the Cloud SDK.
Handling dependencies
We'll use the Swift Package Manager to manage our app's dependencies.
Create a
Package.swift
file with the following contents:import PackageDescription let package = Package( name: "PerfectGAE", targets: [ Target(name: "PerfectGAE", dependencies: []) ], dependencies: [ .Package(url: "https://github.com/PerfectlySoft/Perfect-HTTPServer.git", majorVersion: 2, minor: 0) ] )
Writing the server
Create a
main.swift
with the following contents:import Foundation import PerfectLib import PerfectHTTP import PerfectHTTPServer // Create HTTP server. let server = HTTPServer() var routes = Routes() // Respond to App Engine health check requests // TODO: see #2 // Basic GET request // TODO: see #3 // Add the routes to the server. server.addRoutes(routes) // Set a listen port of 8080 server.serverPort = 8080 do { // Launch the HTTP server. try server.start() } catch PerfectError.networkError(let err, let msg) { print("Network error thrown: \(err) \(msg)") }
Create a route to handle App Engine health-check requests" (per the custom runtime docs):
// Respond to App Engine health check requests routes.add(method: .get, uri: "/_ah/health", handler: { request, response in print("GET - /_ah/health route handler...") response.setBody(string: "OK") response.completed() })
Create a route to handle
GET
requests to/hello
:// Basic GET request routes.add(method: .get, uri: "/hello", handler: { request, response in print("GET - /hello route handler...") response.setBody(string: "Hello from Swift on App Engine flexible environment!") response.completed() })
Creating the Dockerfile
Since Swift doesn't have an officially supported App Engine runtime, we'll create our own.
Create a
Dockerfile
with the following contents:FROM ibmcom/swift-ubuntu:latest LABEL Description="Docker image for Swift + Perfect on App Engine flexible environment." # Get extra dependencies for Perfect RUN apt-get update && apt-get install -y \ openssl \ libssl-dev \ uuid-dev # Expose default port for App Engine EXPOSE 8080 # Copy sources RUN mkdir /root/PerfectGAE ADD main.swift /root/PerfectGAE ADD Package.swift /root/PerfectGAE # Build the app RUN cd /root/PerfectGAE && swift build # Run the app USER root CMD ["/root/PerfectGAE/.build/debug/PerfectGAE"]
Deploying the app
Create an
app.yaml
file with the following contents:runtime: custom env: flex
Run the following command to deploy your app (make take several minutes):
gcloud app deploy
Run the following command to view your app, then append
/hello
to the URL:gcloud app browse
If all goes well, you should see "Hello from Swift on App Engine flexible environment!".
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.