Skip to content

Commit 65c3217

Browse files
committed
chapter 05 updated
1 parent 15c32a2 commit 65c3217

Some content is hidden

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

48 files changed

+506
-498
lines changed

Chapter 05/myProject/.gitignore

Lines changed: 0 additions & 9 deletions
This file was deleted.

Chapter 05/myProject/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.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 05/myProject/Makefile

Lines changed: 0 additions & 7 deletions
This file was deleted.

Chapter 05/myProject/Package.swift

Lines changed: 17 additions & 5 deletions
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(
@@ -7,10 +7,22 @@ let package = Package(
77
.macOS(.v12)
88
],
99
dependencies: [
10-
.package(url: "https://github.com/vapor/vapor", from: "4.54.0"),
11-
.package(url: "https://github.com/vapor/fluent", from: "4.4.0"),
12-
.package(url: "https://github.com/vapor/fluent-sqlite-driver", from: "4.1.0"),
13-
.package(url: "https://github.com/binarybirds/swift-html", from: "1.2.0"),
10+
.package(
11+
url: "https://github.com/vapor/vapor",
12+
from: "4.70.0"
13+
),
14+
.package(
15+
url: "https://github.com/vapor/fluent",
16+
from: "4.4.0"
17+
),
18+
.package(
19+
url: "https://github.com/vapor/fluent-sqlite-driver",
20+
from: "4.1.0"
21+
),
22+
.package(
23+
url: "https://github.com/binarybirds/swift-html",
24+
from: "1.2.0"
25+
),
1426
],
1527
targets: [
1628
.target(name: "App", dependencies: [

Chapter 05/myProject/Sources/App/Controllers/.gitkeep

Whitespace-only changes.
Lines changed: 5 additions & 9 deletions
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-
Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
1-
//
2-
// File.swift
3-
//
4-
//
5-
// Created by Tibor Bodecs on 2021. 12. 31..
6-
//
7-
81
import Vapor
92
import Fluent
103

11-
public protocol DatabaseModelInterface: Fluent.Model where Self.IDValue == UUID {
12-
4+
public protocol DatabaseModelInterface: Fluent.Model
5+
where Self.IDValue == UUID
6+
{
137
associatedtype Module: ModuleInterface
148

159
static var identifier: String { get }
1610
}
1711

1812
public extension DatabaseModelInterface {
1913

20-
static var schema: String { Module.identifier + "_" + identifier }
14+
static var schema: String {
15+
Module.identifier + "_" + identifier
16+
}
2117

2218
static var identifier: String {
23-
String(describing: self).dropFirst(Module.identifier.count).dropLast(5).lowercased() + "s"
19+
String(describing: self)
20+
.dropFirst(Module.identifier.count)
21+
.dropLast(5)
22+
.lowercased() + "s"
2423
}
2524
}
Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
//
2-
// File.swift
3-
//
4-
//
5-
// Created by Tibor Bodecs on 2021. 12. 31..
6-
//
7-
81
import Vapor
92

103
public protocol ModuleInterface {
@@ -15,8 +8,10 @@ public protocol ModuleInterface {
158
}
169

1710
public extension ModuleInterface {
11+
1812
func boot(_ app: Application) throws {}
1913

20-
static var identifier: String { String(describing: self).dropLast(6).lowercased() }
21-
14+
static var identifier: String {
15+
String(describing: self).dropLast(6).lowercased()
16+
}
2217
}
Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
//
2-
// File.swift
3-
//
4-
//
5-
// Created by Tibor Bodecs on 2021. 12. 25..
6-
//
7-
81
import Vapor
92

103
struct ExtendPathMiddleware: AsyncMiddleware {
114

12-
func respond(to req: Request, chainingTo next: AsyncResponder) async throws -> Response {
5+
func respond(
6+
to req: Request,
7+
chainingTo next: AsyncResponder
8+
) async throws -> Response {
139
if !req.url.path.hasSuffix("/") && !req.url.path.contains(".") {
14-
return req.redirect(to: req.url.path + "/", type: .permanent)
10+
return req.redirect(
11+
to: req.url.path + "/",
12+
redirectType: .permanent
13+
)
1514
}
16-
return try await next.respond(to: req)
15+
return try await next.respond(
16+
to: req
17+
)
1718
}
1819
}

Chapter 05/myProject/Sources/App/Modules/Blog/BlogModule.swift

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
//
2-
// File.swift
3-
//
4-
//
5-
// Created by Tibor Bodecs on 2021. 12. 31..
6-
//
7-
81
import Vapor
92

103
struct BlogModule: ModuleInterface {

Chapter 05/myProject/Sources/App/Modules/Blog/BlogRouter.swift

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
1-
//
2-
// BlogRouter.swift
3-
//
4-
//
5-
// Created by Tibor Bodecs on 2021. 12. 25..
6-
//
7-
81
import Vapor
92

103
struct BlogRouter: RouteCollection {
114

125
let controller = BlogFrontendController()
136

14-
func boot(routes: RoutesBuilder) throws {
7+
func boot(
8+
routes: RoutesBuilder
9+
) throws {
1510
routes.get("blog", use: controller.blogView)
1611
routes.get(.anything, use: controller.postView)
1712
}
Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
//
2-
// BlogFrontendController.swift
3-
//
4-
//
5-
// Created by Tibor Bodecs on 2021. 12. 25..
6-
//
7-
81
import Vapor
92
import Fluent
103

@@ -16,22 +9,32 @@ struct BlogFrontendController {
169
.sort(\.$date, .descending)
1710
.all()
1811

19-
let posts = postModels.map { Blog.Post.List(id: $0.id!,
20-
title: $0.title,
21-
slug: $0.slug,
22-
image: $0.imageKey,
23-
excerpt: $0.excerpt,
24-
date: $0.date)
12+
let posts = try postModels.map {
13+
Blog.Post.List(
14+
id: try $0.requireID(),
15+
title: $0.title,
16+
slug: $0.slug,
17+
image: $0.imageKey,
18+
excerpt: $0.excerpt,
19+
date: $0.date
20+
)
2521
}
26-
let ctx = BlogPostsContext(icon: "🔥",
27-
title: "Blog",
28-
message: "Hot news and stories about everything.",
29-
posts: posts)
30-
return req.templates.renderHtml(BlogPostsTemplate(ctx))
22+
23+
let ctx = BlogPostsContext(
24+
icon: "🔥",
25+
title: "Blog",
26+
message: "Hot news and stories about everything.",
27+
posts: posts)
28+
29+
return req.templates.renderHtml(
30+
BlogPostsTemplate(ctx)
31+
)
3132
}
3233

3334
func postView(req: Request) async throws -> Response {
34-
let slug = req.url.path.trimmingCharacters(in: .init(charactersIn: "/"))
35+
let slug = req.url.path.trimmingCharacters(
36+
in: .init(charactersIn: "/")
37+
)
3538
guard
3639
let post = try await BlogPostModel
3740
.query(on: req.db)
@@ -41,15 +44,24 @@ struct BlogFrontendController {
4144
else {
4245
return req.redirect(to: "/")
4346
}
44-
let ctx = BlogPostContext(post: Blog.Post.Detail(id: post.id!,
45-
title: post.title,
46-
slug: post.slug,
47-
image: post.imageKey,
48-
excerpt: post.excerpt,
49-
date: post.date,
50-
category: .init(id: post.category.id!,
51-
title: post.category.title),
52-
content: post.content))
53-
return req.templates.renderHtml(BlogPostTemplate(ctx))
47+
let ctx = BlogPostContext(
48+
post: Blog.Post.Detail(
49+
id: post.id!,
50+
title: post.title,
51+
slug: post.slug,
52+
image: post.imageKey,
53+
excerpt: post.excerpt,
54+
date: post.date,
55+
category: .init(
56+
id: post.category.id!,
57+
title: post.category.title
58+
),
59+
content: post.content
60+
)
61+
)
62+
63+
return req.templates.renderHtml(
64+
BlogPostTemplate(ctx)
65+
)
5466
}
5567
}

0 commit comments

Comments
 (0)