FlurSchnaps-Server/Sources/App/routes.swift
2022-09-05 18:10:42 +02:00

32 lines
840 B
Swift

import Vapor
import BinaryCodable
private let responseEncoder = BinaryEncoder()
private let requestDecoder = BinaryDecoder()
var serverStatus: ServerStatus = .starting
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)
tokenStorage.add(tokenUpload)
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)
}
}