Skip to content

Commit dd1ea46

Browse files
Initial commit
0 parents  commit dd1ea46

17 files changed

+895
-0
lines changed

docker-compose.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
version: '2'
2+
services:
3+
database:
4+
build: mysql
5+
command: --default-authentication-plugin=mysql_native_password
6+
restart: always
7+
environment:
8+
MYSQL_ROOT_PASSWORD: myPassword1
9+
MYSQL_DATABASE: test
10+
appswift:
11+
build: server
12+
depends_on: [database]
13+
links:
14+
- database
15+
ports:
16+
- "8080:8080"
17+
environment:
18+
DATABASE_NAME: test
19+
DATABASE_HOST: database
20+
DATABASE_USERNAME: root
21+
DATABASE_PASSWORD: myPassword1

docs/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Topcoder Starter Packs - Swift/Perfect and MySQL API
2+
3+
## Deployment dependencies
4+
5+
Before performing a Deployment, it's assumed that the following have been set up:
6+
7+
- OS X 10.13.6+
8+
- Swift 4.2.1 (Perfect framework supports this version)
9+
- Docker
10+
11+
## Organization of the submission
12+
- `server`- the directory with sample server written in Swift (with Perfect framework)
13+
- `mysql` - the directory with Dockerfile used to create MySQL image
14+
- `docs` - this directory contains the documentation, including this deployment guide
15+
16+
## 3rd party libraries
17+
18+
- Perfect and its subframeworks
19+
- SwiftyJSON
20+
21+
## Configuration
22+
23+
The server can be configured using environment variables or right in `server/SampleServer/Sources/SampleServer/Configuration.swift`:
24+
The environment variables should be provided in `docker-comppose.yml`:
25+
- `DATABASE_NAME` - the database name. Check that content of `mysql/Docker` and make sure the database name is the same;
26+
- `DATABASE_HOST` - the host where MySQL is installed. It should be the name of the corresponding container if launched in a "docker network";
27+
- `DATABASE_USERNAME` - database username used to connect;
28+
- `DATABASE_PASSWORD` - database password used to connect.
29+
30+
## Launch the database and sample server
31+
```
32+
docker-compose up --build
33+
```
34+
35+
## Debug and test server and MySQL separately
36+
37+
Follow README.md files in `server/` and `mysql/` directories for more information.
38+
39+
## Verification
40+
41+
1. Launch the containers using `docker-compose up --build` command as mentioned above
42+
2. Use Postman sample requests provided in `docs/postman` to verify the server.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
{
2+
"info": {
3+
"_postman_id": "e7b9fda9-049b-46d6-a57a-fefd2b444cbb",
4+
"name": "Swift Server",
5+
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
6+
},
7+
"item": [
8+
{
9+
"name": "Get All",
10+
"request": {
11+
"method": "GET",
12+
"header": [],
13+
"url": {
14+
"raw": "{{URL}}/test",
15+
"host": [
16+
"{{URL}}"
17+
],
18+
"path": [
19+
"test"
20+
]
21+
}
22+
},
23+
"response": []
24+
},
25+
{
26+
"name": "Update",
27+
"request": {
28+
"method": "PUT",
29+
"header": [],
30+
"body": {
31+
"mode": "raw",
32+
"raw": "{\n\t\"handle\": \"updated2\"\n}"
33+
},
34+
"url": {
35+
"raw": "{{URL}}/test/00000000-0000-0000-D9EF-0B0000000000",
36+
"host": [
37+
"{{URL}}"
38+
],
39+
"path": [
40+
"test",
41+
"00000000-0000-0000-D9EF-0B0000000000"
42+
]
43+
}
44+
},
45+
"response": []
46+
},
47+
{
48+
"name": "Update",
49+
"request": {
50+
"method": "POST",
51+
"header": [],
52+
"body": {
53+
"mode": "raw",
54+
"raw": "{\n\t\"handle\": \"sdfsdF\"\n}"
55+
},
56+
"url": {
57+
"raw": "{{URL}}/test",
58+
"host": [
59+
"{{URL}}"
60+
],
61+
"path": [
62+
"test"
63+
]
64+
}
65+
},
66+
"response": []
67+
},
68+
{
69+
"name": "Delete",
70+
"request": {
71+
"method": "GET",
72+
"header": [],
73+
"url": {
74+
"raw": ""
75+
}
76+
},
77+
"response": []
78+
}
79+
]
80+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"id": "e203cccf-8653-4ac4-959f-2050faa65c1d",
3+
"name": "Swift Server",
4+
"values": [
5+
{
6+
"key": "URL",
7+
"value": "localhost:8080",
8+
"description": "",
9+
"enabled": true
10+
}
11+
],
12+
"_postman_variable_scope": "environment",
13+
"_postman_exported_at": "2019-10-12T17:36:25.362Z",
14+
"_postman_exported_using": "Postman/7.9.0"
15+
}

mysql/Dockerfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Create from official mysql image
2+
FROM mysql:5.7
3+
4+
# Add content of the sql-scripts directory to your image
5+
# All scripts in docker-entrypoint-initdb.d/ are automatically
6+
# executed during container startup
7+
ADD ./sql-scripts /docker-entrypoint-initdb.d/

mysql/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Create image
2+
```
3+
docker build -t database-test .
4+
```
5+
6+
# Start new MySQL container from the image
7+
```
8+
docker run -d -p 3336:3306 --name database-test1 -e MYSQL_ROOT_PASSWORD=myPassword database-test
9+
```
10+
11+
# Connect to container to verify what's inside
12+
```
13+
docker exec -it database-test1 bash
14+
```

mysql/sql-scripts/create.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE TABLE `Model` (
2+
`id` varchar(36) NOT NULL,
3+
`handle` longtext NOT NULL,
4+
PRIMARY KEY (`id`)
5+
) ENGINE=InnoDB DEFAULT CHARSET=utf8

server/Dockerfile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#An Ubuntu 16.04 image with Swift preinstalled
2+
FROM seriyvolk83/swift4.2.1
3+
4+
# Copy application
5+
COPY . /app
6+
7+
# Build application
8+
RUN swift --version
9+
RUN swift build -c release --package-path /app/SampleServer --build-path /app/SampleServer/.build
10+
11+
# Make port 8080 available to the world outside this container
12+
EXPOSE 8080
13+
14+
# Run the server when container is launched
15+
CMD ["/app/SampleServer/.build/release/SampleServer"]

server/README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
# 1. Clean previous bluild
3+
```
4+
cd SampleServer
5+
swift package clean
6+
rm -rf .build
7+
```
8+
9+
# 2. Build
10+
11+
Follow "development version" if you test the server locally, not as a part of container.
12+
13+
## Development version
14+
```
15+
cd SampleServer
16+
swift build
17+
```
18+
## Release version
19+
```
20+
cd SampleServer
21+
swift build -c release
22+
```
23+
## Build from current dir (the project is in SampleServer)
24+
```
25+
swift build --package-path SampleServer --build-path SampleServer/.build
26+
```
27+
28+
# 3. Xcode project
29+
30+
To generate Xcode project:
31+
```
32+
cd SampleServer
33+
swift package generate-xcodeproj
34+
```
35+
36+
# 4. Try to run it locally
37+
38+
## Run development version
39+
```
40+
.build/debug/SampleServer
41+
```
42+
## Run release version
43+
```
44+
.build/release/SampleServer
45+
```

server/SampleServer/Package.resolved

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

server/SampleServer/Package.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// swift-tools-version:4.2
2+
3+
import PackageDescription
4+
5+
let package = Package(
6+
name: "SampleServer",
7+
products: [
8+
.executable(name: "SampleServer", targets: ["SampleServer"])
9+
],
10+
dependencies: [
11+
.package(url: "https://github.com/PerfectlySoft/Perfect-HTTPServer.git", from: "3.0.0"),
12+
.package(url: "https://github.com/PerfectlySoft/Perfect-MySQL.git", from: "3.0.0"),
13+
.package(url: "https://github.com/SwiftyJSON/SwiftyJSON.git", from: "4.2.0"),
14+
// .package(url: "https://gitlab.com/seriyvolk83/SwiftEx.git", from: "0.0.7"), dodo
15+
],
16+
targets: [
17+
.target(name: "SampleServer", dependencies: ["PerfectHTTPServer", "PerfectMySQL", "SwiftyJSON"])
18+
]
19+
)

0 commit comments

Comments
 (0)