Move to new Vapor main
This commit is contained in:
@ -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<ServerStatus>("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")
|
||||
}
|
||||
|
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()
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user