Simplify server init
This commit is contained in:
parent
ad0c28ad44
commit
2299cae50c
@ -23,28 +23,9 @@ final class CapServer {
|
||||
|
||||
private var writers: Set<String>
|
||||
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
||||
@ -70,26 +51,59 @@ final class CapServer {
|
||||
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)
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user