feat: Adds middleware to routes

This commit is contained in:
2025-01-19 14:20:51 -05:00
parent b23dc6bf07
commit 185ebbbc15
6 changed files with 158 additions and 37 deletions

View File

@@ -7,10 +7,23 @@ import Fluent
import SharedModels
import Vapor
import VaporElementary
import VaporRouting
@preconcurrency import VaporRouting
func routes(_ app: Application) throws {
app.mount(SiteRoute.router, use: siteHandler)
app.mount(
SiteRoute.router,
middleware: { route in
switch route {
case let .api(route):
return route.middleware
case .health:
return nil
case let .view(route):
return route.middleware
}
},
use: siteHandler
)
app.get { _ in
HTMLResponse {
@@ -21,44 +34,13 @@ func routes(_ app: Application) throws {
}
}
}
//
// app.get("login") { req in
// let context = try req.query.decode(LoginContext.self)
// return await req.render {
// MainPage(displayNav: false, route: .login) {
// UserForm(context: .login(next: context.next))
// }
// }
// }
//
// app.post("login") { req in
// @Dependency(\.database.users) var users
// let loginForm = try req.content.decode(User.Login.self)
// let token = try await users.login(loginForm)
// let user = try await users.get(token.userID)!
// req.session.authenticate(user)
// let context = try req.query.decode(LoginContext.self)
//
// return await req.render {
// MainPage(displayNav: true, route: .purchaseOrders) {
// div(
// .hx.get(context.next ?? "/purchase-orders"),
// .hx.pushURL(true),
// .hx.target("body"),
// .hx.trigger(.event(.revealed)),
// .hx.indicator(".hx-indicator")
// ) {
// Img.spinner().attributes(.class("hx-indicator"))
// }
// }
// }
// }
}
private struct LoginContext: Content {
let next: String?
}
@Sendable
func siteHandler(
request: Request,
route: SiteRoute