Skip to content

Commit e71f9df

Browse files
committed
first commit
0 parents  commit e71f9df

File tree

12 files changed

+206
-0
lines changed

12 files changed

+206
-0
lines changed

.dockerignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.build/
2+
.swiftpm/

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Packages
2+
.build
3+
xcuserdata
4+
*.xcodeproj
5+
DerivedData/
6+
.DS_Store
7+
db.sqlite
8+
.swiftpm
9+
.env

Dockerfile

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# ================================
2+
# Build image
3+
# ================================
4+
FROM swift:5.6-focal as build
5+
6+
# Install OS updates and, if needed, sqlite3
7+
RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true \
8+
&& apt-get -q update \
9+
&& apt-get -q dist-upgrade -y \
10+
&& rm -rf /var/lib/apt/lists/*
11+
12+
# Set up a build area
13+
WORKDIR /build
14+
15+
# First just resolve dependencies.
16+
# This creates a cached layer that can be reused
17+
# as long as your Package.swift/Package.resolved
18+
# files do not change.
19+
COPY ./Package.* ./
20+
RUN swift package resolve
21+
22+
# Copy entire repo into container
23+
COPY . .
24+
25+
# Build everything, with optimizations
26+
RUN swift build -c release --static-swift-stdlib
27+
28+
# Switch to the staging area
29+
WORKDIR /staging
30+
31+
# Copy main executable to staging area
32+
RUN cp "$(swift build --package-path /build -c release --show-bin-path)/Run" ./
33+
34+
# Copy resources bundled by SPM to staging area
35+
RUN find -L "$(swift build --package-path /build -c release --show-bin-path)/" -regex '.*\.resources$' -exec cp -Ra {} ./ \;
36+
37+
# Copy any resources from the public directory and views directory if the directories exist
38+
# Ensure that by default, neither the directory nor any of its contents are writable.
39+
RUN [ -d /build/Public ] && { mv /build/Public ./Public && chmod -R a-w ./Public; } || true
40+
RUN [ -d /build/Resources ] && { mv /build/Resources ./Resources && chmod -R a-w ./Resources; } || true
41+
42+
# ================================
43+
# Run image
44+
# ================================
45+
FROM ubuntu:focal
46+
47+
# Make sure all system packages are up to date.
48+
RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true && \
49+
apt-get -q update && apt-get -q dist-upgrade -y && apt-get -q install -y ca-certificates tzdata && \
50+
rm -r /var/lib/apt/lists/*
51+
52+
# Create a vapor user and group with /app as its home directory
53+
RUN useradd --user-group --create-home --system --skel /dev/null --home-dir /app vapor
54+
55+
# Switch to the new home directory
56+
WORKDIR /app
57+
58+
# Copy built executable and any staged resources from builder
59+
COPY --from=build --chown=vapor:vapor /staging /app
60+
61+
# Ensure all further commands run as the vapor user
62+
USER vapor:vapor
63+
64+
# Let Docker bind to port 8080
65+
EXPOSE 8080
66+
67+
# Start the Vapor service when the image is run, default to listening on 8080 in production environment
68+
ENTRYPOINT ["./Run"]
69+
CMD ["serve", "--env", "production", "--hostname", "0.0.0.0", "--port", "8080"]

Package.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// swift-tools-version:5.6
2+
import PackageDescription
3+
4+
let package = Package(
5+
name: "ParseServerSwift",
6+
platforms: [
7+
.macOS(.v12)
8+
],
9+
dependencies: [
10+
// 💧 A server-side Swift web framework.
11+
.package(url: "https://github.com/vapor/vapor.git", from: "4.0.0"),
12+
.package(url: "https://github.com/vapor/leaf.git", from: "4.0.0"),
13+
],
14+
targets: [
15+
.target(
16+
name: "App",
17+
dependencies: [
18+
.product(name: "Leaf", package: "leaf"),
19+
.product(name: "Vapor", package: "vapor")
20+
],
21+
swiftSettings: [
22+
// Enable better optimizations when building in Release configuration. Despite the use of
23+
// the `.unsafeFlags` construct required by SwiftPM, this flag is recommended for Release
24+
// builds. See <https://github.com/swift-server/guides/blob/main/docs/building.md#building-for-production> for details.
25+
.unsafeFlags(["-cross-module-optimization"], .when(configuration: .release))
26+
]
27+
),
28+
.executableTarget(name: "Run", dependencies: [.target(name: "App")]),
29+
.testTarget(name: "AppTests", dependencies: [
30+
.target(name: "App"),
31+
.product(name: "XCTVapor", package: "vapor"),
32+
])
33+
]
34+
)

Public/.gitkeep

Whitespace-only changes.

Resources/Views/index.leaf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
6+
<title>#(title)</title>
7+
</head>
8+
9+
<body>
10+
<h1>#(title)</h1>
11+
</body>
12+
</html>

Sources/App/Controllers/.gitkeep

Whitespace-only changes.

Sources/App/configure.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import Leaf
2+
import Vapor
3+
4+
// configures your application
5+
public func configure(_ app: Application) throws {
6+
// uncomment to serve files from /Public folder
7+
// app.middleware.use(FileMiddleware(publicDirectory: app.directory.publicDirectory))
8+
9+
app.views.use(.leaf)
10+
11+
12+
13+
// register routes
14+
try routes(app)
15+
}

Sources/App/routes.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import Vapor
2+
3+
func routes(_ app: Application) throws {
4+
app.get { req in
5+
return req.view.render("index", ["title": "Hello Vapor!"])
6+
}
7+
8+
app.get("hello") { req -> String in
9+
return "Hello, world!"
10+
}
11+
}

Sources/Run/main.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import App
2+
import Vapor
3+
4+
var env = try Environment.detect()
5+
try LoggingSystem.bootstrap(from: &env)
6+
let app = Application(env)
7+
defer { app.shutdown() }
8+
try configure(app)
9+
try app.run()

Tests/AppTests/AppTests.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
@testable import App
2+
import XCTVapor
3+
4+
final class AppTests: XCTestCase {
5+
func testHelloWorld() throws {
6+
let app = Application(.testing)
7+
defer { app.shutdown() }
8+
try configure(app)
9+
10+
try app.test(.GET, "hello", afterResponse: { res in
11+
XCTAssertEqual(res.status, .ok)
12+
XCTAssertEqual(res.body.string, "Hello, world!")
13+
})
14+
}
15+
}

docker-compose.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Docker Compose file for Vapor
2+
#
3+
# Install Docker on your system to run and test
4+
# your Vapor app in a production-like environment.
5+
#
6+
# Note: This file is intended for testing and does not
7+
# implement best practices for a production deployment.
8+
#
9+
# Learn more: https://docs.docker.com/compose/reference/
10+
#
11+
# Build images: docker-compose build
12+
# Start app: docker-compose up app
13+
# Stop all: docker-compose down
14+
#
15+
version: '3.7'
16+
17+
x-shared_environment: &shared_environment
18+
LOG_LEVEL: ${LOG_LEVEL:-debug}
19+
20+
services:
21+
app:
22+
image: parse-server-swift:latest
23+
build:
24+
context: .
25+
environment:
26+
<<: *shared_environment
27+
ports:
28+
- '8080:8080'
29+
# user: '0' # uncomment to run as root for testing purposes even though Dockerfile defines 'vapor' user.
30+
command: ["serve", "--env", "production", "--hostname", "0.0.0.0", "--port", "8080"]

0 commit comments

Comments
 (0)