Caps-Server/Sources/App/routes.swift

83 lines
2.7 KiB
Swift
Raw Normal View History

import Vapor
2022-06-24 11:31:09 +02:00
import Foundation
2020-05-19 15:19:19 +02:00
/// The decoder to extract caps from JSON payloads given to the `cap` route.
private let decoder = JSONDecoder()
2022-05-27 09:25:41 +02:00
private func authorize(_ request: Request) throws {
2022-06-11 00:20:43 +02:00
guard let key = request.headers.first(name: "key") else {
2022-06-24 11:31:09 +02:00
throw Abort(.badRequest) // 400
2022-06-11 00:20:43 +02:00
}
2022-05-27 09:25:41 +02:00
guard server.hasAuthorization(for: key) else {
2022-06-24 11:31:09 +02:00
throw Abort(.forbidden) // 403
2022-05-27 09:25:41 +02:00
}
}
2023-01-11 18:29:32 +01:00
func routes(_ app: Application) {
2022-06-23 22:48:58 +02:00
2022-06-24 00:23:02 +02:00
app.get("version") { _ in
"\(server.classifierVersion)"
2022-06-23 22:48:58 +02:00
}
// Add or change a cap
app.postCatching("cap") { request in
2022-05-27 09:25:41 +02:00
try authorize(request)
guard let buffer = request.body.data else {
2020-09-20 11:36:35 +02:00
log("Invalid body data")
2020-05-19 15:19:19 +02:00
throw CapError.invalidBody
}
let cap = try decoder.decode(Cap.self, from: buffer)
try server.addOrUpdate(cap)
2020-05-19 15:19:19 +02:00
}
// Upload an image
app.postCatching("images", ":n") { request in
2022-05-27 09:25:41 +02:00
try authorize(request)
2020-09-20 11:36:35 +02:00
guard let cap = request.parameters.get("n", as: Int.self) else {
log("Invalid parameter for cap")
throw Abort(.badRequest)
}
guard let buffer = request.body.data else {
2022-06-11 00:46:37 +02:00
log("Missing body data: \(request.body.description)")
2020-05-19 15:19:19 +02:00
throw CapError.invalidBody
}
2020-09-20 11:36:35 +02:00
let data = Data(buffer: buffer)
try server.save(image: data, for: cap)
}
2022-06-23 22:48:58 +02:00
// Update the classifier
2022-06-24 11:31:09 +02:00
app.on(.POST, "classifier", ":version", body: .collect(maxSize: "50mb")) { request -> HTTPStatus in
2022-06-23 22:48:58 +02:00
try authorize(request)
guard let version = request.parameters.get("version", as: Int.self) else {
log("Invalid parameter for version")
throw Abort(.badRequest)
}
guard version > server.classifierVersion else {
2022-06-24 11:31:09 +02:00
throw Abort(.alreadyReported) // 208
2022-06-23 22:48:58 +02:00
}
guard let buffer = request.body.data else {
log("Missing body data: \(request.body.description)")
throw CapError.invalidBody
}
let data = Data(buffer: buffer)
2022-06-24 11:31:09 +02:00
try server.save(classifier: data, version: version)
return .ok
2022-06-23 22:48:58 +02:00
}
// Update the trained classes
app.postCatching("classes") { request in
try authorize(request)
guard let buffer = request.body.data else {
log("Missing body data: \(request.body.description)")
throw CapError.invalidBody
}
let data = Data(buffer: buffer)
guard let content = String(data: data, encoding: .utf8) else {
log("Invalid string body: \(request.body.description)")
throw CapError.invalidBody
}
server.updateTrainedClasses(content: content)
server.emptyChangedImageListFile()
2023-01-14 23:04:29 +01:00
}
}