update ktlint rules
This commit is contained in:
@@ -4,17 +4,17 @@ import io.ktor.server.application.Application
|
||||
import org.koin.ktor.ext.get
|
||||
|
||||
fun Application.configure() {
|
||||
configureKoin()
|
||||
configureKoin()
|
||||
|
||||
configureSecurity()
|
||||
configureSecurity()
|
||||
|
||||
configureSerialization()
|
||||
configureSerialization()
|
||||
|
||||
configureWebSockets()
|
||||
declareWebSocketsGameRoute(get(), get())
|
||||
configureWebSockets()
|
||||
declareWebSocketsGameRoute(get(), get())
|
||||
|
||||
configureHttpRouting()
|
||||
declareHttpGameRoute()
|
||||
configureHttpRouting()
|
||||
declareHttpGameRoute()
|
||||
|
||||
configureGameListener()
|
||||
configureGameListener()
|
||||
}
|
||||
|
||||
@@ -21,43 +21,43 @@ private val jwtIssuer = "PlayCardGame"
|
||||
private val jwtSecret = "secret"
|
||||
|
||||
fun Application.configureSecurity() {
|
||||
authentication {
|
||||
jwt {
|
||||
realm = jwtRealm
|
||||
verifier(
|
||||
JWT
|
||||
.require(Algorithm.HMAC256(jwtSecret))
|
||||
.withIssuer(jwtIssuer)
|
||||
.build(),
|
||||
)
|
||||
validate { credential ->
|
||||
if (credential.payload.getClaim("username").asString() != "") {
|
||||
JWTPrincipal(credential.payload)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
challenge { defaultScheme, realm ->
|
||||
call.respond(HttpStatusCode.Unauthorized, "Token is not valid or has expired")
|
||||
}
|
||||
authentication {
|
||||
jwt {
|
||||
realm = jwtRealm
|
||||
verifier(
|
||||
JWT
|
||||
.require(Algorithm.HMAC256(jwtSecret))
|
||||
.withIssuer(jwtIssuer)
|
||||
.build(),
|
||||
)
|
||||
validate { credential ->
|
||||
if (credential.payload.getClaim("username").asString() != "") {
|
||||
JWTPrincipal(credential.payload)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
challenge { defaultScheme, realm ->
|
||||
call.respond(HttpStatusCode.Unauthorized, "Token is not valid or has expired")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
routing {
|
||||
post("login/{username}") {
|
||||
val username = call.parameters["username"]!!
|
||||
val player = Player(name = username)
|
||||
routing {
|
||||
post("login/{username}") {
|
||||
val username = call.parameters["username"]!!
|
||||
val player = Player(name = username)
|
||||
|
||||
call.respond(hashMapOf("token" to player.makeJwt()))
|
||||
}
|
||||
call.respond(hashMapOf("token" to player.makeJwt()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun Player.makeJwt(): String =
|
||||
JWT
|
||||
.create()
|
||||
.withIssuer(jwtIssuer)
|
||||
.withClaim("username", name)
|
||||
.withPayload(Json.encodeToString(this))
|
||||
.withExpiresAt(Date(System.currentTimeMillis() + 60000))
|
||||
.sign(Algorithm.HMAC256(jwtSecret))
|
||||
JWT
|
||||
.create()
|
||||
.withIssuer(jwtIssuer)
|
||||
.withClaim("username", name)
|
||||
.withPayload(Json.encodeToString(this))
|
||||
.withExpiresAt(Date(System.currentTimeMillis() + 60000))
|
||||
.sign(Algorithm.HMAC256(jwtSecret))
|
||||
|
||||
@@ -23,30 +23,30 @@ import org.koin.ktor.plugin.Koin
|
||||
import org.koin.logger.slf4jLogger
|
||||
|
||||
fun Application.configureKoin() {
|
||||
install(Koin) {
|
||||
slf4jLogger()
|
||||
modules(appKoinModule)
|
||||
}
|
||||
install(Koin) {
|
||||
slf4jLogger()
|
||||
modules(appKoinModule)
|
||||
}
|
||||
}
|
||||
|
||||
val appKoinModule =
|
||||
module {
|
||||
single {
|
||||
GameEventBus(EventBusInMemory())
|
||||
}
|
||||
single {
|
||||
GameEventStore(EventStoreInMemory())
|
||||
}
|
||||
single {
|
||||
GameStateRepository(get(), get(), snapshotConfig = SnapshotConfig())
|
||||
}
|
||||
single {
|
||||
CommandStreamChannelBuilder<GameCommand>()
|
||||
}
|
||||
|
||||
singleOf(::VersionBuilderLocal) bind VersionBuilder::class
|
||||
singleOf(::GameEventHandler)
|
||||
singleOf(::GameCommandRunner)
|
||||
singleOf(::GameCommandHandler)
|
||||
singleOf(::PlayerNotificationEventListener)
|
||||
module {
|
||||
single {
|
||||
GameEventBus(EventBusInMemory())
|
||||
}
|
||||
single {
|
||||
GameEventStore(EventStoreInMemory())
|
||||
}
|
||||
single {
|
||||
GameStateRepository(get(), get(), snapshotConfig = SnapshotConfig())
|
||||
}
|
||||
single {
|
||||
CommandStreamChannelBuilder<GameCommand>()
|
||||
}
|
||||
|
||||
singleOf(::VersionBuilderLocal) bind VersionBuilder::class
|
||||
singleOf(::GameEventHandler)
|
||||
singleOf(::GameCommandRunner)
|
||||
singleOf(::GameCommandHandler)
|
||||
singleOf(::PlayerNotificationEventListener)
|
||||
}
|
||||
|
||||
@@ -5,6 +5,6 @@ import io.ktor.server.application.Application
|
||||
import org.koin.ktor.ext.get
|
||||
|
||||
fun Application.configureGameListener() {
|
||||
ReactionEventListener(get(), get(), get())
|
||||
.init()
|
||||
ReactionEventListener(get(), get(), get())
|
||||
.init()
|
||||
}
|
||||
|
||||
@@ -12,41 +12,41 @@ import io.ktor.server.resources.Resources
|
||||
import io.ktor.server.response.respondText
|
||||
|
||||
fun Application.configureHttpRouting() {
|
||||
install(CORS) {
|
||||
allowMethod(HttpMethod.Options)
|
||||
allowMethod(HttpMethod.Put)
|
||||
allowMethod(HttpMethod.Post)
|
||||
allowMethod(HttpMethod.Delete)
|
||||
allowMethod(HttpMethod.Patch)
|
||||
allowHeader(HttpHeaders.Authorization)
|
||||
allowHeader("MyCustomHeader")
|
||||
anyHost() // @TODO: Don't do this in production if possible. Try to limit it.
|
||||
install(CORS) {
|
||||
allowMethod(HttpMethod.Options)
|
||||
allowMethod(HttpMethod.Put)
|
||||
allowMethod(HttpMethod.Post)
|
||||
allowMethod(HttpMethod.Delete)
|
||||
allowMethod(HttpMethod.Patch)
|
||||
allowHeader(HttpHeaders.Authorization)
|
||||
allowHeader("MyCustomHeader")
|
||||
anyHost() // @TODO: Don't do this in production if possible. Try to limit it.
|
||||
}
|
||||
install(AutoHeadResponse)
|
||||
install(Resources)
|
||||
install(StatusPages) {
|
||||
exception<BadRequestException> { call, cause ->
|
||||
call.respondText(text = "400: $cause", status = HttpStatusCode.BadRequest)
|
||||
}
|
||||
install(AutoHeadResponse)
|
||||
install(Resources)
|
||||
install(StatusPages) {
|
||||
exception<BadRequestException> { call, cause ->
|
||||
call.respondText(text = "400: $cause", status = HttpStatusCode.BadRequest)
|
||||
}
|
||||
exception<Throwable> { call, cause ->
|
||||
call.respondText(text = "500: $cause", status = HttpStatusCode.InternalServerError)
|
||||
}
|
||||
exception<Throwable> { call, cause ->
|
||||
call.respondText(text = "500: $cause", status = HttpStatusCode.InternalServerError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class BadRequestException(
|
||||
val httpError: HttpErrorBadRequest,
|
||||
val httpError: HttpErrorBadRequest,
|
||||
) : Exception()
|
||||
|
||||
class HttpErrorBadRequest(
|
||||
statusCode: HttpStatusCode,
|
||||
val title: String = statusCode.description,
|
||||
val invalidParams: List<InvalidParam>,
|
||||
statusCode: HttpStatusCode,
|
||||
val title: String = statusCode.description,
|
||||
val invalidParams: List<InvalidParam>,
|
||||
) {
|
||||
val statusCode: Int = statusCode.value
|
||||
val statusCode: Int = statusCode.value
|
||||
|
||||
data class InvalidParam(
|
||||
val name: String,
|
||||
val reason: String,
|
||||
)
|
||||
data class InvalidParam(
|
||||
val name: String,
|
||||
val reason: String,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -18,72 +18,76 @@ import kotlinx.serialization.modules.SerializersModule
|
||||
import java.util.UUID
|
||||
|
||||
fun Application.configureSerialization() {
|
||||
install(ContentNegotiation) {
|
||||
json(
|
||||
defaultJsonSerializer(),
|
||||
)
|
||||
}
|
||||
install(ContentNegotiation) {
|
||||
json(
|
||||
defaultJsonSerializer(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun defaultJsonSerializer(): Json =
|
||||
Json {
|
||||
serializersModule =
|
||||
SerializersModule {
|
||||
contextual(UUID::class) { UUIDSerializer }
|
||||
contextual(GameId::class) { GameIdSerializer }
|
||||
contextual(CommandId::class) { CommandIdSerializer }
|
||||
contextual(Player.PlayerId::class) { PlayerIdSerializer }
|
||||
}
|
||||
}
|
||||
Json {
|
||||
serializersModule =
|
||||
SerializersModule {
|
||||
contextual(UUID::class) { UUIDSerializer }
|
||||
contextual(GameId::class) { GameIdSerializer }
|
||||
contextual(CommandId::class) { CommandIdSerializer }
|
||||
contextual(Player.PlayerId::class) { PlayerIdSerializer }
|
||||
}
|
||||
}
|
||||
|
||||
object CommandIdSerializer : KSerializer<CommandId> {
|
||||
override fun deserialize(decoder: Decoder): CommandId = CommandId(decoder.decodeString())
|
||||
override fun deserialize(decoder: Decoder): CommandId =
|
||||
CommandId(decoder.decodeString())
|
||||
|
||||
override fun serialize(
|
||||
encoder: Encoder,
|
||||
value: CommandId,
|
||||
) {
|
||||
encoder.encodeString(value.toString())
|
||||
}
|
||||
override fun serialize(
|
||||
encoder: Encoder,
|
||||
value: CommandId,
|
||||
) {
|
||||
encoder.encodeString(value.toString())
|
||||
}
|
||||
|
||||
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("CommandId", PrimitiveKind.STRING)
|
||||
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("CommandId", PrimitiveKind.STRING)
|
||||
}
|
||||
|
||||
object PlayerIdSerializer : KSerializer<Player.PlayerId> {
|
||||
override fun deserialize(decoder: Decoder): Player.PlayerId = Player.PlayerId(UUID.fromString(decoder.decodeString()))
|
||||
override fun deserialize(decoder: Decoder): Player.PlayerId =
|
||||
Player.PlayerId(UUID.fromString(decoder.decodeString()))
|
||||
|
||||
override fun serialize(
|
||||
encoder: Encoder,
|
||||
value: Player.PlayerId,
|
||||
) {
|
||||
encoder.encodeString(value.id.toString())
|
||||
}
|
||||
override fun serialize(
|
||||
encoder: Encoder,
|
||||
value: Player.PlayerId,
|
||||
) {
|
||||
encoder.encodeString(value.id.toString())
|
||||
}
|
||||
|
||||
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("PlayerId", PrimitiveKind.STRING)
|
||||
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("PlayerId", PrimitiveKind.STRING)
|
||||
}
|
||||
|
||||
object GameIdSerializer : KSerializer<GameId> {
|
||||
override fun deserialize(decoder: Decoder): GameId = GameId(UUID.fromString(decoder.decodeString()))
|
||||
override fun deserialize(decoder: Decoder): GameId =
|
||||
GameId(UUID.fromString(decoder.decodeString()))
|
||||
|
||||
override fun serialize(
|
||||
encoder: Encoder,
|
||||
value: GameId,
|
||||
) {
|
||||
encoder.encodeString(value.id.toString())
|
||||
}
|
||||
override fun serialize(
|
||||
encoder: Encoder,
|
||||
value: GameId,
|
||||
) {
|
||||
encoder.encodeString(value.id.toString())
|
||||
}
|
||||
|
||||
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("GameId", PrimitiveKind.STRING)
|
||||
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("GameId", PrimitiveKind.STRING)
|
||||
}
|
||||
|
||||
object UUIDSerializer : KSerializer<UUID> {
|
||||
override fun deserialize(decoder: Decoder): UUID = UUID.fromString(decoder.decodeString())
|
||||
override fun deserialize(decoder: Decoder): UUID =
|
||||
UUID.fromString(decoder.decodeString())
|
||||
|
||||
override fun serialize(
|
||||
encoder: Encoder,
|
||||
value: UUID,
|
||||
) {
|
||||
encoder.encodeString(value.toString())
|
||||
}
|
||||
override fun serialize(
|
||||
encoder: Encoder,
|
||||
value: UUID,
|
||||
) {
|
||||
encoder.encodeString(value.toString())
|
||||
}
|
||||
|
||||
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("UUID", PrimitiveKind.STRING)
|
||||
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("UUID", PrimitiveKind.STRING)
|
||||
}
|
||||
|
||||
@@ -8,10 +8,10 @@ import io.ktor.server.websocket.timeout
|
||||
import java.time.Duration
|
||||
|
||||
fun Application.configureWebSockets() {
|
||||
install(WebSockets) {
|
||||
pingPeriod = Duration.ofSeconds(15)
|
||||
timeout = Duration.ofSeconds(15)
|
||||
maxFrameSize = Long.MAX_VALUE
|
||||
masking = false
|
||||
}
|
||||
install(WebSockets) {
|
||||
pingPeriod = Duration.ofSeconds(15)
|
||||
timeout = Duration.ofSeconds(15)
|
||||
maxFrameSize = Long.MAX_VALUE
|
||||
masking = false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,10 +9,10 @@ import kotlinx.coroutines.DelicateCoroutinesApi
|
||||
|
||||
@OptIn(DelicateCoroutinesApi::class)
|
||||
fun Application.declareWebSocketsGameRoute(
|
||||
playerNotificationListener: PlayerNotificationEventListener,
|
||||
commandHandler: GameCommandHandler,
|
||||
playerNotificationListener: PlayerNotificationEventListener,
|
||||
commandHandler: GameCommandHandler,
|
||||
) {
|
||||
routing {
|
||||
gameSocket(playerNotificationListener, commandHandler)
|
||||
}
|
||||
routing {
|
||||
gameSocket(playerNotificationListener, commandHandler)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import io.ktor.server.routing.routing
|
||||
import org.koin.ktor.ext.get
|
||||
|
||||
fun Application.declareHttpGameRoute() {
|
||||
routing {
|
||||
readTheGameState(get())
|
||||
}
|
||||
routing {
|
||||
readTheGameState(get())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user