2020-05-17 20:01:30 +02:00
|
|
|
import Vapor
|
2022-05-22 23:24:04 +02:00
|
|
|
import Foundation
|
2023-01-11 18:29:32 +01:00
|
|
|
import Clairvoyant
|
2023-09-08 10:05:55 +02:00
|
|
|
import ClairvoyantVapor
|
|
|
|
import ClairvoyantBinaryCodable
|
|
|
|
|
|
|
|
private var provider: VaporMetricProvider!
|
2023-11-22 10:02:16 +01:00
|
|
|
private var serverStatus: Metric<ServerStatus>!
|
2020-05-17 20:01:30 +02:00
|
|
|
|
2023-10-02 00:04:36 +02:00
|
|
|
private let asyncScheduler = EventLoopScheduler()
|
|
|
|
|
2023-11-22 10:02:16 +01:00
|
|
|
private var server: CapServer!
|
|
|
|
|
|
|
|
private func status(_ newStatus: ServerStatus) {
|
|
|
|
asyncScheduler.schedule {
|
|
|
|
try await serverStatus.update(newStatus)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public func configure(_ app: Application) throws {
|
2022-05-22 23:24:04 +02:00
|
|
|
|
2023-01-11 18:26:53 +01:00
|
|
|
let resourceDirectory = URL(fileURLWithPath: app.directory.resourcesDirectory)
|
2022-05-22 23:24:04 +02:00
|
|
|
let publicDirectory = app.directory.publicDirectory
|
2023-01-11 18:29:32 +01:00
|
|
|
|
2023-01-11 18:26:53 +01:00
|
|
|
let config = Config(loadFrom: resourceDirectory)
|
2023-01-17 22:02:27 +01:00
|
|
|
let authenticator = Authenticator(writers: config.writers)
|
|
|
|
|
2023-11-22 10:35:47 +01:00
|
|
|
let logURL = config.logURL(possiblyRelativeTo: resourceDirectory)
|
|
|
|
let monitor = MetricObserver(logFileFolder: logURL, logMetricId: "caps.log")
|
2023-01-17 22:02:27 +01:00
|
|
|
MetricObserver.standard = monitor
|
2023-01-11 18:29:32 +01:00
|
|
|
|
2023-11-22 10:02:16 +01:00
|
|
|
serverStatus = Metric<ServerStatus>("caps.status",
|
2023-02-06 10:03:08 +01:00
|
|
|
name: "Status",
|
|
|
|
description: "The general status of the service")
|
2023-11-22 10:02:16 +01:00
|
|
|
status(.initializing)
|
2023-01-11 18:29:32 +01:00
|
|
|
|
|
|
|
app.http.server.configuration.port = config.port
|
|
|
|
app.routes.defaultMaxBodySize = .init(stringLiteral: config.maxBodySize)
|
|
|
|
|
2023-11-22 10:02:16 +01:00
|
|
|
server = CapServer(in: URL(fileURLWithPath: publicDirectory))
|
2023-02-16 23:08:58 +01:00
|
|
|
|
2023-09-08 10:05:55 +02:00
|
|
|
provider = .init(observer: monitor, accessManager: config.writers)
|
2023-10-02 00:04:36 +02:00
|
|
|
provider.asyncScheduler = asyncScheduler
|
2023-09-08 10:05:55 +02:00
|
|
|
provider.registerRoutes(app)
|
2023-02-16 23:08:58 +01:00
|
|
|
|
2022-05-22 23:24:04 +02:00
|
|
|
if config.serveFiles {
|
|
|
|
let middleware = FileMiddleware(publicDirectory: publicDirectory)
|
|
|
|
app.middleware.use(middleware)
|
|
|
|
}
|
|
|
|
|
2023-01-17 22:02:27 +01:00
|
|
|
// Register routes to the router
|
|
|
|
server.registerRoutes(with: app, authenticator: authenticator)
|
|
|
|
|
|
|
|
// Initialize the server data
|
2022-10-07 21:13:21 +02:00
|
|
|
do {
|
2023-01-11 18:29:32 +01:00
|
|
|
try server.loadData()
|
2022-10-07 21:13:21 +02:00
|
|
|
} catch {
|
2023-11-22 10:02:16 +01:00
|
|
|
status(.initializationFailure)
|
|
|
|
print("[\(df.string(from: Date()))] Server failed to start: \(error)")
|
|
|
|
return
|
2022-10-07 21:13:21 +02:00
|
|
|
}
|
2023-10-25 15:38:22 +02:00
|
|
|
if server.canResizeImages {
|
2023-11-22 10:02:16 +01:00
|
|
|
status(.nominal)
|
2023-10-25 15:38:22 +02:00
|
|
|
} else {
|
2023-11-22 10:02:16 +01:00
|
|
|
status(.reducedFunctionality)
|
2023-10-25 15:38:22 +02:00
|
|
|
}
|
2023-11-22 10:02:16 +01:00
|
|
|
print("[\(df.string(from: Date()))] Server started (\(server.capCount) caps)")
|
2022-10-07 21:13:21 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 18:29:32 +01:00
|
|
|
func log(_ message: String) {
|
2023-02-16 23:08:58 +01:00
|
|
|
guard let observer = MetricObserver.standard else {
|
|
|
|
print(message)
|
|
|
|
return
|
|
|
|
}
|
2023-10-02 00:04:36 +02:00
|
|
|
asyncScheduler.schedule {
|
|
|
|
await observer.log(message)
|
|
|
|
}
|
2022-10-07 21:13:21 +02:00
|
|
|
}
|
2023-11-22 10:02:16 +01:00
|
|
|
|
|
|
|
private let df: DateFormatter = {
|
|
|
|
let df = DateFormatter()
|
|
|
|
df.dateStyle = .short
|
|
|
|
df.timeStyle = .short
|
|
|
|
return df
|
|
|
|
}()
|