Switch to new Vapor main
This commit is contained in:
parent
1e649f297e
commit
e62ccb9241
@ -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 <https://github.com/swift-server/guides/blob/main/docs/building.md#building-for-production> 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"),
|
||||
// ])
|
||||
]
|
||||
)
|
||||
]
|
||||
)
|
||||
|
@ -11,23 +11,7 @@ private var status: Metric<ServerStatus>!
|
||||
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) {
|
||||
|
43
Sources/App/entrypoint.swift
Normal file
43
Sources/App/entrypoint.swift
Normal file
@ -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()
|
||||
}
|
||||
}
|
@ -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()
|
@ -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!")
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user