Skip to content

Commit 6697c43

Browse files
committed
chapter 16 updated
1 parent 31d3553 commit 6697c43

File tree

185 files changed

+2776
-2306
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

185 files changed

+2776
-2306
lines changed

Chapter 16/myProject/.gitignore

-9
This file was deleted.

Chapter 16/myProject/Dockerfile

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# ================================
2+
# Build image
3+
# ================================
4+
FROM swift:5.7-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)/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: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 ["./Run"]
77+
CMD ["serve", "--env", "production", "--hostname", "0.0.0.0", "--port", "8080"]

Chapter 16/myProject/Makefile

-7
This file was deleted.

Chapter 16/myProject/Package.swift

+31-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version:5.5
1+
// swift-tools-version:5.7
22
import PackageDescription
33

44
let package = Package(
@@ -10,13 +10,34 @@ let package = Package(
1010
.library(name: "AppApi", targets: ["AppApi"]),
1111
],
1212
dependencies: [
13-
.package(url: "https://github.com/vapor/vapor", from: "4.54.0"),
14-
.package(url: "https://github.com/vapor/fluent", from: "4.4.0"),
15-
.package(url: "https://github.com/vapor/fluent-sqlite-driver", from: "4.1.0"),
16-
.package(url: "https://github.com/binarybirds/liquid", from: "1.3.0"),
17-
.package(url: "https://github.com/binarybirds/liquid-local-driver", from: "1.3.0"),
18-
.package(url: "https://github.com/binarybirds/swift-html", from: "1.2.0"),
19-
.package(url: "https://github.com/binarybirds/spec", from: "1.2.0"),
13+
.package(
14+
url: "https://github.com/vapor/vapor",
15+
from: "4.70.0"
16+
),
17+
.package(
18+
url: "https://github.com/vapor/fluent",
19+
from: "4.4.0"
20+
),
21+
.package(
22+
url: "https://github.com/vapor/fluent-sqlite-driver",
23+
from: "4.1.0"
24+
),
25+
.package(
26+
url: "https://github.com/binarybirds/liquid",
27+
from: "1.3.0"
28+
),
29+
.package(
30+
url: "https://github.com/binarybirds/liquid-local-driver",
31+
from: "1.3.0"
32+
),
33+
.package(
34+
url: "https://github.com/binarybirds/swift-html",
35+
from: "1.7.0"
36+
),
37+
.package(
38+
url: "https://github.com/binarybirds/spec",
39+
from: "1.2.0"
40+
),
2041
],
2142
targets: [
2243
.target(name: "AppApi", dependencies: []),
@@ -25,10 +46,9 @@ let package = Package(
2546
.product(name: "Fluent", package: "fluent"),
2647
.product(name: "FluentSQLiteDriver", package: "fluent-sqlite-driver"),
2748
.product(name: "Liquid", package: "liquid"),
28-
.product(name: "LiquidLocalDriver", package: "liquid-local-driver"),
49+
.product(name: "LiquidLocalDriver", package: "liquid-local-driver"),
2950
.product(name: "SwiftHtml", package: "swift-html"),
3051
.product(name: "SwiftSvg", package: "swift-html"),
31-
3252
.target(name: "AppApi")
3353
]),
3454
.executableTarget(name: "Run", dependencies: ["App"]),
@@ -39,6 +59,6 @@ let package = Package(
3959
]),
4060
.testTarget(name: "AppApiTests", dependencies: [
4161
.target(name: "AppApi"),
42-
])
62+
]),
4363
]
4464
)
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
11
tr {
2-
grid-template-columns: 4rem 1fr 4rem;
32
column-gap: 1rem;
43
}
5-
td img {
6-
display: block;
7-
}
8-
th {
9-
text-align: left;
10-
}

Chapter 16/myProject/Public/js/admin.js

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ document.addEventListener("keydown", function(e) {
44
document.forms[0].submit();
55
}
66
}, false);
7+

Chapter 16/myProject/Sources/App/Extensions/Svg+MenuIcon.swift

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
1-
//
2-
// File.swift
3-
//
4-
//
5-
// Created by Tibor Bodecs on 2022. 01. 03..
6-
//
7-
81
import SwiftSvg
92

103
extension Svg {
4+
115
static func menuIcon() -> Svg {
126
Svg {
137
Line(x1: 3, y1: 12, x2: 21, y2: 12)
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
//
2-
// File.swift
3-
//
4-
//
5-
// Created by Tibor Bodecs on 2022. 01. 09..
6-
//
7-
81
import Vapor
2+
import AppApi
93

104
extension ApiModelInterface {
115

12-
static var pathIdComponent: PathComponent { .init(stringLiteral: ":" + pathIdKey) }
6+
static var pathIdComponent: PathComponent {
7+
.init(stringLiteral: ":" + pathIdKey)
8+
}
139
}
14-
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
1-
//
2-
// File.swift
3-
//
4-
//
5-
// Created by Tibor Bodecs on 2021. 12. 31..
6-
//
7-
81
import Vapor
92

103
public struct AuthenticatedUser {
4+
115
public let id: UUID
126
public let email: String
137

14-
public init(id: UUID, email: String) {
8+
public init(
9+
id: UUID,
10+
email: String
11+
) {
1512
self.id = id
1613
self.email = email
1714
}
@@ -20,4 +17,3 @@ public struct AuthenticatedUser {
2017
extension AuthenticatedUser: SessionAuthenticatable {
2118
public var sessionID: UUID { id }
2219
}
23-
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,42 @@
1-
//
2-
// File.swift
3-
//
4-
//
5-
// Created by Tibor Bodecs on 2022. 01. 06..
6-
//
7-
81
import Vapor
92

103
protocol CreateController: ModelController {
11-
func beforeCreate(_ req: Request, _ model: DatabaseModel) async throws
12-
func afterCreate(_ req: Request, _ model: DatabaseModel) async throws
134

14-
func create(_ req: Request, _ model: DatabaseModel) async throws
5+
func create(
6+
_ req: Request,
7+
_ model: DatabaseModel
8+
) async throws
9+
10+
func beforeCreate(
11+
_ req: Request,
12+
_ model: DatabaseModel
13+
) async throws
14+
15+
func afterCreate(
16+
_ req: Request,
17+
_ model: DatabaseModel
18+
) async throws
1519
}
1620

1721
extension CreateController {
1822

19-
func beforeCreate(_ req: Request, _ model: DatabaseModel) async throws {}
20-
21-
func afterCreate(_ req: Request, _ model: DatabaseModel) async throws {}
22-
23-
func create(_ req: Request, _ model: DatabaseModel) async throws {
23+
func create(
24+
_ req: Request,
25+
_ model: DatabaseModel
26+
) async throws {
2427
try await beforeCreate(req, model)
2528
try await model.create(on: req.db)
2629
try await afterCreate(req, model)
2730
}
28-
}
31+
32+
func beforeCreate(
33+
_ req: Request,
34+
_ model: DatabaseModel
35+
) async throws {}
36+
37+
func afterCreate(
38+
_ req: Request,
39+
_ model: DatabaseModel
40+
) async throws {}
2941

42+
}
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,41 @@
1-
//
2-
// File.swift
3-
//
4-
//
5-
// Created by Tibor Bodecs on 2022. 01. 06..
6-
//
7-
81
import Vapor
92

10-
protocol DeleteController: ModelController {
11-
func beforeDelete(_ req: Request, _ model: DatabaseModel) async throws
12-
func afterDelete(_ req: Request, _ model: DatabaseModel) async throws
13-
func delete(_ req: Request, _ model: DatabaseModel) async throws
14-
}
15-
16-
extension DeleteController {
3+
public protocol DeleteController: ModelController {
174

18-
func beforeDelete(_ req: Request, _ model: DatabaseModel) async throws {}
19-
func afterDelete(_ req: Request, _ model: DatabaseModel) async throws {}
5+
func delete(
6+
_ req: Request,
7+
_ model: DatabaseModel
8+
) async throws
209

21-
func delete(_ req: Request, _ model: DatabaseModel) async throws {
10+
func beforeDelete(
11+
_ req: Request,
12+
_ model: DatabaseModel
13+
) async throws
14+
15+
func afterDelete(
16+
_ req: Request,
17+
_ model: DatabaseModel
18+
) async throws
19+
}
20+
21+
public extension DeleteController {
22+
23+
func delete(
24+
_ req: Request,
25+
_ model: DatabaseModel
26+
) async throws {
2227
try await beforeDelete(req, model)
2328
try await model.delete(on: req.db)
2429
try await afterDelete(req, model)
2530
}
31+
32+
func beforeDelete(
33+
_ req: Request,
34+
_ model: DatabaseModel
35+
) async throws {}
36+
37+
func afterDelete(
38+
_ req: Request,
39+
_ model: DatabaseModel
40+
) async throws {}
2641
}

0 commit comments

Comments
 (0)