feat: Removes old tests, fixes authentication middleware not working, view routes updated to not have delete routes and uses api routes for delete methods.

This commit is contained in:
2025-01-24 10:55:59 -05:00
parent aa60f69758
commit 90c6058d56
37 changed files with 146 additions and 564 deletions

View File

@@ -0,0 +1,53 @@
import Elementary
import SharedModels
import Vapor
import VaporElementary
import ViewController
extension ViewController {
func respond(route: ViewRoute, request: Vapor.Request) async throws -> any AsyncResponseEncodable {
let html = try await view(
for: route,
isHtmxRequest: request.isHtmxRequest,
logger: request.logger,
authenticate: { request.session.authenticate($0) }
)
return AnyHTMLResponse(value: html)
}
}
// Re-adapted from `HTMLResponse` in the VaporElementary package to work with any html types
// returned from the view controller.
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: AnySendableHTML) {
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)
})
)
}
}