Caps-Server/Sources/App/configure.swift
2023-02-16 23:08:58 +01:00

59 lines
1.7 KiB
Swift
Executable File

import Vapor
import Foundation
import Clairvoyant
public func configure(_ app: Application) async throws {
let resourceDirectory = URL(fileURLWithPath: app.directory.resourcesDirectory)
let publicDirectory = app.directory.publicDirectory
let config = Config(loadFrom: resourceDirectory)
let authenticator = Authenticator(writers: config.writers)
let monitor = await MetricObserver(
logFolder: config.logURL,
accessManager: authenticator,
logMetricId: "caps.log")
// All new metrics are automatically registered with the standard observer
MetricObserver.standard = monitor
let status = try await Metric<ServerStatus>("caps.status",
name: "Status",
description: "The general status of the service")
try await status.update(.initializing)
app.http.server.configuration.port = config.port
app.routes.defaultMaxBodySize = .init(stringLiteral: config.maxBodySize)
let server = await CapServer(in: URL(fileURLWithPath: publicDirectory))
await monitor.registerRoutes(app)
if config.serveFiles {
let middleware = FileMiddleware(publicDirectory: publicDirectory)
app.middleware.use(middleware)
}
// Register routes to the router
server.registerRoutes(with: app, authenticator: authenticator)
// Initialize the server data
do {
try server.loadData()
} catch {
try await status.update(.initializationFailure)
}
try await status.update(.nominal)
}
func log(_ message: String) {
guard let observer = MetricObserver.standard else {
print(message)
return
}
Task {
await observer.log(message)
}
}