Skip to content

Commit 00eb829

Browse files
first commit
0 parents  commit 00eb829

File tree

11 files changed

+225
-0
lines changed

11 files changed

+225
-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: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# ================================
2+
# Build image
3+
# ================================
4+
FROM swift:5.8-jammy 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)/App" ./
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:jammy
46+
47+
# Make sure all system packages are up to date, and install only essential packages.
48+
RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true \
49+
&& apt-get -q update \
50+
&& apt-get -q dist-upgrade -y \
51+
&& apt-get -q install -y \
52+
ca-certificates \
53+
tzdata \
54+
# If your app or its dependencies import FoundationNetworking, also install `libcurl4`.
55+
# libcurl4 \
56+
# If your app or its dependencies import FoundationXML, also install `libxml2`.
57+
# libxml2 \
58+
&& rm -r /var/lib/apt/lists/*
59+
60+
# Create a vapor user and group with /app as its home directory
61+
RUN useradd --user-group --create-home --system --skel /dev/null --home-dir /app vapor
62+
63+
# Switch to the new home directory
64+
WORKDIR /app
65+
66+
# Copy built executable and any staged resources from builder
67+
COPY --from=build --chown=vapor:vapor /staging /app
68+
69+
# Ensure all further commands run as the vapor user
70+
USER vapor:vapor
71+
72+
# Let Docker bind to port 8080
73+
EXPOSE 8080
74+
75+
# Start the Vapor service when the image is run, default to listening on 8080 in production environment
76+
ENTRYPOINT ["./App"]
77+
CMD ["serve", "--env", "production", "--hostname", "0.0.0.0", "--port", "8080"]

Package.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// swift-tools-version:5.8
2+
import PackageDescription
3+
4+
let package = Package(
5+
name: "install-helper",
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.76.0"),
12+
],
13+
targets: [
14+
.executableTarget(
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://www.swift.org/server/guides/building.html#building-for-production> for details.
23+
.unsafeFlags(["-cross-module-optimization"], .when(configuration: .release))
24+
]
25+
),
26+
.testTarget(name: "AppTests", dependencies: [
27+
.target(name: "App"),
28+
.product(name: "XCTVapor", package: "vapor"),
29+
])
30+
]
31+
)

Public/.gitkeep

Whitespace-only changes.

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) async 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/entrypoint.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import Vapor
2+
import Dispatch
3+
import Logging
4+
5+
/// This extension is temporary and can be removed once Vapor gets this support.
6+
private extension Vapor.Application {
7+
static let baseExecutionQueue = DispatchQueue(label: "vapor.codes.entrypoint")
8+
9+
func runFromAsyncMainEntrypoint() async throws {
10+
try await withCheckedThrowingContinuation { continuation in
11+
Vapor.Application.baseExecutionQueue.async { [self] in
12+
do {
13+
try self.run()
14+
continuation.resume()
15+
} catch {
16+
continuation.resume(throwing: error)
17+
}
18+
}
19+
}
20+
}
21+
}
22+
23+
@main
24+
enum Entrypoint {
25+
static func main() async throws {
26+
var env = try Environment.detect()
27+
try LoggingSystem.bootstrap(from: &env)
28+
29+
let app = Application(env)
30+
defer { app.shutdown() }
31+
32+
do {
33+
try await configure(app)
34+
} catch {
35+
app.logger.report(error: error)
36+
throw error
37+
}
38+
try await app.runFromAsyncMainEntrypoint()
39+
}
40+
}

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 async in
5+
"It works!"
6+
}
7+
8+
app.get("hello") { req async -> String in
9+
"Hello, world!"
10+
}
11+
}

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() async throws {
6+
let app = Application(.testing)
7+
defer { app.shutdown() }
8+
try await 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: install-helper: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)