Extend configuration
This commit is contained in:
parent
08b34cfff9
commit
ad0c28ad44
@ -1,4 +1,6 @@
|
||||
{
|
||||
"port" : 6001,
|
||||
"maxBodySize" : "2mb",
|
||||
"logPath": "\/var\/log\/caps.log",
|
||||
"serveFiles": true,
|
||||
"writers" : [
|
||||
|
@ -2,6 +2,12 @@ import Foundation
|
||||
|
||||
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
|
||||
let logPath: String
|
||||
|
||||
@ -14,8 +20,47 @@ struct Config: Codable {
|
||||
var logURL: URL {
|
||||
.init(fileURLWithPath: logPath)
|
||||
}
|
||||
}
|
||||
|
||||
static var `default`: Config {
|
||||
.init(logPath: "/var/log/caps.log", serveFiles: true, writers: [])
|
||||
extension Config {
|
||||
|
||||
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)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -5,22 +5,12 @@ import Foundation
|
||||
private(set) var server: CapServer!
|
||||
|
||||
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)
|
||||
|
||||
let resourceDirectory = URL(fileURLWithPath: app.directory.resourcesDirectory)
|
||||
let publicDirectory = app.directory.publicDirectory
|
||||
let config = Config(loadFrom: resourceDirectory)
|
||||
if config.serveFiles {
|
||||
let middleware = FileMiddleware(publicDirectory: publicDirectory)
|
||||
app.middleware.use(middleware)
|
||||
|
Loading…
Reference in New Issue
Block a user