Skip to content

Commit 25d5817

Browse files
MVP
1 parent 2a4388b commit 25d5817

19 files changed

+319
-28
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
build
22
swift-shared-libs
3-
.serverless
3+
.serverless
4+
.build

CODE_OF_CONDUCT.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Contributor Covenant Code of Conduct
2+
3+
## Our Pledge
4+
5+
In the interest of fostering an open and welcoming environment, we as
6+
contributors and maintainers pledge to making participation in our project and
7+
our community a harassment-free experience for everyone, regardless of age, body
8+
size, disability, ethnicity, sex characteristics, gender identity and expression,
9+
level of experience, education, socio-economic status, nationality, personal
10+
appearance, race, religion, or sexual identity and orientation.
11+
12+
## Our Standards
13+
14+
Examples of behavior that contributes to creating a positive environment
15+
include:
16+
17+
* Using welcoming and inclusive language
18+
* Being respectful of differing viewpoints and experiences
19+
* Gracefully accepting constructive criticism
20+
* Focusing on what is best for the community
21+
* Showing empathy towards other community members
22+
23+
Examples of unacceptable behavior by participants include:
24+
25+
* The use of sexualized language or imagery and unwelcome sexual attention or
26+
advances
27+
* Trolling, insulting/derogatory comments, and personal or political attacks
28+
* Public or private harassment
29+
* Publishing others' private information, such as a physical or electronic
30+
address, without explicit permission
31+
* Other conduct which could reasonably be considered inappropriate in a
32+
professional setting
33+
34+
## Our Responsibilities
35+
36+
Project maintainers are responsible for clarifying the standards of acceptable
37+
behavior and are expected to take appropriate and fair corrective action in
38+
response to any instances of unacceptable behavior.
39+
40+
Project maintainers have the right and responsibility to remove, edit, or
41+
reject comments, commits, code, wiki edits, issues, and other contributions
42+
that are not aligned to this Code of Conduct, or to ban temporarily or
43+
permanently any contributor for other behaviors that they deem inappropriate,
44+
threatening, offensive, or harmful.
45+
46+
## Scope
47+
48+
This Code of Conduct applies both within project spaces and in public spaces
49+
when an individual is representing the project or its community. Examples of
50+
representing a project or community include using an official project e-mail
51+
address, posting via an official social media account, or acting as an appointed
52+
representative at an online or offline event. Representation of a project may be
53+
further defined and clarified by project maintainers.
54+
55+
## Enforcement
56+
57+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
58+
reported by contacting the project team at [email protected]. All
59+
complaints will be reviewed and investigated and will result in a response that
60+
is deemed necessary and appropriate to the circumstances. The project team is
61+
obligated to maintain confidentiality with regard to the reporter of an incident.
62+
Further details of specific enforcement policies may be posted separately.
63+
64+
Project maintainers who do not follow or enforce the Code of Conduct in good
65+
faith may face temporary or permanent repercussions as determined by other
66+
members of the project's leadership.
67+
68+
## Attribution
69+
70+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71+
available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
72+
73+
[homepage]: https://www.contributor-covenant.org
74+
75+
For answers to common questions about this code of conduct, see
76+
https://www.contributor-covenant.org/faq

CONTRIBUTING.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Contributing Guidelines
2+
3+
Thank you for your interest in contributing to our project. Whether it's a bug report, new feature, correction, or additional
4+
documentation, we greatly value feedback and contributions from our community.
5+
6+
Please read through this document before submitting any issues or pull requests to ensure we have all the necessary
7+
information to effectively respond to your bug report or contribution.
8+
9+
10+
## Reporting Bugs/Feature Requests
11+
12+
We welcome you to use the GitHub issue tracker to report bugs or suggest features.
13+
14+
When filing an issue, please check existing open, or recently closed, issues to make sure somebody else hasn't already
15+
reported the issue. Please try to include as much information as you can. Details like these are incredibly useful:
16+
17+
* A reproducible test case or series of steps
18+
* The version of our code being used
19+
* Any modifications you've made relevant to the bug
20+
* Anything unusual about your environment or deployment
21+
22+
23+
## Contributing via Pull Requests
24+
Contributions via pull requests are much appreciated. Before sending us a pull request, please ensure that:
25+
26+
1. You are working against the latest source on the *master* branch.
27+
2. You check existing open, and recently merged, pull requests to make sure someone else hasn't addressed the problem already.
28+
3. You open an issue to discuss any significant work - we would hate for your time to be wasted.
29+
30+
To send us a pull request, please:
31+
32+
1. Fork the repository.
33+
2. Modify the source; please focus on the specific change you are contributing. If you also reformat all the code, it will be hard for us to focus on your change.
34+
3. Ensure local tests pass.
35+
4. Commit to your fork using clear commit messages.
36+
5. Send us a pull request, answering any default questions in the pull request interface.
37+
6. Pay attention to any automated CI failures reported in the pull request, and stay involved in the conversation.
38+
39+
GitHub provides additional document on [forking a repository](https://help.github.com/articles/fork-a-repo/) and
40+
[creating a pull request](https://help.github.com/articles/creating-a-pull-request/).
41+
42+
43+
## Finding contributions to work on
44+
Looking at the existing issues is a great way to find something to contribute on. As our projects, by default, use the default GitHub issue labels ((enhancement/bug/duplicate/help wanted/invalid/question/wontfix), looking at any 'help wanted') issues is a great place to start.
45+
46+
47+
## Code of Conduct
48+
This project has adopted the [contributor-covenant.org](https://www.contributor-covenant.org) code of conduct.
49+
50+
51+
## Security issue notifications
52+
If you discover a potential security issue in this projectplease do **not** create a public github issue.
53+
If the security issue is relared to the library send a private email.
54+
If the security issue is related to AWS notify [AWS/Amazon Security](http://aws.amazon.com/security/vulnerability-reporting/).
55+
If the security issue is related to Swift notify [Apple Security](https://support.apple.com/en-ie/HT201220).
56+
57+
58+
## Licensing
59+
60+
See the LICENSE file for our project's licensing. We will ask you to confirm the licensing of your contribution.
61+
62+
We may ask you to sign a [Contributor License Agreement (CLA)](http://en.wikipedia.org/wiki/Contributor_License_Agreement) for larger changes.

Makefile

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,20 @@
1414

1515
# Use this tag to build a customized local image
1616

17-
SWIFT_VERSION?=5.2.3
18-
LAYER_VERSION?=5-2.3
17+
SWIFT_VERSION?=nightly-amazonlinux2
18+
LAYER_VERSION?=nightly-amazonlinux2
19+
DOCKER_OS?=amazonlinux2
20+
21+
# SWIFT_VERSION?=5.2.3-bionic
22+
# LAYER_VERSION?=5-2-3-bionic
23+
# DOCKER_OS=bionic
1924

2025
DOCKER_TAG=nio-swift:$(SWIFT_VERSION)
2126
SWIFT_DOCKER_IMAGE=$(DOCKER_TAG)
2227
SWIFT_LAMBDA_LIBRARY=nio-swift-lambda-runtime-$(LAYER_VERSION)
2328
SWIFT_CONFIGURATION=release
2429

30+
BUILD_PATH=.build
2531
SERVERLESS_BUILD=build
2632
SERVERLESS_LAYER=swift-lambda-runtime
2733

@@ -44,6 +50,15 @@ LAMBDA_BUILD_PATH=$(ROOT_BUILD_PATH)
4450
# use this for local development
4551
MOUNT_ROOT=$(shell pwd)
4652
DOCKER_PROJECT_PATH=$(SWIFT_PROJECT_PATH)
53+
ROOT_BUILD_PATH=./.build
54+
LAYER_BUILD_PATH=$(ROOT_BUILD_PATH)/layer
55+
LAMBDA_BUILD_PATH=$(ROOT_BUILD_PATH)/lambda
56+
LOCAL_LAMBDA_PATH=$(ROOT_BUILD_PATH)/local
57+
LOCALSTACK_TMP=$(ROOT_BUILD_PATH)/.tmp
58+
TMP_BUILD_PATH=$(ROOT_BUILD_PATH)/tmp
59+
DATETIME=$(shell date +'%y%m%d-%H%M%S')
60+
DOCKER_FOLDER=docker
61+
BOOTSTRAP=$(DOCKER_FOLDER)/$(SWIFT_VERSION)/bootstrap
4762

4863
docker_build:
4964
docker build --tag $(DOCKER_TAG) docker/$(SWIFT_VERSION)/.
@@ -56,41 +71,42 @@ build_lambda:
5671
$(SWIFT_DOCKER_IMAGE) \
5772
/bin/bash -c "swift build --configuration $(SWIFT_CONFIGURATION)"
5873

59-
cp_to_serverless_build: create_build_directory
74+
cp_lambda_to_sls_build_local: create_build_directory
6075
docker run \
6176
--rm \
6277
--volume "$(MOUNT_ROOT)/:/src" \
6378
--workdir "/src/$(DOCKER_PROJECT_PATH)" \
6479
$(SWIFT_DOCKER_IMAGE) \
65-
/bin/bash -c "swift build -c $(SWIFT_CONFIGURATION) --show-bin-path | tr '\n' '/' > .build/path.txt; echo '$(SWIFT_EXECUTABLE)' >> .build/path.txt | cat .build/path.txt | xargs cp -t ../$(SERVERLESS_BUILD); rm .build/path.txt"
80+
/bin/bash -c "swift build -c $(SWIFT_CONFIGURATION) --show-bin-path | tr '\n' '/' > $(BUILD_PATH)/path.txt; echo '$(SWIFT_EXECUTABLE)' >> $(BUILD_PATH)/path.txt | cat $(BUILD_PATH)/path.txt | xargs cp -t ../$(SERVERLESS_BUILD); rm $(BUILD_PATH)/path.txt"
6681

6782
create_build_directory:
6883
if [ ! -d "$(LAMBDA_BUILD_PATH)" ]; then mkdir -p $(LAMBDA_BUILD_PATH); fi
6984
if [ ! -d "$(LAYER_BUILD_PATH)" ]; then mkdir -p $(LAYER_BUILD_PATH); fi
7085
if [ ! -d "$(SERVERLESS_BUILD)" ]; then mkdir -p $(SERVERLESS_BUILD); fi
7186

7287
package_lambda: create_build_directory build_lambda
73-
zip -r -j $(LAMBDA_BUILD_PATH)/$(LAMBDA_ZIP) $(SWIFT_PROJECT_PATH)/.build/$(SWIFT_CONFIGURATION)/$(SWIFT_EXECUTABLE)
88+
zip -r -j $(LAMBDA_BUILD_PATH)/$(LAMBDA_ZIP) $(SWIFT_PROJECT_PATH)/$(BUILD_PATH)/$(SWIFT_CONFIGURATION)/$(SWIFT_EXECUTABLE)
7489

7590
package_layer: create_build_directory
7691
$(eval SHARED_LIBRARIES := $(shell cat docker/$(SWIFT_VERSION)/swift-shared-libraries.txt | tr '\n' ' '))
7792
mkdir -p $(SHARED_LIBS_FOLDER)/lib
93+
ifeq '$(DOCKER_OS)' 'xenial'
7894
docker run \
7995
--rm \
8096
--volume "$(shell pwd)/:/src" \
8197
--workdir "/src" \
8298
$(SWIFT_DOCKER_IMAGE) \
8399
cp /lib64/ld-linux-x86-64.so.2 $(SHARED_LIBS_FOLDER)
100+
endif
84101
docker run \
85102
--rm \
86103
--volume "$(shell pwd)/:/src" \
87104
--workdir "/src" \
88105
$(SWIFT_DOCKER_IMAGE) \
89106
cp -t $(SHARED_LIBS_FOLDER)/lib $(SHARED_LIBRARIES)
90-
zip -r $(LAYER_BUILD_PATH)/$(LAYER_ZIP) bootstrap $(SHARED_LIBS_FOLDER)
107+
cp $(BOOTSTRAP) $(SHARED_LIBS_FOLDER)
108+
cd $(SHARED_LIBS_FOLDER); pwd; zip -r ../$(LAYER_BUILD_PATH)/$(LAYER_ZIP) bootstrap lib
91109

92-
unzip_package_to_build: create_build_directory
110+
cp_layer_to_sls_build_local: create_build_directory
93111
if [ ! -d "./$(SERVERLESS_BUILD)/$(SERVERLESS_LAYER)" ]; then mkdir -p ./$(SERVERLESS_BUILD)/$(SERVERLESS_LAYER); fi
94-
cp $(LAYER_BUILD_PATH)/$(LAYER_ZIP) ./$(SERVERLESS_BUILD)/$(SERVERLESS_LAYER)/$(LAYER_ZIP)
95-
unzip ./$(SERVERLESS_BUILD)/$(SERVERLESS_LAYER)/$(LAYER_ZIP) -d ./$(SERVERLESS_BUILD)/$(SERVERLESS_LAYER)
96-
rm ./$(SERVERLESS_BUILD)/$(SERVERLESS_LAYER)/$(LAYER_ZIP)
112+
cp -R ./$(SHARED_LIBS_FOLDER)/. ./$(SERVERLESS_BUILD)/$(SERVERLESS_LAYER)

Products/Package.resolved

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

Products/Package.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ let package = Package(
77
name: "swift-rest-api",
88
products: [
99
// Products define the executables and libraries produced by a package, and make them visible to other packages.
10+
.executable(name: "Products", targets: ["Products"]),
1011
.library(
1112
name: "ProductService",
1213
targets: ["ProductService"]

README.md

Lines changed: 107 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,119 @@
1-
#Serverless Swift REST API
1+
# AWS Serverless Swift-Sprinter API Template
22

3+
[![Swift 5.2.3](https://img.shields.io/badge/Swift-5.2.3-blue.svg)](https://swift.org/download/) [![docker amazonlinux2](https://img.shields.io/badge/docker-amazonlinux2-orange.svg)](https://swift.org/download/)
34

4-
Install Serverless Framework
5+
This package demostrates how to write a Scalable REST API with the Serverless stack by using only Swift as development language.
6+
7+
## Swift-Sprinter Product API Example
8+
9+
The example shows how to build a Rest API based on a `Product` swift class.
10+
11+
```swift
12+
public struct Product: Codable {
13+
public let sku: String
14+
public let name: String
15+
public let description: String
16+
public var createdAt: String?
17+
public var updatedAt: String?
18+
}
519
```
6-
curl -o- -L https://slss.io/install | bash
20+
21+
## API Definition
22+
23+
```
24+
- /Product
25+
-> GET
26+
-> POST
27+
-> PUT
28+
- /Product/{sku}
29+
-> DELETE
30+
-> GET
731
```
832

9-
Export the binary PATH
33+
JSON body for PUT and POST
34+
```JSON
35+
{
36+
"sku": "3",
37+
"name": "Book",
38+
"description": "Book 3"
39+
}
1040
```
11-
export PATH="$HOME/.serverless/bin:$PATH"
41+
42+
## Serverless architecture
43+
44+
The architecture is based on the classical AWS Serverless stack: APIGateway, Lambda and DynamoDB.
45+
- `APIGateway`: acts as a `proxy` for the `Lambda` and exposing it to the internet.
46+
- `Lambda`: is the computational layer.
47+
- `DynamoDB`: is the AWS `NoSQL` database
48+
49+
Advantages:
50+
- Pay per use
51+
- No fixed costs
52+
- Auto-Scaling
53+
- DevOps
54+
55+
## Application
56+
57+
The application uses [Swift-Sprinter](https://github.com/swift-sprinter/aws-lambda-swift-sprinter) as AWS Custom Lambda Runtime and acts as a presentation layer of the DynamoDB content via Web.
58+
59+
The following frameworks are used:
60+
- [aws-lambda-swift-sprinter-nio-plugin](https://github.com/swift-sprinter/aws-lambda-swift-sprinter-nio-plugin): Implements the AWS Custom Runtime usin Swift NIO.
61+
- [aws-sdk-swift](https://github.com/swift-aws/aws-sdk-swift): Interacts with DynamoDB
62+
63+
## Requirements
64+
65+
- Install [Docker](https://docs.docker.com/install/)
66+
- Install [Serverless Framework](https://www.serverless.com/framework/docs/getting-started/)
67+
- Ensure your AWS Account has the right [credentials](https://www.serverless.com/framework/docs/providers/aws/guide/credentials/) to deploy a Serverless stack.
68+
- Clone this repository. From the command line type:
69+
70+
```console
71+
git clone https://github.com/swift-sprinter/aws-serverless-swift-sprinter-web-template
72+
cd aws-serverless-swift-sprinter-web-template
1273
```
74+
- Ensure you can run `make`:
1375

14-
Build Swift
76+
```console
77+
make --version
1578
```
16-
build.sh
79+
80+
the `Makefile` was developed with this version:
1781
```
82+
GNU Make 3.81
83+
Copyright (C) 2006 Free Software Foundation, Inc.
84+
This is free software; see the source for copying conditions.
85+
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
86+
PARTICULAR PURPOSE.
1887
19-
Deploy Swift
88+
This program built for i386-apple-darwin11.3.0
89+
```
90+
91+
## Build
92+
93+
Use the following command to build the code before using the serverless commands:
94+
```
95+
./build.sh
96+
```
97+
98+
## Deploy
99+
100+
Deploy the full solution to your AWS using Serverless:
101+
```
102+
./deploy.sh
103+
```
104+
105+
After the deployment is completed, the URL of the website is provided by the Serverless framework.
106+
107+
## Update
108+
109+
Rebuild the code and update the Lambda to your AWS using Serverless:
110+
```
111+
./update.sh
112+
```
113+
114+
## Remove
115+
116+
To remove the deployment use:
117+
```
118+
serverless remove
20119
```
21-
deploy.sh
22-
```

bootstrap

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)