Skip to content

Commit 7a1f875

Browse files
authored
Merge pull request #6 from boostcampwm-2024/feature/#5-migrate-server
서버 스펙 변경 Node.js -> Vapor
2 parents caefc57 + 9fe8a8f commit 7a1f875

File tree

12 files changed

+476
-0
lines changed

12 files changed

+476
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.build/
2+
.swiftpm/
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Packages
2+
.build
3+
xcuserdata
4+
*.xcodeproj
5+
DerivedData/
6+
.DS_Store
7+
db.sqlite
8+
.swiftpm
9+
.env
10+
.env.*
11+
! .env.example
12+
.vscode
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# ================================
2+
# Build image
3+
# ================================
4+
FROM swift:6.0-jammy AS build
5+
6+
# Install OS updates
7+
RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true \
8+
&& apt-get -q update \
9+
&& apt-get -q dist-upgrade -y \
10+
&& apt-get install -y libjemalloc-dev
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+
$([ -f ./Package.resolved ] && echo "--force-resolved-versions" || true)
22+
23+
# Copy entire repo into container
24+
COPY . .
25+
26+
# Build everything, with optimizations, with static linking, and using jemalloc
27+
# N.B.: The static version of jemalloc is incompatible with the static Swift runtime.
28+
RUN swift build -c release \
29+
--static-swift-stdlib \
30+
-Xlinker -ljemalloc
31+
32+
# Switch to the staging area
33+
WORKDIR /staging
34+
35+
# Copy main executable to staging area
36+
RUN cp "$(swift build --package-path /build -c release --show-bin-path)/App" ./
37+
38+
# Copy static swift backtracer binary to staging area
39+
RUN cp "/usr/libexec/swift/linux/swift-backtrace-static" ./
40+
41+
# Copy resources bundled by SPM to staging area
42+
RUN find -L "$(swift build --package-path /build -c release --show-bin-path)/" -regex '.*\.resources$' -exec cp -Ra {} ./ \;
43+
44+
# Copy any resources from the public directory and views directory if the directories exist
45+
# Ensure that by default, neither the directory nor any of its contents are writable.
46+
RUN [ -d /build/Public ] && { mv /build/Public ./Public && chmod -R a-w ./Public; } || true
47+
RUN [ -d /build/Resources ] && { mv /build/Resources ./Resources && chmod -R a-w ./Resources; } || true
48+
49+
# ================================
50+
# Run image
51+
# ================================
52+
FROM ubuntu:jammy
53+
54+
# Make sure all system packages are up to date, and install only essential packages.
55+
RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true \
56+
&& apt-get -q update \
57+
&& apt-get -q dist-upgrade -y \
58+
&& apt-get -q install -y \
59+
libjemalloc2 \
60+
ca-certificates \
61+
tzdata \
62+
# If your app or its dependencies import FoundationNetworking, also install `libcurl4`.
63+
# libcurl4 \
64+
# If your app or its dependencies import FoundationXML, also install `libxml2`.
65+
# libxml2 \
66+
&& rm -r /var/lib/apt/lists/*
67+
68+
# Create a vapor user and group with /app as its home directory
69+
RUN useradd --user-group --create-home --system --skel /dev/null --home-dir /app vapor
70+
71+
# Switch to the new home directory
72+
WORKDIR /app
73+
74+
# Copy built executable and any staged resources from builder
75+
COPY --from=build --chown=vapor:vapor /staging /app
76+
77+
# Provide configuration needed by the built-in crash reporter and some sensible default behaviors.
78+
ENV SWIFT_BACKTRACE=enable=yes,sanitize=yes,threads=all,images=all,interactive=no,swift-backtrace=./swift-backtrace-static
79+
80+
# Ensure all further commands run as the vapor user
81+
USER vapor:vapor
82+
83+
# Let Docker bind to port 8080
84+
EXPOSE 8080
85+
86+
# Start the Vapor service when the image is run, default to listening on 8080 in production environment
87+
ENTRYPOINT ["./App"]
88+
CMD ["serve", "--env", "production", "--hostname", "0.0.0.0", "--port", "8080"]

PhotoGetherServer/PhotoGetherServer/Package.resolved

Lines changed: 204 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// swift-tools-version:6.0
2+
import PackageDescription
3+
4+
let package = Package(
5+
name: "PhotoGetherServer",
6+
platforms: [
7+
.macOS(.v13), .iOS(.v16)
8+
],
9+
dependencies: [
10+
// 💧 A server-side Swift web framework.
11+
.package(url: "https://github.com/vapor/vapor.git", from: "4.99.3"),
12+
// 🔵 Non-blocking, event-driven networking for Swift. Used for custom executors
13+
.package(url: "https://github.com/apple/swift-nio.git", from: "2.65.0"),
14+
],
15+
targets: [
16+
.executableTarget(
17+
name: "App",
18+
dependencies: [
19+
.product(name: "Vapor", package: "vapor"),
20+
.product(name: "NIOCore", package: "swift-nio"),
21+
.product(name: "NIOPosix", package: "swift-nio"),
22+
],
23+
swiftSettings: swiftSettings
24+
),
25+
.testTarget(
26+
name: "AppTests",
27+
dependencies: [
28+
.target(name: "App"),
29+
.product(name: "XCTVapor", package: "vapor"),
30+
],
31+
swiftSettings: swiftSettings
32+
)
33+
],
34+
swiftLanguageModes: [.v5]
35+
)
36+
37+
var swiftSettings: [SwiftSetting] { [
38+
.enableUpcomingFeature("DisableOutwardActorInference"),
39+
.enableExperimentalFeature("StrictConcurrency"),
40+
] }

PhotoGetherServer/PhotoGetherServer/Public/.gitkeep

Whitespace-only changes.

PhotoGetherServer/PhotoGetherServer/Sources/App/Controllers/.gitkeep

Whitespace-only changes.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
// register routes
8+
app.http.server.configuration.hostname = "0.0.0.0"
9+
app.http.server.configuration.port = 8080
10+
try routes(app)
11+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import Vapor
2+
import Logging
3+
import NIOCore
4+
import NIOPosix
5+
6+
@main
7+
enum Entrypoint {
8+
static func main() async throws {
9+
var env = try Environment.detect()
10+
try LoggingSystem.bootstrap(from: &env)
11+
12+
let app = try await Application.make(env)
13+
14+
// This attempts to install NIO as the Swift Concurrency global executor.
15+
// You can enable it if you'd like to reduce the amount of context switching between NIO and Swift Concurrency.
16+
// Note: this has caused issues with some libraries that use `.wait()` and cleanly shutting down.
17+
// If enabled, you should be careful about calling async functions before this point as it can cause assertion failures.
18+
// let executorTakeoverSuccess = NIOSingletons.unsafeTryInstallSingletonPosixEventLoopGroupAsConcurrencyGlobalExecutor()
19+
// app.logger.debug("Tried to install SwiftNIO's EventLoopGroup as Swift's global concurrency executor", metadata: ["success": .stringConvertible(executorTakeoverSuccess)])
20+
21+
do {
22+
try await configure(app)
23+
} catch {
24+
app.logger.report(error: error)
25+
try? await app.asyncShutdown()
26+
throw error
27+
}
28+
try await app.execute()
29+
try await app.asyncShutdown()
30+
}
31+
}

0 commit comments

Comments
 (0)