Caps-Server/Sources/App/configure.swift
2023-11-22 10:35:47 +01:00

86 lines
2.4 KiB
Swift
Executable File

import Vapor
import Foundation
import Clairvoyant
import ClairvoyantVapor
import ClairvoyantBinaryCodable
private var provider: VaporMetricProvider!
private var serverStatus: Metric<ServerStatus>!
private let asyncScheduler = EventLoopScheduler()
private var server: CapServer!
private func status(_ newStatus: ServerStatus) {
asyncScheduler.schedule {
try await serverStatus.update(newStatus)
}
}
public func configure(_ app: Application) 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 logURL = config.logURL(possiblyRelativeTo: resourceDirectory)
let monitor = MetricObserver(logFileFolder: logURL, logMetricId: "caps.log")
MetricObserver.standard = monitor
serverStatus = Metric<ServerStatus>("caps.status",
name: "Status",
description: "The general status of the service")
status(.initializing)
app.http.server.configuration.port = config.port
app.routes.defaultMaxBodySize = .init(stringLiteral: config.maxBodySize)
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 {
status(.initializationFailure)
print("[\(df.string(from: Date()))] Server failed to start: \(error)")
return
}
if server.canResizeImages {
status(.nominal)
} else {
status(.reducedFunctionality)
}
print("[\(df.string(from: Date()))] Server started (\(server.capCount) caps)")
}
func log(_ message: String) {
guard let observer = MetricObserver.standard else {
print(message)
return
}
asyncScheduler.schedule {
await observer.log(message)
}
}
private let df: DateFormatter = {
let df = DateFormatter()
df.dateStyle = .short
df.timeStyle = .short
return df
}()