Skip to content

Restructure Examples folder #387

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Restructure Examples folder
  • Loading branch information
sebsto committed Sep 30, 2024
commit dc3daaffc7775b9c9a8663b39c2032bec5259087
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ xcuserdata
Package.resolved
.serverless
.vscode
Makefile
2 changes: 2 additions & 0 deletions Examples/APIGateway/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
samconfig.toml
Makefile
55 changes: 55 additions & 0 deletions Examples/APIGateway/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// swift-tools-version:6.0

import PackageDescription

// needed for CI to test the local version of the library
import class Foundation.ProcessInfo
import struct Foundation.URL

#if os(macOS)
let platforms: [PackageDescription.SupportedPlatform]? = [.macOS(.v15)]
#else
let platforms: [PackageDescription.SupportedPlatform]? = nil
#endif

let package = Package(
name: "swift-aws-lambda-runtime-example",
platforms: platforms,
products: [
.executable(name: "APIGAtewayLambda", targets: ["APIGAtewayLambda"])
],
dependencies: [
// dependency on swift-aws-lambda-runtime is added dynamically below
// .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", branch: "main")

.package(url: "https://github.com/swift-server/swift-aws-lambda-events.git", branch: "main")
],
targets: [
.executableTarget(
name: "APIGAtewayLambda",
dependencies: [
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
.product(name: "AWSLambdaEvents", package: "swift-aws-lambda-events"),
],
path: "."
)
]
)

if let localDepsPath = ProcessInfo.processInfo.environment["LAMBDA_USE_LOCAL_DEPS"],
localDepsPath != "",
let v = try? URL(fileURLWithPath: localDepsPath).resourceValues(forKeys: [.isDirectoryKey]),
let _ = v.isDirectory
{
print("[INFO] Compiling against swift-aws-lambda-runtime located at \(localDepsPath)")
package.dependencies += [
.package(name: "swift-aws-lambda-runtime", path: localDepsPath)
]

} else {
print("[INFO] LAMBDA_USE_LOCAL_DEPS is not pointing to your local swift-aws-lambda-runtime code")
print("[INFO] This project will compile against the main branch of the Lambda Runtime on GitHub")
package.dependencies += [
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", branch: "main")
]
}
1 change: 1 addition & 0 deletions Examples/APIGateway/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TO BE DONE
34 changes: 34 additions & 0 deletions Examples/APIGateway/Sources/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftAWSLambdaRuntime open source project
//
// Copyright (c) 2024 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
//
//===----------------------------------------------------------------------===//

import AWSLambdaEvents
import AWSLambdaRuntime
import class Foundation.JSONEncoder

let runtime = LambdaRuntime {
(event: APIGatewayV2Request, context: LambdaContext) -> APIGatewayV2Response in

var header = HTTPHeaders()
context.logger.debug("HTTP API Message received")

header["content-type"] = "application/json"

// echo the request in the response
let data = try JSONEncoder().encode(event)
let response = String(data: data, encoding: .utf8)

return APIGatewayV2Response(statusCode: .ok, headers: header, body: response)
}

try await runtime.run()
31 changes: 31 additions & 0 deletions Examples/APIGateway/template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: SAM Template for QuoteService

Resources:
# Lambda function
APIGAtewayLambda:
Type: AWS::Serverless::Function
Properties:
CodeUri: .build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/APIGAtewayLambda/APIGAtewayLambda.zip
Timeout: 60
Handler: swift.bootstrap
Runtime: provided.al2
MemorySize: 512
Architectures:
- arm64
Environment:
Variables:
# by default, AWS Lambda runtime produces no log
# use `LOG_LEVEL: debug` for for lifecycle and event handling information
# use `LOG_LEVEL: trace` for detailed input event information
LOG_LEVEL: trace
Events:
HttpApiEvent:
Type: HttpApi

Outputs:
# print API Gateway endpoint
APIGAtewayEndpoint:
Description: API Gateway endpoint UR"
Value: !Sub "https://${ServerlessHttpApi}.execute-api.${AWS::Region}.amazonaws.com"
34 changes: 0 additions & 34 deletions Examples/Benchmark/Package.swift

This file was deleted.

4 changes: 4 additions & 0 deletions Examples/HelloWorld/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
response.json
samconfig.toml
template.yaml
Makefile
53 changes: 53 additions & 0 deletions Examples/HelloWorld/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// swift-tools-version:6.0

import PackageDescription

#if os(macOS)
let platforms: [PackageDescription.SupportedPlatform]? = [.macOS(.v15)]
#else
let platforms: [PackageDescription.SupportedPlatform]? = nil
#endif

// needed for CI to test the local version of the library
import class Foundation.ProcessInfo
import struct Foundation.URL

let package = Package(
name: "swift-aws-lambda-runtime-example",
platforms: platforms,
products: [
.executable(name: "MyLambda", targets: ["MyLambda"])
],
dependencies: [
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", branch: "main")
],
targets: [
.executableTarget(
name: "MyLambda",
dependencies: [
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime")
],
path: "."
)
]
)

if let localDepsPath = ProcessInfo.processInfo.environment["LAMBDA_USE_LOCAL_DEPS"], localDepsPath != "" {

print("++++++++ \(localDepsPath)")

// check if directory exists
let u = URL(fileURLWithPath: localDepsPath)
if let v = try? u.resourceValues(forKeys: [.isDirectoryKey]), v.isDirectory! {
print("Compiling against swift-aws-lambda-runtime located at \(localDepsPath)")
package.dependencies = [
.package(name: "swift-aws-lambda-runtime", path: localDepsPath)
]
} else {
print("LAMBDA_USE_LOCAL_DEPS is not pointing to your local swift-aws-lambda-runtime code")
print("This project will compile against the main branch of the Lambda Runtime on GitHub")
}
} else {
print("++++++++ NO ENV VAR ")

}
Empty file added Examples/HelloWorld/README.md
Empty file.
23 changes: 23 additions & 0 deletions Examples/HelloWorld/Sources/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftAWSLambdaRuntime open source project
//
// Copyright (c) 2024 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
//
//===----------------------------------------------------------------------===//

import AWSLambdaRuntime

// in this example we are receiving and responding with strings

let runtime = LambdaRuntime { (event: String, context: LambdaContext) in
return "Hello \(event)"
}

try await runtime.run()
53 changes: 53 additions & 0 deletions Examples/v1/Benchmark/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// swift-tools-version:5.7

import PackageDescription

// needed for CI to test the local version of the library
import class Foundation.ProcessInfo
import struct Foundation.URL

let runtimeVersion = Version("1.0.0-alpha.3")

let package = Package(
name: "swift-aws-lambda-runtime-example",
platforms: [
.macOS(.v12)
],
products: [
.executable(name: "MyLambda", targets: ["MyLambda"])
],
dependencies: [
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: runtimeVersion)
],
targets: [
.executableTarget(
name: "MyLambda",
dependencies: [
.product(name: "AWSLambdaRuntimeCore", package: "swift-aws-lambda-runtime")
],
path: "."
)
]
)

// for CI to test the local version of the library
// if ProcessInfo.processInfo.environment["LAMBDA_USE_LOCAL_DEPS"] != nil {
// print("LAMBDA_USE_LOCAL_DEPS is ignored for runtime v1 examples.")
// print("This project will compile against runtime version \(runtimeVersion)")
// }

if let localDepsPath = ProcessInfo.processInfo.environment["LAMBDA_USE_LOCAL_DEPS"], localDepsPath != "" {

// check if directory exists
let u = URL(fileURLWithPath: localDepsPath)
if let v = try? u.resourceValues(forKeys: [.isDirectoryKey]), v.isDirectory! {
print("Compiling against swift-aws-lambda-runtime located at \(localDepsPath)")
package.dependencies = [
.package(name: "swift-aws-lambda-runtime", path: localDepsPath)
]
} else {
print("LAMBDA_USE_LOCAL_DEPS is not pointing to your local swift-aws-lambda-runtime code")
print("This project will compile against runtime version \(runtimeVersion)")
}

}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ let package = Package(
// demonstrate different types of error handling
],
dependencies: [
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0-alpha")
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0-alpha.3")
],
targets: [
.executableTarget(
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading
Loading