73 lines
2.5 KiB
Swift
Executable File
73 lines
2.5 KiB
Swift
Executable File
import Vapor
|
|
|
|
/// Register your application's routes here.
|
|
///
|
|
/// [Learn More →](https://docs.vapor.codes/3.0/getting-started/structure/#routesswift)
|
|
func routes(_ app: Application) throws {
|
|
|
|
// Get the name of a cap
|
|
app.getCatching("name", ":n") { request -> Data in
|
|
guard let cap = request.parameters.get("n", as: Int.self) else {
|
|
log("Invalid body data")
|
|
throw Abort(.badRequest)
|
|
}
|
|
return try server.name(for: cap)
|
|
}
|
|
|
|
// Set the name of a cap
|
|
app.postCatching("name", ":n") { request in
|
|
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, let name = String(data: Data(buffer: buffer), encoding: .utf8) else {
|
|
log("Invalid body data")
|
|
throw CapError.invalidBody
|
|
}
|
|
try server.set(name: name, for: cap)
|
|
}
|
|
|
|
// Upload an image
|
|
app.postCatching("images", ":n") { request -> Data in
|
|
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 {
|
|
log("Invalid body data")
|
|
throw CapError.invalidBody
|
|
}
|
|
let data = Data(buffer: buffer)
|
|
let newCount = try server.save(image: data, for: cap)
|
|
return "\(newCount)".data(using: .utf8)!
|
|
}
|
|
|
|
// Get count of a cap
|
|
app.getCatching("count", ":c") { request -> Data in
|
|
guard let cap = request.parameters.get("c", as: Int.self) else {
|
|
log("Invalid parameter for cap")
|
|
throw Abort(.badRequest)
|
|
}
|
|
let c = try server.count(of: cap)
|
|
return "\(c)".data(using: .utf8)!
|
|
}
|
|
|
|
// Get the count of all caps
|
|
app.getCatching("counts") { request -> Data in
|
|
try server.getCountData()
|
|
}
|
|
|
|
// Set a different version as the main image
|
|
app.getCatching("switch", ":n", ":v") { request in
|
|
guard let cap = request.parameters.get("n", as: Int.self) else {
|
|
log("Invalid parameter for cap")
|
|
throw Abort(.badRequest)
|
|
}
|
|
guard let version = request.parameters.get("v", as: Int.self) else {
|
|
log("Invalid parameter for cap version")
|
|
throw Abort(.badRequest)
|
|
}
|
|
try server.switchMainImage(to: version, for: cap)
|
|
}
|
|
}
|