Skip to content

Commit e304be9

Browse files
committed
Vapor hello world
1 parent 1bd23f7 commit e304be9

File tree

10 files changed

+183
-0
lines changed

10 files changed

+183
-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+

Dockerfile

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# ================================
2+
# Build image
3+
# ================================
4+
FROM swift:5.3-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 and test discovery
26+
RUN swift build --enable-test-discovery -c release
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 any resouces from the public directory and views directory if the directories exist
35+
# Ensure that by default, neither the directory nor any of its contents are writable.
36+
RUN [ -d /build/Public ] && { mv /build/Public ./Public && chmod -R a-w ./Public; } || true
37+
RUN [ -d /build/Resources ] && { mv /build/Resources ./Resources && chmod -R a-w ./Resources; } || true
38+
39+
# ================================
40+
# Run image
41+
# ================================
42+
FROM swift:5.3-focal-slim
43+
44+
# Make sure all system packages are up to date.
45+
RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true && \
46+
apt-get -q update && apt-get -q dist-upgrade -y && rm -r /var/lib/apt/lists/*
47+
48+
# Create a vapor user and group with /app as its home directory
49+
RUN useradd --user-group --create-home --system --skel /dev/null --home-dir /app vapor
50+
51+
# Switch to the new home directory
52+
WORKDIR /app
53+
54+
# Copy built executable and any staged resources from builder
55+
COPY --from=build --chown=vapor:vapor /staging /app
56+
57+
# Ensure all further commands run as the vapor user
58+
USER vapor:vapor
59+
60+
# Let Docker bind to port 8080
61+
EXPOSE 8080
62+
63+
# Start the Vapor service when the image is run, default to listening on 8080 in production environment
64+
ENTRYPOINT ["./Run"]
65+
CMD ["serve", "--env", "production", "--hostname", "0.0.0.0", "--port", "8080"]

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

Sources/App/Controllers/.gitkeep

Whitespace-only changes.

Sources/App/configure.swift

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

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 "It works!"
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: regexplanet-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)