Improve logging
This commit is contained in:
parent
29ecde067f
commit
e4f2fc547b
@ -8,7 +8,7 @@ let package = Package(
|
||||
],
|
||||
dependencies: [
|
||||
.package(url: "https://github.com/vapor/vapor", from: "4.0.0"),
|
||||
.package(url: "https://github.com/christophhagen/Clairvoyant", branch: "main"),
|
||||
.package(url: "https://github.com/christophhagen/Clairvoyant", from: "0.4.0"),
|
||||
],
|
||||
targets: [
|
||||
.target(name: "App",
|
||||
|
@ -1,7 +1,7 @@
|
||||
{
|
||||
"port" : 6001,
|
||||
"maxBodySize" : "2mb",
|
||||
"logPath": "\/var\/log\/caps.log",
|
||||
"logPath": "\/var\/log\/caps/metrics",
|
||||
"serveFiles": true,
|
||||
"writers" : [
|
||||
"auth_key_1"
|
||||
|
@ -29,26 +29,26 @@ extension CapServer {
|
||||
app.postCatching("cap") { request in
|
||||
try self.ensureOperability()
|
||||
try authenticator.authorize(request)
|
||||
let data = try request.getBodyData()
|
||||
let data = try request.getBodyData(request: "/cap")
|
||||
let cap = try decoder.decode(Cap.self, from: data)
|
||||
try self.addOrUpdate(cap)
|
||||
}
|
||||
|
||||
// Upload an image
|
||||
app.postCatching("images", ":n") { request in
|
||||
app.postCatching("images", ":cap") { request in
|
||||
try self.ensureOperability()
|
||||
try authenticator.authorize(request)
|
||||
guard let cap = request.parameters.get("n", as: Int.self) else {
|
||||
log("Invalid parameter for cap")
|
||||
log("/images/:cap: Invalid 'cap' parameter")
|
||||
throw Abort(.badRequest)
|
||||
}
|
||||
let data = try request.getBodyData()
|
||||
let data = try request.getBodyData(request: "/images/:cap")
|
||||
try self.save(image: data, for: cap)
|
||||
}
|
||||
// Update the classifier
|
||||
app.on(.POST, "classifier", ":version", body: .collect(maxSize: "50mb")) { request -> HTTPStatus in
|
||||
guard let version = request.parameters.get("version", as: Int.self) else {
|
||||
log("Invalid parameter for version")
|
||||
log("/classifier/:version: Invalid parameter for version")
|
||||
throw Abort(.badRequest)
|
||||
}
|
||||
guard version > self.classifierVersion else {
|
||||
@ -57,7 +57,7 @@ extension CapServer {
|
||||
|
||||
try self.ensureOperability()
|
||||
try authenticator.authorize(request)
|
||||
let data = try request.getBodyData()
|
||||
let data = try request.getBodyData(request: "/classifier/:version")
|
||||
try self.save(classifier: data, version: version)
|
||||
return .ok
|
||||
}
|
||||
@ -65,17 +65,17 @@ extension CapServer {
|
||||
// Update the trained classes
|
||||
app.postCatching("classes", ":date") { request in
|
||||
guard let dateString = request.parameters.get("date") else {
|
||||
log("Invalid parameter for date")
|
||||
log("/classes/:date: Invalid 'date' parameter")
|
||||
throw Abort(.badRequest)
|
||||
}
|
||||
guard let date = dateFormatter.date(from: dateString) else {
|
||||
log("Invalid date specification")
|
||||
log("/classes/:date: Invalid 'date' specification")
|
||||
throw Abort(.badRequest)
|
||||
}
|
||||
|
||||
try self.ensureOperability()
|
||||
try authenticator.authorize(request)
|
||||
let body = try request.getStringBody()
|
||||
let body = try request.getStringBody(request: "/classes/:date")
|
||||
|
||||
self.updateTrainedClasses(content: body)
|
||||
self.removeAllEntriesInImageChangeList(before: date)
|
||||
@ -91,23 +91,23 @@ extension CapServer {
|
||||
// Upload the thumbnail of a cap
|
||||
app.postCatching("thumbnails", ":cap") { request in
|
||||
guard let cap = request.parameters.get("cap", as: Int.self) else {
|
||||
log("Invalid cap parameter for thumbnail upload")
|
||||
log("/thumbnails/:cap: Invalid cap parameter for thumbnail upload")
|
||||
throw Abort(.badRequest)
|
||||
}
|
||||
try self.ensureOperability()
|
||||
try authenticator.authorize(request)
|
||||
let data = try request.getBodyData()
|
||||
let data = try request.getBodyData(request: "/thumbnails/:cap")
|
||||
self.saveThumbnail(data, for: cap)
|
||||
}
|
||||
|
||||
// Delete the image of a cap
|
||||
app.postCatching("delete", ":cap", ":version") { request in
|
||||
guard let cap = request.parameters.get("cap", as: Int.self) else {
|
||||
log("Invalid cap parameter for image deletion")
|
||||
log("/delete/:cap/:version: Invalid 'cap' parameter for image deletion")
|
||||
throw Abort(.badRequest)
|
||||
}
|
||||
guard let version = request.parameters.get("version", as: Int.self) else {
|
||||
log("Invalid version parameter for image deletion")
|
||||
log("/delete/:cap/:version: Invalid 'version' parameter for image deletion")
|
||||
throw Abort(.badRequest)
|
||||
}
|
||||
|
||||
@ -122,18 +122,18 @@ extension CapServer {
|
||||
|
||||
private extension Request {
|
||||
|
||||
func getBodyData() throws -> Data {
|
||||
func getBodyData(request: String) throws -> Data {
|
||||
guard let buffer = body.data else {
|
||||
log("Missing body data")
|
||||
log("\(request): Missing body data")
|
||||
throw CapError.invalidBody
|
||||
}
|
||||
return Data(buffer: buffer)
|
||||
}
|
||||
|
||||
func getStringBody() throws -> String {
|
||||
let data = try getBodyData()
|
||||
func getStringBody(request: String) throws -> String {
|
||||
let data = try getBodyData(request: request)
|
||||
guard let content = String(data: data, encoding: .utf8) else {
|
||||
log("Invalid string body")
|
||||
log("\(request): Invalid string body")
|
||||
throw CapError.invalidBody
|
||||
}
|
||||
return content
|
||||
|
@ -8,7 +8,7 @@ struct Config: Codable {
|
||||
/// The maximum size of the request body
|
||||
let maxBodySize: String
|
||||
|
||||
/// The path to the log file
|
||||
/// The path to the folder where the metric logs are stored
|
||||
let logPath: String
|
||||
|
||||
/// Serve files in the Public directory using Vapor
|
||||
|
@ -1,7 +1,7 @@
|
||||
import App
|
||||
import Vapor
|
||||
|
||||
var env = try Environment.detect()
|
||||
var env = Environment.production
|
||||
try LoggingSystem.bootstrap(from: &env)
|
||||
let app = Application(env)
|
||||
defer { app.shutdown() }
|
||||
|
Loading…
Reference in New Issue
Block a user