From b2b3c7458686e61595fd5877ceaa2b00330f5efe Mon Sep 17 00:00:00 2001 From: Christoph Hagen Date: Wed, 6 Dec 2023 09:49:26 +0100 Subject: [PATCH] Move to new Vapor main --- Package.swift | 15 ++---------- Sources/App/configure.swift | 20 ++++------------ Sources/App/entrypoint.swift | 43 +++++++++++++++++++++++++++++++++++ Sources/Run/main.swift | 13 ----------- Tests/AppTests/AppTests.swift | 13 ----------- 5 files changed, 49 insertions(+), 55 deletions(-) create mode 100644 Sources/App/entrypoint.swift delete mode 100644 Sources/Run/main.swift delete mode 100644 Tests/AppTests/AppTests.swift diff --git a/Package.swift b/Package.swift index 54f2d9d..2028e1b 100644 --- a/Package.swift +++ b/Package.swift @@ -13,25 +13,14 @@ let package = Package( .package(url: "https://github.com/christophhagen/ClairvoyantBinaryCodable", from: "0.3.1"), ], targets: [ - .target( + .executableTarget( 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: [.target(name: "App")]), - .testTarget(name: "AppTests", dependencies: [ - .target(name: "App"), - .product(name: "XCTVapor", package: "vapor"), - ]) + ) ] ) diff --git a/Sources/App/configure.swift b/Sources/App/configure.swift index d0aeae5..426c0bd 100755 --- a/Sources/App/configure.swift +++ b/Sources/App/configure.swift @@ -22,18 +22,8 @@ enum ServerError: Error { case invalidAuthenticationToken } -private func updateStatus(_ newStatus: ServerStatus) { - asyncScheduler.schedule { - do { - try await status.update(newStatus) - } catch { - print("Failed to update server status: \(error)") - } - } -} - // configures your application -public func configure(_ app: Application) throws { +public func configure(_ app: Application) async throws { let storageFolder = URL(fileURLWithPath: app.directory.resourcesDirectory) let configUrl = storageFolder.appendingPathComponent("config.json") @@ -45,7 +35,7 @@ public func configure(_ app: Application) throws { MetricObserver.standard = monitor status = Metric("sesame.status") - updateStatus(.initializing) + try await status.update(.initializing) app.http.server.configuration.port = config.port @@ -61,12 +51,10 @@ public func configure(_ app: Application) throws { provider.registerRoutes(app) monitor.saveCurrentListOfMetricsToLogFolder() - updateStatus(.nominal) + try await status.update(.nominal) // Update the metric of the device status to ensure that it is accurate - asyncScheduler.schedule { - await deviceManager.updateDeviceConnectionMetric() - } + await deviceManager.updateDeviceConnectionMetric() log("[\(df.string(from: Date()))] Server started") } 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 100644 index 8a0f34d..0000000 --- a/Sources/Run/main.swift +++ /dev/null @@ -1,13 +0,0 @@ -import App -import Vapor - -var env = Environment.production //.detect() -try LoggingSystem.bootstrap(from: &env) -let app = Application(env) -defer { - shutdown() - app.shutdown() -} - -try configure(app) -try app.run() diff --git a/Tests/AppTests/AppTests.swift b/Tests/AppTests/AppTests.swift deleted file mode 100644 index 913872e..0000000 --- a/Tests/AppTests/AppTests.swift +++ /dev/null @@ -1,13 +0,0 @@ -@testable import App -import XCTVapor - -final class AppTests: XCTestCase { - - func testEncodingUInt32() { - let input: UInt32 = 123 - let data = input.encoded - let output = UInt32(data: data) - XCTAssertEqual(input, output) - } - -}