import Vapor import Foundation import Clairvoyant import ClairvoyantVapor import ClairvoyantBinaryCodable private var provider: VaporMetricProvider! private let asyncScheduler = EventLoopScheduler() 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 = MetricObserver(logFileFolder: config.logURL, logMetricId: "caps.log") MetricObserver.standard = monitor let status = Metric("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 = CapServer(in: URL(fileURLWithPath: publicDirectory)) provider = .init(observer: monitor, accessManager: config.writers) provider.asyncScheduler = asyncScheduler provider.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 } asyncScheduler.schedule { await observer.log(message) } }