feat: Moves api controller to it's own module.
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import ApiController
|
||||
import DatabaseClientLive
|
||||
import Dependencies
|
||||
import Elementary
|
||||
@@ -15,6 +16,24 @@ public func configure(
|
||||
_ app: Application,
|
||||
makeDatabaseClient: @escaping (any Database) -> DatabaseClient = { .live(database: $0) }
|
||||
) async throws {
|
||||
// Setup the database client.
|
||||
let databaseClient = try await setupDatabase(on: app, factory: makeDatabaseClient)
|
||||
// Add the global middlewares.
|
||||
addMiddleware(to: app, database: databaseClient)
|
||||
#if DEBUG
|
||||
// Live reload of the application for development when launched with the `./swift-dev` command
|
||||
app.lifecycle.use(BrowserSyncHandler())
|
||||
#endif
|
||||
// Add our route handlers.
|
||||
addRoutes(to: app)
|
||||
if app.environment != .testing {
|
||||
try await app.autoMigrate()
|
||||
}
|
||||
// Add our custom cli-commands to the application.
|
||||
addCommands(to: app)
|
||||
}
|
||||
|
||||
private func addMiddleware(to app: Application, database databaseClient: DatabaseClient) {
|
||||
// cors middleware should come before default error middleware using `at: .beginning`
|
||||
let corsConfiguration = CORSMiddleware.Configuration(
|
||||
allowedOrigin: .all,
|
||||
@@ -27,11 +46,13 @@ public func configure(
|
||||
|
||||
app.middleware.use(FileMiddleware(publicDirectory: app.directory.publicDirectory))
|
||||
app.middleware.use(app.sessions.middleware)
|
||||
app.middleware.use(DependenciesMiddleware(database: databaseClient))
|
||||
}
|
||||
|
||||
#if DEBUG
|
||||
app.lifecycle.use(BrowserSyncHandler())
|
||||
#endif
|
||||
|
||||
private func setupDatabase(
|
||||
on app: Application,
|
||||
factory makeDatabaseClient: @escaping (any Database) -> DatabaseClient
|
||||
) async throws -> DatabaseClient {
|
||||
switch app.environment {
|
||||
case .production, .development:
|
||||
let dbFileName = Environment.get("SQLITE_FILENAME") ?? "db.sqlite"
|
||||
@@ -46,8 +67,10 @@ public func configure(
|
||||
try await app.migrations.add(databaseClient.migrations())
|
||||
}
|
||||
|
||||
app.middleware.use(DependenciesMiddleware(database: databaseClient))
|
||||
return databaseClient
|
||||
}
|
||||
|
||||
private func addRoutes(to app: Application) {
|
||||
// Redirect the index path to purchase order route.
|
||||
app.get { req in
|
||||
req.redirect(to: ViewRoute.router.path(for: .purchaseOrder(.index)))
|
||||
@@ -64,18 +87,16 @@ public func configure(
|
||||
},
|
||||
use: siteHandler
|
||||
)
|
||||
}
|
||||
|
||||
if app.environment != .testing {
|
||||
try await app.autoMigrate()
|
||||
}
|
||||
|
||||
private func addCommands(to app: Application) {
|
||||
#if DEBUG
|
||||
app.asyncCommands.use(SeedCommand(), as: "seed")
|
||||
#endif
|
||||
app.asyncCommands.use(GenerateAdminUserCommand(), as: "generate-admin")
|
||||
}
|
||||
|
||||
extension SiteRoute {
|
||||
private extension SiteRoute {
|
||||
|
||||
func middleware() -> [any Middleware]? {
|
||||
switch self {
|
||||
@@ -90,14 +111,16 @@ extension SiteRoute {
|
||||
}
|
||||
|
||||
@Sendable
|
||||
func siteHandler(
|
||||
private func siteHandler(
|
||||
request: Request,
|
||||
route: SiteRoute
|
||||
) async throws -> any AsyncResponseEncodable {
|
||||
@Dependency(\.apiController) var apiController
|
||||
@Dependency(\.viewController) var viewController
|
||||
|
||||
switch route {
|
||||
case let .api(route):
|
||||
return try await route.respond(request: request)
|
||||
return try await apiController.respond(route, request: request)
|
||||
case .health:
|
||||
return HTTPStatus.ok
|
||||
case let .view(route):
|
||||
|
||||
Reference in New Issue
Block a user