Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions Examples/EndToEndDebugging/README.md

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 50;
objectVersion = 52;
objects = {

/* Begin PBXBuildFile section */
Expand All @@ -13,6 +13,7 @@
F7B6C204246121E900607A89 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = F7B6C203246121E900607A89 /* Assets.xcassets */; };
F7B6C207246121E900607A89 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = F7B6C206246121E900607A89 /* Preview Assets.xcassets */; };
F7B6C20A246121E900607A89 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = F7B6C208246121E900607A89 /* LaunchScreen.storyboard */; };
F7EA8D6024762E4000B0D09E /* Shared in Frameworks */ = {isa = PBXBuildFile; productRef = F7EA8D5F24762E4000B0D09E /* Shared */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -31,6 +32,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
F7EA8D6024762E4000B0D09E /* Shared in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -42,6 +44,7 @@
children = (
F7B6C1FC246121E800607A89 /* MyApp */,
F7B6C1FB246121E800607A89 /* Products */,
F7EA8D5E24762E4000B0D09E /* Frameworks */,
);
sourceTree = "<group>";
};
Expand Down Expand Up @@ -75,6 +78,13 @@
path = "Preview Content";
sourceTree = "<group>";
};
F7EA8D5E24762E4000B0D09E /* Frameworks */ = {
isa = PBXGroup;
children = (
);
name = Frameworks;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand All @@ -91,6 +101,9 @@
dependencies = (
);
name = MyApp;
packageProductDependencies = (
F7EA8D5F24762E4000B0D09E /* Shared */,
);
productName = MyApp;
productReference = F7B6C1FA246121E800607A89 /* MyApp.app */;
productType = "com.apple.product-type.application";
Expand Down Expand Up @@ -340,6 +353,13 @@
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */

/* Begin XCSwiftPackageProductDependency section */
F7EA8D5F24762E4000B0D09E /* Shared */ = {
isa = XCSwiftPackageProductDependency;
productName = Shared;
};
/* End XCSwiftPackageProductDependency section */
};
rootObject = F7B6C1F2246121E800607A89 /* Project object */;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,43 @@
//
//===----------------------------------------------------------------------===//

import Shared
import SwiftUI

struct ContentView: View {
@State var name: String = "World"
@State var name: String = ""
@State var password: String = ""
@State var response: String = ""

var body: some View {
VStack(alignment: .leading, spacing: 20) {
TextField("Enter your name", text: $name)
TextField("Username", text: $name)
SecureField("Password", text: $password)
Button(
action: self.sayHello,
action: self.register,
label: {
Text("say hello")
Text("Register")
.padding()
.foregroundColor(.white)
.background(Color.black)
.border(Color.black, width: 2)
}
)
Text(response)
.foregroundColor(response.starts(with: "CommunicationError") ? .red : .black)
}.padding(100)
}

func sayHello() {
func register() {
guard let url = URL(string: "http://localhost:7000/invoke") else {
fatalError("invalid url")
}

var request = URLRequest(url: url)
request.httpMethod = "POST"
request.httpBody = self.name.data(using: .utf8)

guard let jsonRequest = try? JSONEncoder().encode(Request(name: self.name, password: self.password)) else {
fatalError("encoding error")
}
request.httpBody = jsonRequest

let task = URLSession.shared.dataTask(with: request as URLRequest) { data, response, error in
do {
Expand All @@ -59,7 +64,8 @@ struct ContentView: View {
guard let data = data else {
throw CommunicationError(reason: "invald response, empty body")
}
self.response = String(data: data, encoding: .utf8)!
let response = try JSONDecoder().decode(Response.self, from: data)
self.response = response.message
} catch {
self.response = "\(error)"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ let package = Package(
// in real-world projects this would say
// .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0")
.package(name: "swift-aws-lambda-runtime", path: "../../.."),
.package(name: "Shared", path: "../Shared"),
],
targets: [
.target(
name: "MyLambda", dependencies: [
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
.product(name: "Shared", package: "Shared"),
]
),
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
//===----------------------------------------------------------------------===//

import AWSLambdaRuntime
import Shared

try Lambda.withLocalServer {
Lambda.run { (_: Lambda.Context, payload: String, callback: @escaping (Result<String, Error>) -> Void) in
callback(.success("Hello, \(payload)!"))
Lambda.run { (_: Lambda.Context, request: Request, callback: @escaping (Result<Response, Error>) -> Void) in
// TODO: something useful
callback(.success(Response(message: "Hello, \(request.name)!")))
}
}
28 changes: 28 additions & 0 deletions Examples/LocalDebugging/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Local Debugging Example

This sample project demonstrates how to write a simple Lambda function in Swift,
and how to use local debugging techniques that emulate how the Lambda function
would be invoked by the AWS Lambda Runtime engine.

The example includes three modules:

1. [MyApp](MyApp) is a SwiftUI iOS application that calls the Lambda function.
2. [MyLambda](MyLambda) is a SwiftPM executable package for the Lambda function.
3. [Shared](Shared) is a SwiftPM library package used for shared code between the iOS application and the Lambda function,
such as the Request and Response model objects.

The local debugging experience is achieved by running the Lambda function in the context of the debug only `Lambda.withLocalServer`
function which starts a local emulator enabling the communication
between the iOS application and the Lambda function over HTTP.

To try out this example, open the workspace in Xcode and "run" the two targets,
using the relevant `MyLambda` and `MyApp` Xcode schemas.

Start with running the `MyLambda` target on the "My Mac" destination, once it is up you should see a log message in the Xcode console saying
`LocalLambdaServer started and listening on 127.0.0.1:7000, receiving payloads on /invoke`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps make this into a code block

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will address when we merge #83 since it changes things

which means the local emulator is up and receiving traffic on port `7000` and expecting payloads on the `/invoke` endpoint.

Continue to run the `MyApp` target in a simulator destination. Once up, the application's UI should appear in the simulator allowing you
to interact with it.

Once both targets are running, set up breakpoints in the iOS application or Lambda function to observe the system behavior.
16 changes: 16 additions & 0 deletions Examples/LocalDebugging/Shared/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// swift-tools-version:5.2
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "Shared",
products: [
.library(name: "Shared", targets: ["Shared"]),
],
dependencies: [
],
targets: [
.target(name: "Shared", dependencies: []),
]
)
35 changes: 35 additions & 0 deletions Examples/LocalDebugging/Shared/Sources/Shared/Shared.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftAWSLambdaRuntime open source project
//
// Copyright (c) 2020 Apple Inc. and the SwiftAWSLambdaRuntime project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

public struct Request: Codable, CustomStringConvertible {
public let name: String
public let password: String

public init(name: String, password: String) {
self.name = name
self.password = password
}

public var description: String {
"name: \(self.name), password: ***"
}
}

public struct Response: Codable {
public let message: String

public init(message: String) {
self.message = message
}
}
15 changes: 15 additions & 0 deletions Examples/LocalDebugging/Shared/Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftAWSLambdaRuntime open source project
//
// Copyright (c) 2020 Apple Inc. and the SwiftAWSLambdaRuntime project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

preconditionFailure("use `swift test --enable-test-discovery`")
3 changes: 3 additions & 0 deletions docker/docker-compose.1804.52.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@ services:
test:
image: swift-aws-lambda:18.04-5.2

test-samples:
image: swift-aws-lambda:18.04-5.2

shell:
image: swift-aws-lambda:18.04-5.2
3 changes: 3 additions & 0 deletions docker/docker-compose.1804.53.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@ services:
test:
image: swift-aws-lambda:18.04-5.3

test-samples:
image: swift-aws-lambda:18.04-5.3

shell:
image: swift-aws-lambda:18.04-5.3
7 changes: 7 additions & 0 deletions docker/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ services:
<<: *common
command: /bin/bash -cl "swift test --enable-test-discovery -Xswiftc -warnings-as-errors $${SANITIZER_ARG-}"

test-samples:
<<: *common
command: >-
/bin/bash -clx "
swift build --package-path Examples/LambdaFunctions &&
swift build --package-path Examples/LocalDebugging/MyLambda"

# util

shell:
Expand Down