From e62ccb9241e382b396e24097bfa77fe628d05eda Mon Sep 17 00:00:00 2001 From: Christoph Hagen Date: Wed, 6 Dec 2023 10:01:19 +0100 Subject: [PATCH] Switch to new Vapor main --- Package.swift | 15 ++---------- Sources/App/configure.swift | 39 ++++++++----------------------- Sources/App/entrypoint.swift | 43 +++++++++++++++++++++++++++++++++++ Sources/Run/main.swift | 10 -------- Tests/AppTests/AppTests.swift | 15 ------------ 5 files changed, 55 insertions(+), 67 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 76f9fd1..9cd0604 100644 --- a/Package.swift +++ b/Package.swift @@ -20,7 +20,7 @@ let package = Package( .package(url: "https://github.com/christophhagen/ClairvoyantBinaryCodable", from: "0.3.1"), ], targets: [ - .target( + .executableTarget( name: "App", dependencies: [ .product(name: "Fluent", package: "fluent"), @@ -30,18 +30,7 @@ let package = Package( .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 8cc3462..a49e83c 100644 --- a/Sources/App/configure.swift +++ b/Sources/App/configure.swift @@ -11,23 +11,7 @@ private var status: Metric! private let scheduler = EventLoopScheduler() private var configurationError: Error? = nil -public func configure(_ app: Application) throws { - let semaphore = DispatchSemaphore(value: 0) - scheduler.schedule { - do { - try await configureAsync(app) - } catch { - configurationError = error - } - semaphore.signal() - } - semaphore.wait() - if let configurationError { - throw configurationError - } -} - -private func configureAsync(_ app: Application) async throws { +func configure(_ app: Application) async throws { let storageFolder = URL(fileURLWithPath: app.directory.resourcesDirectory) let configPath = URL(fileURLWithPath: app.directory.resourcesDirectory) @@ -45,7 +29,7 @@ private func configureAsync(_ app: Application) async throws { name: "Status", description: "The main status of the server") - _ = try? await status.update(.initializing) + try await status.update(.initializing) app.http.server.configuration.port = configuration.serverPort @@ -69,7 +53,7 @@ private func configureAsync(_ app: Application) async throws { try await app.autoMigrate() } catch { await monitor.log("Failed to migrate database: \(error)") - _ = try? await status.update(.initializationFailure) + try await status.update(.initializationFailure) return } @@ -79,15 +63,6 @@ private func configureAsync(_ app: Application) async throws { let db = app.databases.database(.sqlite, logger: .init(label: "Init"), on: app.databases.eventLoopGroup.next())! server = try await SQLiteDatabase(database: db, mail: configuration.mail) - // Gracefully shut down by closing potentially open socket - DispatchQueue.global(qos: .utility).asyncAfter(deadline: .now() + .seconds(5)) { - _ = app.server.onShutdown.always { _ in - scheduler.schedule { - await server.disconnectAllSockets() - } - } - } - // register routes routes(app) @@ -96,7 +71,13 @@ private func configureAsync(_ app: Application) async throws { provider.asyncScheduler = scheduler provider.registerRoutes(app) - _ = try? await status.update(.nominal) + try await status.update(.nominal) +} + +func shutdown() { + scheduler.schedule { + await server.disconnectAllSockets() + } } func log(_ message: String) { 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 6d8dae7..0000000 --- a/Sources/Run/main.swift +++ /dev/null @@ -1,10 +0,0 @@ -import App -import Vapor - -var env = try Environment.detect() -try LoggingSystem.bootstrap(from: &env) -let app = Application(env) -defer { 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 9817630..0000000 --- a/Tests/AppTests/AppTests.swift +++ /dev/null @@ -1,15 +0,0 @@ -@testable import App -import XCTVapor - -final class AppTests: XCTestCase { - func testHelloWorld() throws { - let app = Application(.testing) - defer { app.shutdown() } - try configure(app) - - try app.test(.GET, "hello", afterResponse: { res in - XCTAssertEqual(res.status, .ok) - XCTAssertEqual(res.body.string, "Hello, world!") - }) - } -}