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
|
2020-05-17 20:01:30 +02:00
|
|
|
|
2020-09-20 11:36:35 +02:00
|
|
|
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)
|
|
|
|
|
|
|
|
let monitor = MetricObserver(
|
|
|
|
logFolder: config.logURL,
|
2023-01-30 16:07:04 +01:00
|
|
|
accessManager: authenticator,
|
2023-01-17 22:02:27 +01:00
|
|
|
logMetricId: "caps.log")
|
2023-01-11 18:29:32 +01:00
|
|
|
|
2023-01-17 22:02:27 +01:00
|
|
|
// All new metrics are automatically registered with the standard observer
|
|
|
|
MetricObserver.standard = monitor
|
2023-01-11 18:29:32 +01:00
|
|
|
|
2023-02-06 10:03:08 +01:00
|
|
|
let status = Metric<ServerStatus>("caps.status",
|
|
|
|
name: "Status",
|
|
|
|
description: "The general status of the service")
|
2023-01-17 22:02:27 +01:00
|
|
|
status.update(.initializing)
|
|
|
|
|
|
|
|
let server = CapServer(in: URL(fileURLWithPath: publicDirectory))
|
2023-01-11 18:29:32 +01:00
|
|
|
|
|
|
|
monitor.registerRoutes(app)
|
|
|
|
|
|
|
|
app.http.server.configuration.port = config.port
|
|
|
|
app.routes.defaultMaxBodySize = .init(stringLiteral: config.maxBodySize)
|
|
|
|
|
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()
|
2023-01-17 22:02:27 +01:00
|
|
|
status.update(.nominal)
|
2022-10-07 21:13:21 +02:00
|
|
|
} catch {
|
2023-01-17 22:02:27 +01:00
|
|
|
status.update(.initializationFailure)
|
2022-10-07 21:13:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 18:29:32 +01:00
|
|
|
func log(_ message: String) {
|
2023-01-17 22:02:27 +01:00
|
|
|
MetricObserver.standard?.log(message)
|
2023-01-11 18:29:32 +01:00
|
|
|
print(message)
|
2022-10-07 21:13:21 +02:00
|
|
|
}
|