feat: Cleans up some file names, adds generate admin user command.

This commit is contained in:
2025-01-19 17:07:08 -05:00
parent 185ebbbc15
commit 81f0e03549
10 changed files with 159 additions and 110 deletions

View File

@@ -5,6 +5,7 @@ import FluentSQLiteDriver
import NIOSSL
import SharedModels
import Vapor
@preconcurrency import VaporRouting
// configures your application
public func configure(_ app: Application) async throws {
@@ -18,7 +19,6 @@ public func configure(_ app: Application) async throws {
let cors = CORSMiddleware(configuration: corsConfiguration)
app.middleware.use(cors, at: .beginning)
// uncomment to serve files from /Public folder
app.middleware.use(FileMiddleware(publicDirectory: app.directory.publicDirectory))
app.middleware.use(app.sessions.middleware)
app.middleware.use(DependenciesMiddleware())
@@ -38,17 +38,45 @@ public func configure(_ app: Application) async throws {
let databaseClient = DatabaseClient.live(database: app.db)
try await app.migrations.add(databaseClient.migrations())
try withDependencies {
$0.database = databaseClient
} operation: {
try routes(app)
}
app.mount(
SiteRoute.router,
middleware: { $0.middleware() },
use: siteHandler
)
// if app.environment != .production {
try await app.autoMigrate()
// }
#if DEBUG
app.asyncCommands.use(SeedCommand(), as: "seed")
#endif
app.asyncCommands.use(GenerateAdminUserCommand(), as: "generate-admin")
}
extension SiteRoute {
func middleware() -> [any Middleware]? {
switch self {
case let .api(route):
return route.middleware
case .health:
return nil
case let .view(route):
return route.middleware
}
}
}
@Sendable
func siteHandler(
request: Request,
route: SiteRoute
) async throws -> any AsyncResponseEncodable {
switch route {
case let .api(route):
return try await route.handle(request: request)
case .health:
return HTTPStatus.ok
case let .view(route):
return try await route.handle(request: request)
}
}