Extend configuration

This commit is contained in:
Christoph Hagen 2023-01-11 18:26:53 +01:00
parent 08b34cfff9
commit ad0c28ad44
3 changed files with 51 additions and 14 deletions

View File

@ -1,4 +1,6 @@
{ {
"port" : 6001,
"maxBodySize" : "2mb",
"logPath": "\/var\/log\/caps.log", "logPath": "\/var\/log\/caps.log",
"serveFiles": true, "serveFiles": true,
"writers" : [ "writers" : [

View File

@ -2,6 +2,12 @@ import Foundation
struct Config: Codable { struct Config: Codable {
/// The port where the server runs
let port: Int
/// The maximum size of the request body
let maxBodySize: String
/// The path to the log file /// The path to the log file
let logPath: String let logPath: String
@ -14,8 +20,47 @@ struct Config: Codable {
var logURL: URL { var logURL: URL {
.init(fileURLWithPath: logPath) .init(fileURLWithPath: logPath)
} }
}
static var `default`: Config { extension Config {
.init(logPath: "/var/log/caps.log", serveFiles: true, writers: [])
private static func file(in directory: URL) -> URL {
directory.appendingPathComponent("config.json")
}
init(loadFrom directory: URL) {
let configFileUrl = Config.file(in: directory)
if FileManager.default.fileExists(atPath: configFileUrl.path) {
self.init(loadAt: configFileUrl)
} else {
self.init(standardIn: directory)
}
}
private init(loadAt url: URL) {
do {
let configData = try Data(contentsOf: url)
self = try JSONDecoder().decode(Config.self, from: configData)
} catch {
print("Failed to load configuration from \(url.path): \(error)")
print("Using default configuration")
self.init(standardIn: url.deletingLastPathComponent())
}
}
private init(standardIn directory: URL) {
let defaultLogPath = directory.appendingPathComponent("logs").path
self.init(port: 8000, maxBodySize: "2mb", logPath: defaultLogPath, serveFiles: true, writers: [])
let configFileUrl = Config.file(in: directory)
do {
let configData = try JSONEncoder().encode(self)
try configData.write(to: configFileUrl)
print("Default configuration written at \(configFileUrl.path)")
} catch {
print("Failed to write default configuration to \(configFileUrl.path)")
}
} }
} }

View File

@ -5,22 +5,12 @@ import Foundation
private(set) var server: CapServer! private(set) var server: CapServer!
public func configure(_ app: Application) throws { public func configure(_ app: Application) throws {
app.http.server.configuration.port = 6001
app.routes.defaultMaxBodySize = "2mb"
let configFile = URL(fileURLWithPath: app.directory.resourcesDirectory)
.appendingPathComponent("config.json")
let config: Config
if !FileManager.default.fileExists(atPath: configFile.path) {
config = try writeDefaultCofig(to: configFile)
} else {
config = try loadConfig(at: configFile)
}
try Log.set(logFile: config.logPath) try Log.set(logFile: config.logPath)
let resourceDirectory = URL(fileURLWithPath: app.directory.resourcesDirectory)
let publicDirectory = app.directory.publicDirectory let publicDirectory = app.directory.publicDirectory
let config = Config(loadFrom: resourceDirectory)
if config.serveFiles { if config.serveFiles {
let middleware = FileMiddleware(publicDirectory: publicDirectory) let middleware = FileMiddleware(publicDirectory: publicDirectory)
app.middleware.use(middleware) app.middleware.use(middleware)