2021-11-25 19:15:38 +01:00
|
|
|
import Vapor
|
2021-12-22 22:13:09 +01:00
|
|
|
import Fluent
|
2023-01-31 22:16:44 +01:00
|
|
|
import Clairvoyant
|
2023-09-07 13:56:27 +02:00
|
|
|
import ClairvoyantBinaryCodable
|
|
|
|
import ClairvoyantVapor
|
2021-11-25 19:15:38 +01:00
|
|
|
|
2021-12-22 22:13:09 +01:00
|
|
|
var server: SQLiteDatabase!
|
2021-11-27 11:59:13 +01:00
|
|
|
|
2023-09-07 13:56:27 +02:00
|
|
|
private var provider: VaporMetricProvider! = nil
|
2023-10-02 00:37:50 +02:00
|
|
|
private var status: Metric<ServerStatus>!
|
2023-12-06 10:02:43 +01:00
|
|
|
private let scheduler = MultiThreadedEventLoopGroup(numberOfThreads: 2)
|
2023-11-01 11:52:04 +01:00
|
|
|
private var configurationError: Error? = nil
|
2023-10-02 00:37:50 +02:00
|
|
|
|
2023-12-06 10:01:19 +01:00
|
|
|
func configure(_ app: Application) async throws {
|
2023-12-25 18:46:05 +01:00
|
|
|
let resourceFolder = URL(fileURLWithPath: app.directory.resourcesDirectory)
|
|
|
|
let publicDirectory = app.directory.publicDirectory
|
2023-11-22 11:47:34 +01:00
|
|
|
|
|
|
|
let configPath = URL(fileURLWithPath: app.directory.resourcesDirectory)
|
|
|
|
.appendingPathComponent("config.json")
|
|
|
|
let configuration = try Configuration(loadFromUrl: configPath)
|
2021-12-09 14:47:18 +01:00
|
|
|
|
2023-12-25 18:46:05 +01:00
|
|
|
let logFolder = configuration.logURL(possiblyRelativeTo: resourceFolder)
|
2023-09-07 13:56:27 +02:00
|
|
|
let monitor = MetricObserver(
|
|
|
|
logFileFolder: logFolder,
|
2023-01-31 22:16:44 +01:00
|
|
|
logMetricId: "schafkopf.log")
|
|
|
|
MetricObserver.standard = monitor
|
|
|
|
|
2023-10-02 00:37:50 +02:00
|
|
|
status = Metric<ServerStatus>(
|
2023-02-06 11:25:30 +01:00
|
|
|
"schafkopf.status",
|
|
|
|
name: "Status",
|
|
|
|
description: "The main status of the server")
|
|
|
|
|
2023-12-06 10:01:19 +01:00
|
|
|
try await status.update(.initializing)
|
2023-01-31 22:16:44 +01:00
|
|
|
|
2022-10-11 12:00:46 +02:00
|
|
|
app.http.server.configuration.port = configuration.serverPort
|
2021-12-23 09:38:31 +01:00
|
|
|
|
2023-12-21 08:50:28 +01:00
|
|
|
switch app.environment {
|
|
|
|
case .production:
|
2021-12-22 22:13:09 +01:00
|
|
|
app.logger.logLevel = .notice
|
2023-12-25 18:46:05 +01:00
|
|
|
let dataDirectory = configuration.customDataDirectory(or: publicDirectory)
|
|
|
|
let dbFile = dataDirectory.appendingPathComponent("db.sqlite").path
|
2023-02-06 22:03:02 +01:00
|
|
|
log("[PRODUCTION] Using database at \(dbFile)")
|
2021-12-22 22:13:09 +01:00
|
|
|
app.databases.use(.sqlite(.file(dbFile)), as: .sqlite)
|
2023-12-25 19:17:16 +01:00
|
|
|
default:
|
|
|
|
log("[DEVELOPMENT] Using in-memory database")
|
|
|
|
app.databases.use(.sqlite(.memory), as: .sqlite)
|
2021-12-22 22:13:09 +01:00
|
|
|
}
|
2023-12-21 08:50:28 +01:00
|
|
|
|
2021-12-22 22:13:09 +01:00
|
|
|
app.migrations.add(UserTableMigration())
|
2022-10-11 12:04:15 +02:00
|
|
|
app.migrations.add(PasswordResetMigration())
|
2021-12-22 22:13:09 +01:00
|
|
|
|
2023-01-31 22:16:44 +01:00
|
|
|
do {
|
2023-02-16 18:51:27 +01:00
|
|
|
try await app.autoMigrate()
|
2023-01-31 22:16:44 +01:00
|
|
|
} catch {
|
2023-10-02 00:37:50 +02:00
|
|
|
await monitor.log("Failed to migrate database: \(error)")
|
2023-12-06 10:01:19 +01:00
|
|
|
try await status.update(.initializationFailure)
|
2023-01-31 22:16:44 +01:00
|
|
|
return
|
|
|
|
}
|
2021-12-09 14:47:18 +01:00
|
|
|
|
2021-11-27 11:59:13 +01:00
|
|
|
// serve files from /Public folder
|
|
|
|
app.middleware.use(FileMiddleware(publicDirectory: app.directory.publicDirectory))
|
2021-11-25 19:15:38 +01:00
|
|
|
|
2021-12-22 22:13:09 +01:00
|
|
|
let db = app.databases.database(.sqlite, logger: .init(label: "Init"), on: app.databases.eventLoopGroup.next())!
|
2023-02-16 18:31:30 +01:00
|
|
|
server = try await SQLiteDatabase(database: db, mail: configuration.mail)
|
2022-01-24 17:15:11 +01:00
|
|
|
|
2021-11-25 19:15:38 +01:00
|
|
|
// register routes
|
2023-01-31 22:17:15 +01:00
|
|
|
routes(app)
|
2023-01-31 22:16:44 +01:00
|
|
|
|
2023-09-07 13:56:27 +02:00
|
|
|
// Expose metrics
|
|
|
|
provider = .init(observer: monitor, accessManager: configuration.monitoringTokens)
|
2023-10-02 00:37:50 +02:00
|
|
|
provider.asyncScheduler = scheduler
|
2023-09-07 13:56:27 +02:00
|
|
|
provider.registerRoutes(app)
|
2023-12-25 19:20:50 +01:00
|
|
|
monitor.saveCurrentListOfMetricsToLogFolder()
|
2023-09-07 13:56:27 +02:00
|
|
|
|
2023-12-06 10:01:19 +01:00
|
|
|
try await status.update(.nominal)
|
|
|
|
}
|
|
|
|
|
|
|
|
func shutdown() {
|
|
|
|
scheduler.schedule {
|
|
|
|
await server.disconnectAllSockets()
|
2023-12-06 10:02:43 +01:00
|
|
|
try await scheduler.shutdownGracefully()
|
2023-12-06 10:01:19 +01:00
|
|
|
}
|
2021-11-25 19:15:38 +01:00
|
|
|
}
|
2023-02-06 22:03:02 +01:00
|
|
|
|
|
|
|
func log(_ message: String) {
|
2023-02-16 18:31:30 +01:00
|
|
|
guard let observer = MetricObserver.standard else {
|
|
|
|
print(message)
|
|
|
|
return
|
|
|
|
}
|
2023-10-02 00:37:50 +02:00
|
|
|
scheduler.schedule {
|
|
|
|
await observer.log(message)
|
|
|
|
}
|
2023-02-06 22:03:02 +01:00
|
|
|
}
|