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
|
|
|
|
|
2021-11-25 19:15:38 +01:00
|
|
|
// configures your application
|
2023-02-16 18:31:30 +01:00
|
|
|
public func configure(_ app: Application) async throws {
|
2021-12-22 22:13:09 +01:00
|
|
|
let storageFolder = URL(fileURLWithPath: app.directory.resourcesDirectory)
|
2021-12-09 14:47:18 +01:00
|
|
|
|
2023-01-31 22:16:44 +01:00
|
|
|
let logFolder = storageFolder.appendingPathComponent("logs")
|
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-02-16 18:31:30 +01:00
|
|
|
let status = try! await Metric<ServerStatus>(
|
2023-02-06 11:25:30 +01:00
|
|
|
"schafkopf.status",
|
|
|
|
name: "Status",
|
|
|
|
description: "The main status of the server")
|
|
|
|
|
2023-09-07 13:56:27 +02:00
|
|
|
_ = try? await status.update(.initializing)
|
2023-01-31 22:16:44 +01:00
|
|
|
|
2022-10-11 12:00:46 +02:00
|
|
|
let configPath = URL(fileURLWithPath: app.directory.resourcesDirectory)
|
|
|
|
.appendingPathComponent("config.json")
|
2023-02-01 16:44:07 +01:00
|
|
|
let configuration: Configuration
|
|
|
|
|
|
|
|
do {
|
|
|
|
configuration = try Configuration(loadFromUrl: configPath)
|
|
|
|
} catch {
|
2023-09-07 13:56:27 +02:00
|
|
|
_ = try? await status.update(.initializationFailure)
|
|
|
|
monitor.log("Failed to read configuration: \(error)")
|
2023-02-01 16:44:07 +01:00
|
|
|
// 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
|
|
|
|
}
|
2022-10-11 12:00:46 +02:00
|
|
|
|
|
|
|
app.http.server.configuration.port = configuration.serverPort
|
2021-12-23 09:38:31 +01:00
|
|
|
|
2021-12-09 14:47:18 +01:00
|
|
|
// Set target environment
|
2021-12-18 15:08:43 +01:00
|
|
|
app.environment = .production
|
2021-12-22 22:13:09 +01:00
|
|
|
|
2022-10-13 12:25:51 +02:00
|
|
|
if !configuration.production {
|
2021-12-22 22:13:09 +01:00
|
|
|
app.logger.logLevel = .info
|
2023-02-06 22:03:02 +01:00
|
|
|
log("[DEVELOPMENT] Using in-memory database")
|
2021-12-22 22:13:09 +01:00
|
|
|
app.databases.use(.sqlite(.memory), as: .sqlite)
|
|
|
|
} else {
|
|
|
|
app.logger.logLevel = .notice
|
|
|
|
let dbFile = storageFolder.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)
|
|
|
|
}
|
|
|
|
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-09-07 13:56:27 +02:00
|
|
|
monitor.log("Failed to migrate database: \(error)")
|
|
|
|
_ = 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
|
|
|
|
|
|
|
// Gracefully shut down by closing potentially open socket
|
|
|
|
DispatchQueue.global(qos: .utility).asyncAfter(deadline: .now() + .seconds(5)) {
|
|
|
|
_ = app.server.onShutdown.always { _ in
|
|
|
|
server.disconnectAllSockets()
|
|
|
|
}
|
|
|
|
}
|
2021-12-22 22:13:09 +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)
|
|
|
|
provider.registerRoutes(app)
|
|
|
|
|
|
|
|
_ = try? await status.update(.nominal)
|
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-09-07 13:56:27 +02:00
|
|
|
observer.log(message)
|
2023-02-06 22:03:02 +01:00
|
|
|
}
|