53 lines
1.6 KiB
Swift
53 lines
1.6 KiB
Swift
import DatabaseClientLive
|
|
import Dependencies
|
|
import Fluent
|
|
import FluentSQLiteDriver
|
|
import Leaf
|
|
import NIOSSL
|
|
import SharedModels
|
|
import Vapor
|
|
|
|
// configures your application
|
|
public func configure(_ app: Application) async throws {
|
|
// cors middleware should come before default error middleware using `at: .beginning`
|
|
let corsConfiguration = CORSMiddleware.Configuration(
|
|
allowedOrigin: .all,
|
|
allowedMethods: [.GET, .POST, .PUT, .OPTIONS, .DELETE, .PATCH],
|
|
allowedHeaders: [.accept, .authorization, .contentType, .origin,
|
|
.xRequestedWith, .userAgent, .accessControlAllowOrigin]
|
|
)
|
|
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())
|
|
|
|
#if DEBUG
|
|
app.lifecycle.use(BrowserSyncHandler())
|
|
#endif
|
|
|
|
switch app.environment {
|
|
case .production, .development:
|
|
app.databases.use(DatabaseConfigurationFactory.sqlite(.file("db.sqlite")), as: .sqlite)
|
|
default:
|
|
app.databases.use(DatabaseConfigurationFactory.sqlite(.memory), as: .sqlite)
|
|
}
|
|
|
|
let databaseClient = DatabaseClient.live(database: app.db)
|
|
try await app.migrations.add(databaseClient.migrations())
|
|
|
|
app.views.use(.leaf)
|
|
|
|
try withDependencies {
|
|
$0.database = databaseClient
|
|
} operation: {
|
|
try routes(app)
|
|
}
|
|
|
|
if app.environment != .production {
|
|
try await app.autoMigrate()
|
|
}
|
|
}
|