forked from GoogleCloudPlatform/community
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding Swift + Perfect tutorial (GoogleCloudPlatform#116)
* Adding Swift + Perfect tutorial * Update index.md Updating code formatting * Fix code snippets so they'll render correctly. Triple backticks inside list don't render. Also a several branding fixes (should avoid using abbreviations when referring to Google Cloud products). * Fix for my fix * One last indentation fix. * Branding updates in main.swift * Branding updates in Dockerfile * Update Dockerfile Cleaned up Dockerfile * Update index.md Use github name not google name * Update index.md Just some minor edits. Committing to the branch. * Update index.md
- Loading branch information
Showing
5 changed files
with
236 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
FROM ibmcom/swift-ubuntu:latest | ||
LABEL Description="Docker image for Swift + Perfect on Google 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 | ||
|
||
# Add app source | ||
ADD . /app | ||
WORKDIR /app | ||
|
||
# Build release | ||
RUN swift build --configuration release | ||
|
||
# Run the app | ||
ENTRYPOINT [".build/release/PerfectGAE"] |
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,12 @@ | ||
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) | ||
] | ||
) |
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 @@ | ||
runtime: custom | ||
env: flex |
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,164 @@ | ||
--- | ||
title: Perfect on Google App Engine Tutorial | ||
description: Learn how to build an app with Swift and Perfect in the Google App Engine flexible environment. | ||
author: mcdonamp | ||
tags: App Engine, Swift, Perfect | ||
date_published: 2017-03-21 | ||
--- | ||
This tutorial shows a sample [Swift][swift] app built with [Perfect][perfect] | ||
deployed to the Google 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][perfect-github]. | ||
|
||
This tutorial assumes basic familiarity with Swift programming. | ||
|
||
[swift]: http://swift.org | ||
[perfect]: https://perfect.org | ||
[perfect-github]: https://github.com/PerfectlySoft/Perfect | ||
|
||
## Objectives | ||
|
||
+ Create a Swift "Hello, world" app that uses the Perfect framework. | ||
+ Deploy the app to the Google App Engine flexible environment. | ||
|
||
## Costs | ||
|
||
This tutorial uses billable components of Google Cloud Platform, including: | ||
|
||
+ Google App Engine flexible environment | ||
|
||
Use the [Pricing Calculator][pricing] to generate a cost estimate based on your | ||
projected usage. | ||
|
||
[pricing]: https://cloud.google.com/products/calculator | ||
|
||
## Before you begin | ||
|
||
1. Create a project in the [Google Cloud Platform Console][console]. | ||
1. Enable billing for your project. | ||
1. Install the [Google Cloud SDK][cloud-sdk]. | ||
|
||
[console]: https://console.cloud.google.com/ | ||
[cloud-sdk]: https://cloud.google.com/sdk/ | ||
|
||
## Handling dependencies | ||
|
||
We'll use the [Swift Package Manager][spm] to manage our app's dependencies. | ||
|
||
1. 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) | ||
] | ||
) | ||
|
||
[spm]: https://github.com/apple/swift-package-manager | ||
|
||
## Writing the server | ||
|
||
1. 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 | ||
... | ||
|
||
// Basic GET request | ||
... | ||
|
||
// 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)") | ||
} | ||
|
||
1. Create a route to handle App Engine health-check requests" (per the [custom runtime docs][custom-runtime]): | ||
|
||
// 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() | ||
}) | ||
|
||
1. 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 Google App Engine flexible environment!") | ||
response.completed() | ||
}) | ||
|
||
[custom-runtime]: https://cloud.google.com/appengine/docs/flexible/custom-runtimes/build#lifecycle_events | ||
|
||
## 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 + Perfect on Google 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 | ||
|
||
1. Create an `app.yaml` file with the following contents: | ||
|
||
runtime: custom | ||
env: flex | ||
|
||
1. Run the following command to deploy your app (make take several minutes): | ||
|
||
gcloud app deploy | ||
|
||
1. 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 Google App Engine flexible environment!". |
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,37 @@ | ||
import Foundation | ||
import PerfectLib | ||
import PerfectHTTP | ||
import PerfectHTTPServer | ||
|
||
// Create HTTP server. | ||
let server = HTTPServer() | ||
var routes = Routes() | ||
|
||
// 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() | ||
}) | ||
|
||
// 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 Google App Engine flexible environment!") | ||
response.completed() | ||
}) | ||
|
||
// 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)") | ||
} |