Simplify server init
This commit is contained in:
parent
ad0c28ad44
commit
2299cae50c
@ -23,28 +23,9 @@ final class CapServer {
|
|||||||
|
|
||||||
private var writers: Set<String>
|
private var writers: Set<String>
|
||||||
|
|
||||||
var classifierVersion: Int {
|
var classifierVersion: Int = 0 {
|
||||||
get {
|
didSet {
|
||||||
do {
|
writeClassifierVersion()
|
||||||
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)")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,27 +50,60 @@ final class CapServer {
|
|||||||
var nextClassifierVersion: Int {
|
var nextClassifierVersion: Int {
|
||||||
caps.values.compactMap { $0.classifierVersion }.max() ?? 1
|
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.imageFolder = folder.appendingPathComponent("images")
|
||||||
self.dbFile = folder.appendingPathComponent("caps.json")
|
self.dbFile = folder.appendingPathComponent("caps.json")
|
||||||
self.htmlFile = folder.appendingPathComponent("count.html")
|
self.htmlFile = folder.appendingPathComponent("count.html")
|
||||||
self.classifierVersionFile = folder.appendingPathComponent("classifier.version")
|
self.classifierVersionFile = folder.appendingPathComponent("classifier.version")
|
||||||
self.classifierFile = folder.appendingPathComponent("classifier.mlmodel")
|
self.classifierFile = folder.appendingPathComponent("classifier.mlmodel")
|
||||||
self.writers = Set(writers)
|
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 loadCaps()
|
||||||
try updateCounts()
|
try updateCounts()
|
||||||
saveCapCountHTML()
|
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 {
|
private func loadCaps() throws {
|
||||||
do {
|
do {
|
||||||
let data = try Data(contentsOf: dbFile)
|
let data = try Data(contentsOf: dbFile)
|
||||||
|
@ -11,13 +11,13 @@ public func configure(_ app: Application) throws {
|
|||||||
let resourceDirectory = URL(fileURLWithPath: app.directory.resourcesDirectory)
|
let resourceDirectory = URL(fileURLWithPath: app.directory.resourcesDirectory)
|
||||||
let publicDirectory = app.directory.publicDirectory
|
let publicDirectory = app.directory.publicDirectory
|
||||||
let config = Config(loadFrom: resourceDirectory)
|
let config = Config(loadFrom: resourceDirectory)
|
||||||
|
server = CapServer(in: URL(fileURLWithPath: publicDirectory),
|
||||||
|
writers: config.writers)
|
||||||
if config.serveFiles {
|
if config.serveFiles {
|
||||||
let middleware = FileMiddleware(publicDirectory: publicDirectory)
|
let middleware = FileMiddleware(publicDirectory: publicDirectory)
|
||||||
app.middleware.use(middleware)
|
app.middleware.use(middleware)
|
||||||
}
|
}
|
||||||
|
|
||||||
server = try CapServer(in: URL(fileURLWithPath: publicDirectory),
|
|
||||||
writers: config.writers)
|
|
||||||
|
|
||||||
// Register routes to the router
|
// Register routes to the router
|
||||||
try routes(app)
|
try routes(app)
|
||||||
|
Loading…
Reference in New Issue
Block a user