Switch to new vapor main

This commit is contained in:
Christoph Hagen
2023-12-06 09:39:12 +01:00
parent 6aaa9cb458
commit e6132a38b3
7 changed files with 58 additions and 52 deletions

View File

@ -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<ServerStatus>("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 {

View 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()
}
}

View File

@ -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()