From e6132a38b3df9c86085a902caefb94c8f727e192 Mon Sep 17 00:00:00 2001 From: Christoph Hagen Date: Wed, 6 Dec 2023 09:39:12 +0100 Subject: [PATCH] Switch to new vapor main --- Package.swift | 24 ++++++++----------- Sources/App/configure.swift | 18 +++++---------- Sources/App/entrypoint.swift | 43 +++++++++++++++++++++++++++++++++++ Sources/Run/main.swift | 12 ---------- Tests/.gitkeep | 0 Tests/AppTests/AppTests.swift | 13 ----------- Tests/LinuxMain.swift | 0 7 files changed, 58 insertions(+), 52 deletions(-) create mode 100644 Sources/App/entrypoint.swift delete mode 100755 Sources/Run/main.swift delete mode 100755 Tests/.gitkeep delete mode 100755 Tests/AppTests/AppTests.swift delete mode 100755 Tests/LinuxMain.swift diff --git a/Package.swift b/Package.swift index 53a6fda..7f4908f 100755 --- a/Package.swift +++ b/Package.swift @@ -13,21 +13,15 @@ let package = Package( .package(url: "https://github.com/christophhagen/ClairvoyantBinaryCodable", from: "0.3.0"), ], targets: [ - .target(name: "App", - dependencies: [ - .product(name: "Vapor", package: "vapor"), - .product(name: "Clairvoyant", package: "Clairvoyant"), - .product(name: "ClairvoyantVapor", package: "ClairvoyantVapor"), - .product(name: "ClairvoyantBinaryCodable", package: "ClairvoyantBinaryCodable"), - ], - swiftSettings: [ - // Enable better optimizations when building in Release configuration. Despite the use of - // the `.unsafeFlags` construct required by SwiftPM, this flag is recommended for Release - // builds. See for details. - .unsafeFlags(["-cross-module-optimization"], .when(configuration: .release)) - ]), - .executableTarget(name: "Run", dependencies: ["App"]), - .testTarget(name: "AppTests", dependencies: ["App"]), + .executableTarget( + name: "App", + dependencies: [ + .product(name: "Vapor", package: "vapor"), + .product(name: "Clairvoyant", package: "Clairvoyant"), + .product(name: "ClairvoyantVapor", package: "ClairvoyantVapor"), + .product(name: "ClairvoyantBinaryCodable", package: "ClairvoyantBinaryCodable"), + ] + ) ] ) diff --git a/Sources/App/configure.swift b/Sources/App/configure.swift index e621a52..7d8c5eb 100755 --- a/Sources/App/configure.swift +++ b/Sources/App/configure.swift @@ -11,13 +11,7 @@ private let asyncScheduler = MultiThreadedEventLoopGroup(numberOfThreads: 2) private var server: CapServer! -private func status(_ newStatus: ServerStatus) { - asyncScheduler.schedule { - try await serverStatus.update(newStatus) - } -} - -public func configure(_ app: Application) throws { +func configure(_ app: Application) async throws { let resourceDirectory = URL(fileURLWithPath: app.directory.resourcesDirectory) let publicDirectory = app.directory.publicDirectory @@ -32,7 +26,7 @@ public func configure(_ app: Application) throws { serverStatus = Metric("caps.status", name: "Status", description: "The general status of the service") - status(.initializing) + try await serverStatus.update(.initializing) app.http.server.configuration.port = config.port app.routes.defaultMaxBodySize = .init(stringLiteral: config.maxBodySize) @@ -55,19 +49,19 @@ public func configure(_ app: Application) throws { do { try server.loadData() } catch { - status(.initializationFailure) + try await serverStatus.update(.initializationFailure) print("[\(df.string(from: Date()))] Server failed to start: \(error)") return } if server.canResizeImages { - status(.nominal) + try await serverStatus.update(.nominal) } else { - status(.reducedFunctionality) + try await serverStatus.update(.reducedFunctionality) } print("[\(df.string(from: Date()))] Server started (\(server.capCount) caps)") } -public func shutdown() { +func shutdown() { print("[\(df.string(from: Date()))] Server shutdown") asyncScheduler.schedule { do { diff --git a/Sources/App/entrypoint.swift b/Sources/App/entrypoint.swift new file mode 100644 index 0000000..d3885da --- /dev/null +++ b/Sources/App/entrypoint.swift @@ -0,0 +1,43 @@ +import Vapor +import Dispatch +import Logging + +/// This extension is temporary and can be removed once Vapor gets this support. +private extension Vapor.Application { + static let baseExecutionQueue = DispatchQueue(label: "vapor.codes.entrypoint") + + func runFromAsyncMainEntrypoint() async throws { + try await withCheckedThrowingContinuation { continuation in + Vapor.Application.baseExecutionQueue.async { [self] in + do { + try self.run() + continuation.resume() + } catch { + continuation.resume(throwing: error) + } + } + } + } +} + +@main +enum Entrypoint { + static func main() async throws { + var env = try Environment.detect() + try LoggingSystem.bootstrap(from: &env) + + let app = Application(env) + defer { + shutdown() + app.shutdown() + } + + do { + try await configure(app) + } catch { + app.logger.report(error: error) + throw error + } + try await app.runFromAsyncMainEntrypoint() + } +} diff --git a/Sources/Run/main.swift b/Sources/Run/main.swift deleted file mode 100755 index 9163d1e..0000000 --- a/Sources/Run/main.swift +++ /dev/null @@ -1,12 +0,0 @@ -import App -import Vapor - -var env = Environment.production -try LoggingSystem.bootstrap(from: &env) -let app = Application(env) -defer { - shutdown() - app.shutdown() -} -try configure(app) -try app.run() diff --git a/Tests/.gitkeep b/Tests/.gitkeep deleted file mode 100755 index e69de29..0000000 diff --git a/Tests/AppTests/AppTests.swift b/Tests/AppTests/AppTests.swift deleted file mode 100755 index cfce2e4..0000000 --- a/Tests/AppTests/AppTests.swift +++ /dev/null @@ -1,13 +0,0 @@ -import App -import Dispatch -import XCTest - -final class AppTests : XCTestCase { - func testNothing() throws { - XCTAssert(true) - } - - static let allTests = [ - ("testNothing", testNothing), - ] -} diff --git a/Tests/LinuxMain.swift b/Tests/LinuxMain.swift deleted file mode 100755 index e69de29..0000000