Skip to content

Commit 948d79c

Browse files
Merge pull request #440 from SwiftFiddle/reformat
Reformat (indent => 2)
2 parents 886751f + e37c170 commit 948d79c

File tree

12 files changed

+1716
-1730
lines changed

12 files changed

+1716
-1730
lines changed
Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,49 @@
11
import Vapor
22

33
final class CommonErrorMiddleware: Middleware {
4-
func respond(to request: Request, chainingTo next: Responder) -> EventLoopFuture<Response> {
5-
return next.respond(to: request).flatMapError { (error) in
6-
let headers: HTTPHeaders
7-
let status: HTTPResponseStatus
8-
switch error {
9-
case let abort as AbortError:
10-
headers = abort.headers
11-
status = abort.status
12-
default:
13-
headers = [:]
14-
status = .internalServerError
15-
}
4+
func respond(to request: Request, chainingTo next: Responder) -> EventLoopFuture<Response> {
5+
return next.respond(to: request).flatMapError { (error) in
6+
let headers: HTTPHeaders
7+
let status: HTTPResponseStatus
8+
switch error {
9+
case let abort as AbortError:
10+
headers = abort.headers
11+
status = abort.status
12+
default:
13+
headers = [:]
14+
status = .internalServerError
15+
}
1616

17-
let errotTitles: [UInt: String] = [
18-
400: "Bad Request",
19-
401: "Unauthorized",
20-
403: "Access Denied",
21-
404: "Resource not found",
22-
500: "Webservice currently unavailable",
23-
503: "Webservice currently unavailable",
24-
]
17+
let errotTitles: [UInt: String] = [
18+
400: "Bad Request",
19+
401: "Unauthorized",
20+
403: "Access Denied",
21+
404: "Resource not found",
22+
500: "Webservice currently unavailable",
23+
503: "Webservice currently unavailable",
24+
]
2525

26-
let errotReasons: [UInt: String] = [
27-
400: "The server cannot process the request due to something that is perceived to be a client error.",
28-
401: "The requested resource requires an authentication.",
29-
403: "The requested resource requires an authentication.",
30-
404: "The requested resource could not be found but may be available again in the future.",
31-
500: "An unexpected condition was encountered. Our service team has been dispatched to bring it back online.",
32-
503: "We&#39;ve got some trouble with our backend upstream cluster. Our service team has been dispatched to bring it back online.",
33-
]
26+
let errotReasons: [UInt: String] = [
27+
400: "The server cannot process the request due to something that is perceived to be a client error.",
28+
401: "The requested resource requires an authentication.",
29+
403: "The requested resource requires an authentication.",
30+
404: "The requested resource could not be found but may be available again in the future.",
31+
500: "An unexpected condition was encountered. Our service team has been dispatched to bring it back online.",
32+
503: "We&#39;ve got some trouble with our backend upstream cluster. Our service team has been dispatched to bring it back online.",
33+
]
3434

35-
if request.headers[.accept].map({ $0.lowercased() }).contains("application/json") {
36-
return request.eventLoop.makeSucceededFuture(["error": status.code])
37-
.encodeResponse(status: status, headers: headers, for: request)
38-
} else {
39-
return request.view.render("error", [
40-
"title": "We've got some trouble",
41-
"error": errotTitles[status.code],
42-
"reason": errotReasons[status.code],
43-
"status": "\(status.code)",
44-
])
45-
.encodeResponse(status: status, headers: headers, for: request)
46-
}
47-
}
35+
if request.headers[.accept].map({ $0.lowercased() }).contains("application/json") {
36+
return request.eventLoop.makeSucceededFuture(["error": status.code])
37+
.encodeResponse(status: status, headers: headers, for: request)
38+
} else {
39+
return request.view.render("error", [
40+
"title": "We've got some trouble",
41+
"error": errotTitles[status.code],
42+
"reason": errotReasons[status.code],
43+
"status": "\(status.code)",
44+
])
45+
.encodeResponse(status: status, headers: headers, for: request)
46+
}
4847
}
48+
}
4949
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import Vapor
22

33
final class CustomHeaderMiddleware: Middleware {
4-
func respond(to request: Request, chainingTo next: Responder) -> EventLoopFuture<Response> {
5-
return next.respond(to: request).map { (response) in
6-
response.headers.add(name: "X-Frame-Options", value: "DENY")
7-
response.headers.add(name: "Permissions-Policy", value: "interest-cohort=()")
8-
return response
9-
}
4+
func respond(to request: Request, chainingTo next: Responder) -> EventLoopFuture<Response> {
5+
return next.respond(to: request).map { (response) in
6+
response.headers.add(name: "X-Frame-Options", value: "DENY")
7+
response.headers.add(name: "Permissions-Policy", value: "interest-cohort=()")
8+
return response
109
}
10+
}
1111
}

Sources/App/Models/ResultResponse.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Foundation
22
import Vapor
33

44
struct ResultResponse: Content {
5-
let method: RequestMethod
6-
let result: String
7-
let error: String
5+
let method: RequestMethod
6+
let result: String
7+
let error: String
88
}

Sources/App/configure.swift

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
import Vapor
22
import Leaf
33

4-
public func configure(_ app: Application) throws {
5-
app.middleware = Middlewares()
6-
app.middleware.use(CommonErrorMiddleware())
7-
app.middleware.use(CustomHeaderMiddleware())
4+
public func configure(_ app: Application) async throws {
5+
app.middleware = Middlewares()
6+
app.middleware.use(CommonErrorMiddleware())
7+
app.middleware.use(CustomHeaderMiddleware())
88

9-
let publicDirectory = "\(app.directory.publicDirectory)/dist"
10-
app.middleware.use(FileMiddleware(publicDirectory: publicDirectory))
11-
12-
app.http.server.configuration.port = Environment.process.PORT.flatMap { Int($0) } ?? 8080
13-
app.http.server.configuration.requestDecompression = .enabled
14-
app.http.server.configuration.responseCompression = .enabled
15-
app.http.server.configuration.supportPipelining = true
9+
let publicDirectory = "\(app.directory.publicDirectory)/dist"
10+
app.middleware.use(FileMiddleware(publicDirectory: publicDirectory))
1611

17-
app.views.use(.leaf)
18-
app.leaf.configuration.rootDirectory = publicDirectory
19-
app.leaf.cache.isEnabled = app.environment.isRelease
20-
21-
try routes(app)
12+
app.http.server.configuration.port = Environment.process.PORT.flatMap { Int($0) } ?? 8080
13+
app.http.server.configuration.requestDecompression = .enabled
14+
app.http.server.configuration.responseCompression = .enabled
15+
app.http.server.configuration.supportPipelining = true
16+
17+
app.views.use(.leaf)
18+
app.leaf.configuration.rootDirectory = publicDirectory
19+
app.leaf.cache.isEnabled = app.environment.isRelease
20+
21+
try routes(app)
2222
}

Sources/App/entrypoint.swift

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,21 @@
11
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-
}
222

233
@main
244
enum Entrypoint {
255
static func main() async throws {
266
var env = try Environment.detect()
277
try LoggingSystem.bootstrap(from: &env)
288

29-
let app = Application(env)
30-
defer { app.shutdown() }
9+
let app = try await Application.make(env)
3110

32-
try await configure(app)
33-
try await app.runFromAsyncMainEntrypoint()
11+
do {
12+
try await configure(app)
13+
try await app.execute()
14+
} catch {
15+
app.logger.report(error: error)
16+
try? await app.asyncShutdown()
17+
throw error
18+
}
19+
try await app.asyncShutdown()
3420
}
3521
}

0 commit comments

Comments
 (0)