From 6938083463b57a0cf83e354a87b1b812e419e726 Mon Sep 17 00:00:00 2001 From: Azalea <22280294+hykilpikonna@users.noreply.github.com> Date: Thu, 26 Dec 2024 07:16:29 -0500 Subject: [PATCH] [+] Special handler --- .../sega/chusan/ChusanServletController.kt | 24 ++++++++++++------- .../samnyan/aqua/sega/general/BaseHandler.kt | 9 +++++++ 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/main/java/icu/samnyan/aqua/sega/chusan/ChusanServletController.kt b/src/main/java/icu/samnyan/aqua/sega/chusan/ChusanServletController.kt index e59d4793..8c5af98d 100644 --- a/src/main/java/icu/samnyan/aqua/sega/chusan/ChusanServletController.kt +++ b/src/main/java/icu/samnyan/aqua/sega/chusan/ChusanServletController.kt @@ -6,8 +6,12 @@ import icu.samnyan.aqua.sega.chusan.handler.* import icu.samnyan.aqua.sega.chusan.model.Chu3Repos import icu.samnyan.aqua.sega.chusan.model.request.UserCMissionResp import icu.samnyan.aqua.sega.general.BaseHandler +import icu.samnyan.aqua.sega.general.RequestContext +import icu.samnyan.aqua.sega.general.SpecialHandler +import icu.samnyan.aqua.sega.general.toSpecial import icu.samnyan.aqua.sega.util.jackson.StringMapper import icu.samnyan.aqua.spring.Metrics +import jakarta.servlet.http.HttpServletRequest import org.slf4j.LoggerFactory import org.springframework.web.bind.annotation.RestController import kotlin.collections.set @@ -79,30 +83,32 @@ class ChusanServletController( .map { it.split("/").last() }.toSet() // Fun! - val initH = mutableMapOf() - operator fun String.invoke(fn: (Map) -> Any) = initH.set(this.lowercase(), BaseHandler(fn)) + val initH = mutableMapOf() + infix fun String.special(fn: SpecialHandler) = initH.set(this.lowercase(), fn) + operator fun String.invoke(fn: (Map) -> Any) = this special { fn(it.data) } infix fun String.user(fn: (Map, Long) -> Any) = this { fn(it, parsing { it["userId"]!!.long }) } infix fun String.static(fn: () -> Any) = mapper.write(fn()).let { resp -> this { resp } } val meow = init() val members = this::class.declaredMemberProperties - val handlers: Map = endpointList.associateWith { api -> + val handlers: Map = endpointList.associateWith { api -> val name = api.replace("Api", "").replace("MatchingServer/", "").lowercase() initH[name]?.let { return@associateWith it } (members.find { it.name.lowercase() == name } ?: members.find { it.name.lowercase() == name.replace("cm", "") }) - ?.let { it.call(this) as BaseHandler } + ?.let { (it.call(this) as BaseHandler).toSpecial() } ?: throw IllegalArgumentException("Chu3: No handler found for $api") } @API("/{endpoint}", "/MatchingServer/{endpoint}") - fun handle(@PV endpoint: Str, @RB request: MutableMap, @PV version: Str): Any { + fun handle(@PV endpoint: Str, @RB data: MutableMap, @PV version: Str, req: HttpServletRequest): Any { + val ctx = RequestContext(req, data) var api = endpoint - request["version"] = version + data["version"] = version // Export version if (api.endsWith("C3Exp")) { api = api.removeSuffix("C3Exp") - request["c3exp"] = true + data["c3exp"] = true } if (api in matchingEndpoints) api = "MatchingServer/$api" @@ -117,11 +123,11 @@ class ChusanServletController( logger.info("Chu3 > $api no-op") return """{"returnCode":"1"}""" } - logger.info("Chu3 < $api : $request") + logger.info("Chu3 < $api : $data") return try { Metrics.timer("aquadx_chusan_api_latency", "api" to api).recordCallable { - handlers[api]!!.handle(request).let { if (it is String) it else mapper.write(it) }.also { + handlers[api]!!(ctx).let { if (it is String) it else mapper.write(it) }.also { if (api !in setOf("GetUserItemApi", "GetGameEventApi")) logger.info("Chu3 > $api : $it") } diff --git a/src/main/java/icu/samnyan/aqua/sega/general/BaseHandler.kt b/src/main/java/icu/samnyan/aqua/sega/general/BaseHandler.kt index d883a6fb..84586267 100644 --- a/src/main/java/icu/samnyan/aqua/sega/general/BaseHandler.kt +++ b/src/main/java/icu/samnyan/aqua/sega/general/BaseHandler.kt @@ -1,6 +1,7 @@ package icu.samnyan.aqua.sega.general import com.fasterxml.jackson.core.JsonProcessingException +import jakarta.servlet.http.HttpServletRequest /** * @author samnyan (privateamusement@protonmail.com) @@ -9,3 +10,11 @@ fun interface BaseHandler { @Throws(JsonProcessingException::class) fun handle(request: Map): Any? } + +data class RequestContext( + val req: HttpServletRequest, + val data: Map, +) + +typealias SpecialHandler = (RequestContext) -> Any? +fun BaseHandler.toSpecial() = { ctx: RequestContext -> handle(ctx.data) }