Schafkopf-Server/Sources/App/configure.swift
2023-11-01 11:52:04 +01:00

121 lines
3.7 KiB
Swift

import Vapor
import Fluent
import Clairvoyant
import ClairvoyantBinaryCodable
import ClairvoyantVapor
var server: SQLiteDatabase!
private var provider: VaporMetricProvider! = nil
private var status: Metric<ServerStatus>!
private let scheduler = EventLoopScheduler()
private var configurationError: Error? = nil
public func configure(_ app: Application) throws {
let semaphore = DispatchSemaphore(value: 0)
scheduler.schedule {
do {
try await configureAsync(app)
} catch {
configurationError = error
}
semaphore.signal()
}
semaphore.wait()
if let configurationError {
throw configurationError
}
}
private func configureAsync(_ app: Application) async throws {
let storageFolder = URL(fileURLWithPath: app.directory.resourcesDirectory)
let logFolder = storageFolder.appendingPathComponent("logs")
let monitor = MetricObserver(
logFileFolder: logFolder,
logMetricId: "schafkopf.log")
MetricObserver.standard = monitor
status = Metric<ServerStatus>(
"schafkopf.status",
name: "Status",
description: "The main status of the server")
_ = try? await status.update(.initializing)
let configPath = URL(fileURLWithPath: app.directory.resourcesDirectory)
.appendingPathComponent("config.json")
let configuration: Configuration
do {
configuration = try Configuration(loadFromUrl: configPath)
} catch {
_ = try? await status.update(.initializationFailure)
await monitor.log("Failed to read configuration: \(error)")
// Note: If configuration can't be loaded, then the server will run on the wrong port
// and access to metrics is impossible, since no tokens are loaded
return
}
app.http.server.configuration.port = configuration.serverPort
// Set target environment
app.environment = .production
if !configuration.production {
app.logger.logLevel = .info
log("[DEVELOPMENT] Using in-memory database")
app.databases.use(.sqlite(.memory), as: .sqlite)
} else {
app.logger.logLevel = .notice
let dbFile = storageFolder.appendingPathComponent("db.sqlite").path
log("[PRODUCTION] Using database at \(dbFile)")
app.databases.use(.sqlite(.file(dbFile)), as: .sqlite)
}
app.migrations.add(UserTableMigration())
app.migrations.add(PasswordResetMigration())
do {
try await app.autoMigrate()
} catch {
await monitor.log("Failed to migrate database: \(error)")
_ = try? await status.update(.initializationFailure)
return
}
// serve files from /Public folder
app.middleware.use(FileMiddleware(publicDirectory: app.directory.publicDirectory))
let db = app.databases.database(.sqlite, logger: .init(label: "Init"), on: app.databases.eventLoopGroup.next())!
server = try await SQLiteDatabase(database: db, mail: configuration.mail)
// Gracefully shut down by closing potentially open socket
DispatchQueue.global(qos: .utility).asyncAfter(deadline: .now() + .seconds(5)) {
_ = app.server.onShutdown.always { _ in
scheduler.schedule {
await server.disconnectAllSockets()
}
}
}
// register routes
routes(app)
// Expose metrics
provider = .init(observer: monitor, accessManager: configuration.monitoringTokens)
provider.asyncScheduler = scheduler
provider.registerRoutes(app)
_ = try? await status.update(.nominal)
}
func log(_ message: String) {
guard let observer = MetricObserver.standard else {
print(message)
return
}
scheduler.schedule {
await observer.log(message)
}
}