82 lines
2.7 KiB
Swift
82 lines
2.7 KiB
Swift
import Vapor
|
|
import Fluent
|
|
import Clairvoyant
|
|
|
|
var server: SQLiteDatabase!
|
|
|
|
// configures your application
|
|
public func configure(_ app: Application) throws {
|
|
let storageFolder = URL(fileURLWithPath: app.directory.resourcesDirectory)
|
|
|
|
let logFolder = storageFolder.appendingPathComponent("logs")
|
|
let accessManager = AccessTokenManager([])
|
|
let monitor = MetricObserver(
|
|
logFolder: logFolder,
|
|
accessManager: accessManager,
|
|
logMetricId: "schafkopf.log")
|
|
MetricObserver.standard = monitor
|
|
|
|
let status = Metric<ServerStatus>("schafkopf.status")
|
|
status.update(.initializing)
|
|
monitor.registerRoutes(app)
|
|
|
|
let configPath = URL(fileURLWithPath: app.directory.resourcesDirectory)
|
|
.appendingPathComponent("config.json")
|
|
let configuration: Configuration
|
|
|
|
do {
|
|
configuration = try Configuration(loadFromUrl: configPath)
|
|
} catch {
|
|
status.update(.initializationFailure)
|
|
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
|
|
}
|
|
configuration.monitoringTokens.map { $0.data(using: .utf8)! }.forEach(accessManager.add)
|
|
|
|
app.http.server.configuration.port = configuration.serverPort
|
|
|
|
// Set target environment
|
|
app.environment = .production
|
|
|
|
if !configuration.production {
|
|
app.logger.logLevel = .info
|
|
print("[DEVELOPMENT] Using in-memory database")
|
|
app.databases.use(.sqlite(.memory), as: .sqlite)
|
|
} else {
|
|
app.logger.logLevel = .notice
|
|
let dbFile = storageFolder.appendingPathComponent("db.sqlite").path
|
|
print("[PRODUCTION] Using database at \(dbFile)")
|
|
app.databases.use(.sqlite(.file(dbFile)), as: .sqlite)
|
|
}
|
|
app.migrations.add(UserTableMigration())
|
|
app.migrations.add(PasswordResetMigration())
|
|
|
|
do {
|
|
try app.autoMigrate().wait()
|
|
} catch {
|
|
monitor.log("Failed to migrate database: \(error)")
|
|
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 = 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
|
|
server.disconnectAllSockets()
|
|
}
|
|
}
|
|
|
|
// register routes
|
|
routes(app)
|
|
|
|
status.update(.nominal)
|
|
}
|