From 2299cae50c6419413d815caf05c47e6ba4c2ce51 Mon Sep 17 00:00:00 2001 From: Christoph Hagen Date: Wed, 11 Jan 2023 18:28:37 +0100 Subject: [PATCH] Simplify server init --- Sources/App/CapServer.swift | 76 ++++++++++++++++++++++--------------- Sources/App/configure.swift | 4 +- 2 files changed, 47 insertions(+), 33 deletions(-) diff --git a/Sources/App/CapServer.swift b/Sources/App/CapServer.swift index 5ab2b1d..66ee65d 100644 --- a/Sources/App/CapServer.swift +++ b/Sources/App/CapServer.swift @@ -23,28 +23,9 @@ final class CapServer { private var writers: Set - var classifierVersion: Int { - get { - do { - let content = try String(contentsOf: classifierVersionFile) - .trimmingCharacters(in: .whitespacesAndNewlines) - guard let value = Int(content) else { - log("Invalid classifier version: \(content)") - return 0 - } - return value - } catch { - log("Failed to read classifier version file: \(error)") - return 0 - } - } - set { - do { - try "\(newValue)".data(using: .utf8)! - .write(to: classifierVersionFile) - } catch { - log("Failed to save classifier version: \(error)") - } + var classifierVersion: Int = 0 { + didSet { + writeClassifierVersion() } } @@ -69,27 +50,60 @@ final class CapServer { var nextClassifierVersion: Int { caps.values.compactMap { $0.classifierVersion }.max() ?? 1 } - - init(in folder: URL, writers: [String]) throws { + + var capCount: Int { + caps.count + } + + var imageCount: Int { + caps.reduce(0) { $0 + $1.value.count } + } + + init(in folder: URL, writers: [String]) { self.imageFolder = folder.appendingPathComponent("images") self.dbFile = folder.appendingPathComponent("caps.json") self.htmlFile = folder.appendingPathComponent("count.html") self.classifierVersionFile = folder.appendingPathComponent("classifier.version") self.classifierFile = folder.appendingPathComponent("classifier.mlmodel") self.writers = Set(writers) - - var isDirectory: ObjCBool = false - guard fm.fileExists(atPath: folder.path, isDirectory: &isDirectory), - isDirectory.boolValue else { - log("Public directory \(folder.path) is not a folder, or doesn't exist") - throw CapError.invalidConfiguration - } + } + func loadData() throws { + loadClassifierVersion(at: classifierVersionFile) try loadCaps() try updateCounts() saveCapCountHTML() } + private func loadClassifierVersion(at url: URL) { + guard fm.fileExists(atPath: url.path) else { + return + } + let content: String + do { + content = try String(contentsOf: url) + .trimmingCharacters(in: .whitespacesAndNewlines) + } catch { + log("Failed to read classifier version file: \(error)") + return + } + + guard let value = Int(content) else { + log("Invalid classifier version: \(content)") + return + } + self.classifierVersion = value + } + + private func writeClassifierVersion() { + do { + try "\(classifierVersion)".data(using: .utf8)! + .write(to: classifierVersionFile) + } catch { + log("Failed to save classifier version: \(error)") + } + } + private func loadCaps() throws { do { let data = try Data(contentsOf: dbFile) diff --git a/Sources/App/configure.swift b/Sources/App/configure.swift index 3ef0e89..a78039e 100755 --- a/Sources/App/configure.swift +++ b/Sources/App/configure.swift @@ -11,13 +11,13 @@ public func configure(_ app: Application) throws { let resourceDirectory = URL(fileURLWithPath: app.directory.resourcesDirectory) let publicDirectory = app.directory.publicDirectory let config = Config(loadFrom: resourceDirectory) + server = CapServer(in: URL(fileURLWithPath: publicDirectory), + writers: config.writers) 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)