Skip to content

Commit

Permalink
Fix Vapor handler
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewBarba committed Apr 6, 2024
1 parent 081a50f commit 2f655ca
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions Sources/VercelVapor/VaporHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,12 @@ extension VaporHandler {
try await configure(app: app)
// Configure vercel server
app.servers.use(.vercel)
// Start the application
try await app.startup()
// Cache the app instance
await MainActor.run {
Shared.app = app
}
await Shared.default.setApp(app)
}

public func onRequest(_ req: Vercel.Request) async throws -> Vercel.Response {
guard let app = await Shared.app else {
guard let app = await Shared.default.app else {
return .status(.serviceUnavailable).send("Vapor application not configured")
}
let vaporRequest = try Vapor.Request.from(request: req, for: app)
Expand All @@ -46,10 +42,15 @@ extension VaporHandler {
}
}

private struct Shared {
private actor Shared {

static let `default` = Shared()

@MainActor
static var app: Application?
private(set) var app: Application?

func setApp(_ app: Application) {
self.app = app
}
}

extension Vapor.Request {
Expand Down

0 comments on commit 2f655ca

Please sign in to comment.