feat: Begins view controller integration into app target.
This commit is contained in:
@@ -1,11 +1,14 @@
|
||||
import DatabaseClientLive
|
||||
import Dependencies
|
||||
import Elementary
|
||||
import Fluent
|
||||
import FluentSQLiteDriver
|
||||
import NIOSSL
|
||||
import SharedModels
|
||||
import Vapor
|
||||
import VaporElementary
|
||||
@preconcurrency import VaporRouting
|
||||
import ViewControllerLive
|
||||
|
||||
// configures your application
|
||||
public func configure(
|
||||
@@ -95,3 +98,52 @@ func siteHandler(
|
||||
return try await route.handle(request: request)
|
||||
}
|
||||
}
|
||||
|
||||
extension ViewRoute {
|
||||
func respond(request: Request) async throws -> any AsyncResponseEncodable {
|
||||
if self == .index {
|
||||
return request.redirect(to: ViewRoute.router.path(for: .purchaseOrder(.index)))
|
||||
} else {
|
||||
let html = try await view(isHtmxRequest: request.isHtmxRequest, authenticate: { request.auth.login($0) })
|
||||
// Delete routes return nil, but are valid routes.
|
||||
guard let html else {
|
||||
return HTTPStatus.ok
|
||||
}
|
||||
return AnyHTMLResponse(value: html)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct AnyHTMLResponse: AsyncResponseEncodable {
|
||||
|
||||
public var chunkSize: Int
|
||||
public var headers: HTTPHeaders = ["Content-Type": "text/html; charset=utf-8"]
|
||||
var value: _SendableAnyHTMLBox
|
||||
|
||||
init(chunkSize: Int = 1024, additionalHeaders: HTTPHeaders = [:], value: any HTML & Sendable) {
|
||||
self.chunkSize = chunkSize
|
||||
if additionalHeaders.contains(name: .contentType) {
|
||||
self.headers = additionalHeaders
|
||||
} else {
|
||||
headers.add(contentsOf: additionalHeaders)
|
||||
}
|
||||
self.value = .init(value)
|
||||
}
|
||||
|
||||
func encodeResponse(for request: Request) async throws -> Response {
|
||||
Response(
|
||||
status: .ok,
|
||||
headers: headers,
|
||||
body: .init(asyncStream: { [value, chunkSize] writer in
|
||||
guard let html = value.tryTake() else {
|
||||
assertionFailure("Non-sendable HTML value consumed more than once")
|
||||
request.logger.error("Non-sendable HTML value consumed more than once")
|
||||
throw Abort(.internalServerError)
|
||||
}
|
||||
try await writer.writeHTML(html, chunkSize: chunkSize)
|
||||
try await writer.write(.end)
|
||||
|
||||
})
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user