feat: Updates environment variables and datbase to allow postgres configuration for production environments.
All checks were successful
CI / Linux Tests (push) Successful in 6m39s
All checks were successful
CI / Linux Tests (push) Successful in 6m39s
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
import DatabaseClient
|
||||
import Dependencies
|
||||
import Elementary
|
||||
import EnvVars
|
||||
import Fluent
|
||||
import FluentPostgresDriver
|
||||
import FluentSQLiteDriver
|
||||
import ManualDCore
|
||||
import NIOSSL
|
||||
@@ -14,12 +16,15 @@ import ViewController
|
||||
// configures your application
|
||||
public func configure(
|
||||
_ app: Application,
|
||||
in environment: EnvVars,
|
||||
makeDatabaseClient: @escaping (any Database) -> DatabaseClient = { .live(database: $0) }
|
||||
) async throws {
|
||||
// Setup the database client.
|
||||
let databaseClient = try await setupDatabase(on: app, factory: makeDatabaseClient)
|
||||
let databaseClient = try await setupDatabase(
|
||||
on: app, environment: environment, factory: makeDatabaseClient
|
||||
)
|
||||
// Add the global middlewares.
|
||||
addMiddleware(to: app, database: databaseClient)
|
||||
addMiddleware(to: app, database: databaseClient, environment: environment)
|
||||
#if DEBUG
|
||||
// Live reload of the application for development when launched with the `./swift-dev` command
|
||||
// app.lifecycle.use(BrowserSyncHandler())
|
||||
@@ -33,7 +38,11 @@ public func configure(
|
||||
addCommands(to: app)
|
||||
}
|
||||
|
||||
private func addMiddleware(to app: Application, database databaseClient: DatabaseClient) {
|
||||
private func addMiddleware(
|
||||
to app: Application,
|
||||
database databaseClient: DatabaseClient,
|
||||
environment: EnvVars
|
||||
) {
|
||||
// cors middleware should come before default error middleware using `at: .beginning`
|
||||
let corsConfiguration = CORSMiddleware.Configuration(
|
||||
allowedOrigin: .all,
|
||||
@@ -48,16 +57,20 @@ private func addMiddleware(to app: Application, database databaseClient: Databas
|
||||
|
||||
app.middleware.use(FileMiddleware(publicDirectory: app.directory.publicDirectory))
|
||||
app.middleware.use(app.sessions.middleware)
|
||||
app.middleware.use(DependenciesMiddleware(database: databaseClient))
|
||||
app.middleware.use(DependenciesMiddleware(database: databaseClient, environment: environment))
|
||||
}
|
||||
|
||||
private func setupDatabase(
|
||||
on app: Application,
|
||||
environment: EnvVars,
|
||||
factory makeDatabaseClient: @escaping (any Database) -> DatabaseClient
|
||||
) async throws -> DatabaseClient {
|
||||
switch app.environment {
|
||||
case .production, .development:
|
||||
let dbFileName = Environment.get("SQLITE_FILENAME") ?? "db.sqlite"
|
||||
case .production:
|
||||
let configuration = try environment.postgresConfiguration()
|
||||
app.databases.use(.postgres(configuration: configuration), as: .psql)
|
||||
case .development:
|
||||
let dbFileName = environment.sqlitePath ?? "db.sqlite"
|
||||
app.databases.use(DatabaseConfigurationFactory.sqlite(.file(dbFileName)), as: .sqlite)
|
||||
default:
|
||||
app.databases.use(DatabaseConfigurationFactory.sqlite(.memory), as: .sqlite)
|
||||
@@ -129,3 +142,30 @@ private func siteHandler(
|
||||
return try await viewController.respond(route: route, request: request)
|
||||
}
|
||||
}
|
||||
|
||||
extension EnvVars {
|
||||
func postgresConfiguration() throws -> SQLPostgresConfiguration {
|
||||
guard let hostname = postgresHostname,
|
||||
let username = postgresUsername,
|
||||
let password = postgresPassword,
|
||||
let database = postgresDatabase
|
||||
else {
|
||||
throw EnvError("Missing environment variables for postgres connection.")
|
||||
}
|
||||
return .init(
|
||||
hostname: hostname,
|
||||
username: username,
|
||||
password: password,
|
||||
database: database,
|
||||
tls: .disable
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
struct EnvError: Error {
|
||||
let reason: String
|
||||
|
||||
init(_ reason: String) {
|
||||
self.reason = reason
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user