diff --git a/Resources/config.json b/Resources/config.json new file mode 100644 index 0000000..5f23903 --- /dev/null +++ b/Resources/config.json @@ -0,0 +1,4 @@ +{ + "logPath": "\/var\/log\/caps.log", + "serveFiles": true +} diff --git a/Resources/paths.conf b/Resources/paths.conf deleted file mode 100644 index 917b297..0000000 --- a/Resources/paths.conf +++ /dev/null @@ -1,2 +0,0 @@ -/data/logs/caps/server.log -/data/servers/caps/Public diff --git a/Sources/App/Config.swift b/Sources/App/Config.swift new file mode 100644 index 0000000..e1740cd --- /dev/null +++ b/Sources/App/Config.swift @@ -0,0 +1,16 @@ +import Foundation + +struct Config: Codable { + + let logPath: String + + let serveFiles: Bool + + var logURL: URL { + .init(fileURLWithPath: logPath) + } + + static var `default`: Config { + .init(logPath: "/var/log/caps.log", serveFiles: true) + } +} diff --git a/Sources/App/configure.swift b/Sources/App/configure.swift index feba30c..d48f6b0 100755 --- a/Sources/App/configure.swift +++ b/Sources/App/configure.swift @@ -1,35 +1,38 @@ import Vapor +import Foundation private(set) var server: CapServer! -// configures your application public func configure(_ app: Application) throws { - // uncomment to serve files from /Public folder - // app.middleware.use(FileMiddleware(publicDirectory: app.directory.publicDirectory)) app.http.server.configuration.port = 6001 app.routes.defaultMaxBodySize = "2mb" - + let configFile = URL(fileURLWithPath: app.directory.resourcesDirectory) - .appendingPathComponent("paths.conf") - let configData = try String(contentsOf: configFile) - .components(separatedBy: "\n") - .map { $0.trimmingCharacters(in: .whitespaces) } - .filter { !$0.isEmpty } - guard configData.count >= 2 else { - print("Invalid configuration file: \(configData.count) lines") - throw CapError.invalidConfiguration + .appendingPathComponent("config.json") + let config: Config + if !FileManager.default.fileExists(atPath: configFile.path) { + config = .default + let configData = try JSONEncoder().encode(config) + try configData.write(to: configFile) + print("Wrote default configuration") + } else { + let configData = try Data(contentsOf: configFile) + config = try JSONDecoder().decode(Config.self, from: configData) } - let logFile = URL(fileURLWithPath: configData[0]) - let publicDirectory = URL(fileURLWithPath: configData[1]) - - try Log.set(logFile: logFile.path) - - server = try CapServer(in: publicDirectory) + + try Log.set(logFile: config.logPath) + + let publicDirectory = app.directory.publicDirectory + if config.serveFiles { + let middleware = FileMiddleware(publicDirectory: publicDirectory) + app.middleware.use(middleware) + } + + server = try CapServer(in: URL(fileURLWithPath: publicDirectory)) try server.loadCapNames() // Register routes to the router try routes(app) - // Configure the rest of your application here }