import Vapor 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 publicDirectory = app.directory.publicDirectory if config.serveFiles { let middleware = FileMiddleware(publicDirectory: publicDirectory) app.middleware.use(middleware) } server = try CapServer(in: URL(fileURLWithPath: publicDirectory), writers: config.writers) // Register routes to the router try routes(app) } private func writeDefaultCofig(to path: URL) throws -> Config { do { let configData = try JSONEncoder().encode(Config.default) try configData.write(to: path) print("Wrote default configuration to \(path.path)") return .default } catch { print("Failed to write default config file at \(path.path): \(error)") throw error } } private func loadConfig(at path: URL) throws -> Config { do { let configData = try Data(contentsOf: path) return try JSONDecoder().decode(Config.self, from: configData) } catch { print("Failed to load config file at \(path.path): \(error)") throw error } }