48 lines
1.7 KiB
Swift
48 lines
1.7 KiB
Swift
import Vapor
|
|
import Fluent
|
|
|
|
struct OpenApiController: RouteCollection {
|
|
func boot(routes: any RoutesBuilder) throws {
|
|
let group = routes.grouped("openapi", "service")
|
|
try group.register(collection: UtilController())
|
|
try group.register(collection: AuthController())
|
|
|
|
group.post("v3", "log", "writeSdkBasicLog") { req in
|
|
req.logger.log(level: .debug, .init(stringLiteral: req.body.string ?? ""))
|
|
return Response()
|
|
}
|
|
|
|
group.post("v3", "player", "heartbeat") { req in
|
|
let beat = try req.content.decode(Heartbeat.self, as: .json)
|
|
guard let zatToken = req.headers["zat"].first else {
|
|
throw Abort(.badRequest, reason: "Missing zat header.")
|
|
}
|
|
|
|
guard let session = try await Session.query(on: req.db).filter(\.$id == zatToken).first(),
|
|
let playerId = Int(beat.playerId),
|
|
session.$account.id == playerId else {
|
|
throw Abort(.unauthorized, reason: "zat invalid")
|
|
}
|
|
|
|
return "{}"
|
|
}
|
|
|
|
group.post("v3", "push", "token", "register") { req in
|
|
let beat = try req.content.decode(Heartbeat.self, as: .json)
|
|
guard let zatToken = req.headers["zat"].first else {
|
|
throw Abort(.badRequest, reason: "Missing zat header.")
|
|
}
|
|
guard let session = try await Session.query(on: req.db).filter(\.$id == zatToken).first(),
|
|
let playerId = Int(beat.playerId),
|
|
session.$account.id == playerId else {
|
|
throw Abort(.unauthorized, reason: "zat invalid")
|
|
}
|
|
return "{}"
|
|
}
|
|
}
|
|
}
|
|
|
|
struct Heartbeat: Content {
|
|
let playerId: String
|
|
}
|