Caps-Server/Sources/App/configure.swift

50 lines
1.2 KiB
Swift
Raw Normal View History

import Vapor
import Foundation
2023-01-11 18:29:32 +01:00
import Clairvoyant
2021-11-08 21:58:55 +01:00
private(set) var server: CapServer!
2023-01-11 18:29:32 +01:00
private(set) var monitor: PropertyManager!
2021-11-08 21:58:55 +01:00
2020-09-20 11:36:35 +02:00
public func configure(_ app: Application) throws {
2023-01-11 18:26:53 +01:00
let resourceDirectory = URL(fileURLWithPath: app.directory.resourcesDirectory)
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-11 18:29:32 +01:00
2023-01-11 18:28:37 +01:00
server = CapServer(in: URL(fileURLWithPath: publicDirectory),
writers: config.writers)
2023-01-11 18:29:32 +01:00
monitor = .init(logFolder: config.logURL, serverOwner: server)
monitor.update(status: .initializing)
server.registerProperties(with: monitor)
monitor.registerRoutes(app)
app.http.server.configuration.port = config.port
app.routes.defaultMaxBodySize = .init(stringLiteral: config.maxBodySize)
if config.serveFiles {
let middleware = FileMiddleware(publicDirectory: publicDirectory)
app.middleware.use(middleware)
}
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-01-11 18:29:32 +01:00
monitor.update(status: .initializationFailure)
return
2022-10-07 21:13:21 +02:00
}
2023-01-11 18:29:32 +01:00
// Register routes to the router
routes(app)
monitor.update(status: .nominal)
2022-10-07 21:13:21 +02:00
}
2023-01-11 18:29:32 +01:00
func log(_ message: String) {
monitor.log(message)
print(message)
2022-10-07 21:13:21 +02:00
}