Skip to content

Commit 8ecc257

Browse files
committed
Initial commit
0 parents  commit 8ecc257

27 files changed

+1561
-0
lines changed

.github/FUNDING.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# These are supported funding model platforms
2+
3+
ko_fi: fabianfett # Replace with a single Ko-fi username

.github/workflows/ci.yaml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
name: CI
2+
on:
3+
push:
4+
branches:
5+
- master
6+
pull_request:
7+
branches:
8+
- master
9+
10+
jobs:
11+
"tuxOS-Tests":
12+
runs-on: ubuntu-latest
13+
strategy:
14+
matrix:
15+
tag: ['5.1']
16+
container:
17+
image: swift:${{ matrix.tag }}
18+
volumes:
19+
- $GITHUB_WORKSPACE:/src
20+
options: --workdir /src
21+
steps:
22+
- name: Checkout
23+
uses: actions/checkout@v1
24+
with:
25+
fetch-depth: 1
26+
- name: Install dependencies
27+
run: apt-get update && apt-get install -y zlib1g-dev zip openssl libssl-dev
28+
- name: Test
29+
run: swift test --enable-code-coverage --enable-test-discovery
30+
- name: Convert coverage files
31+
run: llvm-cov export -format="lcov" .build/debug/vapor-lambda-runtimePackageTests.xctest -instr-profile .build/debug/codecov/default.profdata > info.lcov
32+
- name: Upload to codecov.io
33+
uses: codecov/[email protected]
34+
with:
35+
token: ${{secrets.CODECOV_TOKEN}}
36+
37+
"macOS-Tests":
38+
runs-on: macOS-latest
39+
steps:
40+
- name: Checkout
41+
uses: actions/checkout@v1
42+
with:
43+
fetch-depth: 1
44+
- name: Show all Xcode versions
45+
run: ls -an /Applications/ | grep Xcode*
46+
- name: Change Xcode command line tools
47+
run: sudo xcode-select -s /Applications/Xcode_11.2.app/Contents/Developer
48+
- name: SPM Build
49+
run: swift build
50+
- name: SPM Tests
51+
run: swift test --parallel -Xswiftc -DDEBUG
52+
- name: Xcode Tests
53+
run: |
54+
swift package generate-xcodeproj
55+
xcodebuild -quiet -parallel-testing-enabled YES -scheme vapor-lambda-runtime-Package -enableCodeCoverage YES build test
56+
- name: Codecov
57+
run: bash <(curl -s https://codecov.io/bash) -t ${{secrets.CODECOV_TOKEN}}

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.DS_Store
2+
.build
3+
/*.xcodeproj
4+
xcuserdata
5+
packaged.yaml
6+
examples/**/bootstrap
7+
examples/**/lambda.zip

Package.resolved

Lines changed: 151 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// swift-tools-version:5.1
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "vapor-lambda-runtime",
8+
platforms: [
9+
.macOS(.v10_14)
10+
],
11+
products: [
12+
.library(
13+
name: "VaporLambdaRuntime",
14+
targets: ["VaporLambdaRuntime"]
15+
),
16+
],
17+
dependencies: [
18+
.package(url: "https://github.com/apple/swift-nio.git", .upToNextMajor(from: "2.9.0")),
19+
.package(url: "https://github.com/vapor/vapor.git", from: "4.0.0-beta.3.1"),
20+
.package(url: "https://github.com/fabianfett/swift-lambda-runtime.git", .upToNextMajor(from: "0.4.0")),
21+
.package(url: "https://github.com/fabianfett/swift-base64-kit", .upToNextMajor(from: "0.2.0")),
22+
],
23+
targets: [
24+
.target(
25+
name: "VaporLambdaRuntime",
26+
dependencies: ["Vapor", "LambdaRuntime", "Base64Kit", "NIO", "NIOHTTP1"]),
27+
.testTarget(
28+
name: "VaporLambdaRuntimeTests",
29+
dependencies: ["VaporLambdaRuntime"]),
30+
]
31+
)
32+

README.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# vapor-lambda-runtime
2+
3+
Run your Vapor app on AWS Lambda. This package bridges the communication between [`swift-lambda-runtime`](https://github.com/fabianfett/swift-aws-lambda)
4+
and the [vapor](https://github.com/vapor/vapor) framework. APIGateway requests are transformed into `Vapor.Request`s and `Vapor.Response`s are written back to the APIGateway.
5+
6+
This project is intended to be run using the [Swift Layer from the amazonlinux-swift project](https://fabianfett.de/amazonlinux-swift).
7+
8+
## Status
9+
10+
**Note: Currently this is nothing more than a proof of concept. Use at your own risk. I would like to hear feedback, if you played with this. Please open a GitHub issues for all open ends, you experience.**
11+
12+
What I have tested:
13+
14+
- [x] Routing
15+
- [x] JSON Coding
16+
- [x] Cors Middleware
17+
- [ ] Fluent
18+
- There are probably tons of other things that we should test. I haven't been a Vpor developer so far, so you will need to help me list the things to test.
19+
20+
Examples:
21+
- [HelloWorld](examples/Hello/Sources/Hello/main.swift)
22+
- [Super simple TodoBackend](examples/VaporTodoLambda/Sources/VaporTodoLambda/main.swift) example with DynamoDB backend (terrible code) using aws-sdk-swift
23+
24+
If you test anything, please open a PR so that we can document the state of afairs better. A super small example would be even better. I plan to create some integration tests with the examples.
25+
26+
## Usage
27+
28+
Add `vapor-lambda-runtime` and `vapor` as dependencies to your project. For
29+
this open your `Package.swift`:
30+
31+
```swift
32+
dependencies: [
33+
.package(url: "https://github.com/vapor/vapor.git", from: "4.0.0-beta.3.1"),
34+
.package(url: "https://github.com/fabianfett/vapor-lambda-runtime", .upToNextMajor(from: "0.1.0")),
35+
]
36+
```
37+
38+
Add VaporLambdaRuntime as depency to your target:
39+
40+
```swift
41+
targets: [
42+
.target(name: "Hello", dependencies: ["Vapor", "VaporLambdaRuntime"]),
43+
]
44+
```
45+
46+
Create a simple Vapor app.
47+
48+
```swift
49+
import Vapor
50+
import VaporLambdaRuntime
51+
52+
let app = Application()
53+
defer { app.shutdown() }
54+
55+
struct Name: Codable {
56+
let name: String
57+
}
58+
59+
struct Hello: Content {
60+
let hello: String
61+
}
62+
63+
app.get("hello") { (req) -> Hello in
64+
return Hello(hello: "world")
65+
}
66+
67+
app.post("hello") { req -> Hello in
68+
let name = try req.content.decode(Name.self)
69+
return Hello(hello: name.name)
70+
}
71+
```
72+
73+
Now we just need to run the vapor app. To enable running in Lambda, we
74+
need to change the "serve" command. Then we can start the app by calling
75+
`app.run()`
76+
77+
```swift
78+
app.commands.use(LambdaCommand(), as: "serve", isDefault: true)
79+
80+
try app.run()
81+
```
82+
83+
## Contributing
84+
85+
Please feel welcome and encouraged to contribute to vapor-lambda-runtime. The current version has a long way to go before being ready for production use and help is always welcome.
86+
87+
If you've found a bug, have a suggestion or need help getting started, please open an Issue or a PR. If you use this package, I'd be grateful for sharing your experience.
88+
89+
If you like this project, I'm excited about GitHub stars. 🤓
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import LambdaRuntime
2+
import Vapor
3+
import NIO
4+
import NIOHTTP1
5+
6+
extension APIGateway.Response {
7+
8+
init(response: Vapor.Response) {
9+
10+
if let string = response.body.string {
11+
self = APIGateway.Response(
12+
statusCode: response.status,
13+
headers: response.headers,
14+
body: string,
15+
isBase64Encoded: false)
16+
}
17+
else if let buffer = response.body.buffer {
18+
self = APIGateway.Response(
19+
statusCode: response.status,
20+
headers: response.headers,
21+
body: buffer.withUnsafeReadableBytes { (pointer) -> String in
22+
return String(base64Encoding: pointer)
23+
},
24+
isBase64Encoded: true)
25+
}
26+
else {
27+
self = APIGateway.Response(
28+
statusCode: response.status,
29+
headers: response.headers)
30+
}
31+
}
32+
33+
}

0 commit comments

Comments
 (0)