FlurSchnaps-Server/Sources/App/routes.swift

37 lines
1001 B
Swift
Raw Normal View History

2022-09-04 15:01:35 +02:00
import Vapor
import BinaryCodable
private let responseEncoder = BinaryEncoder()
private let requestDecoder = BinaryDecoder()
var serverStatus: ServerStatus = .starting
var knownTokens = Set<String>()
func routes(_ app: Application) throws {
app.post("token") { req async throws -> HTTPResponseStatus in
guard let data = req.body.data?.allReadableBytes else {
return .badRequest
}
let tokenUpload: TokenUpload = try requestDecoder.decode(from: data)
if let oldToken = tokenUpload.previousToken {
knownTokens.remove(oldToken.hex)
}
knownTokens.insert(tokenUpload.currentToken.hex)
return .ok
}
app.get("status") { req async throws -> Response in
let data = try responseEncoder.encode(serverStatus)
return Response(status: .ok, body: .init(data: data))
}
}
private extension ByteBuffer {
var allReadableBytes: Data? {
getData(at: 0, length: readableBytes)
}
}