[O] Make handlers abstract

This commit is contained in:
Azalea
2024-12-27 16:03:02 -05:00
parent fa45891af4
commit ae6ff97b62
3 changed files with 119 additions and 114 deletions

View File

@@ -5,9 +5,7 @@ import ext.long
import ext.parsing
import jakarta.servlet.http.HttpServletRequest
/**
* @author samnyan (privateamusement@protonmail.com)
*/
fun interface BaseHandler {
@Throws(JsonProcessingException::class)
fun handle(request: Map<String, Any>): Any?
@@ -22,3 +20,11 @@ data class RequestContext(
typealias SpecialHandler = RequestContext.() -> Any?
fun BaseHandler.toSpecial() = { ctx: RequestContext -> handle(ctx.data) }
// A very :3 way of declaring APIs
abstract class MeowApi(val serialize: (String, Any?) -> String) {
val initH = mutableMapOf<String, SpecialHandler>()
infix operator fun String.invoke(fn: SpecialHandler) = initH.set("${this}Api", fn)
infix fun List<String>.all(fn: SpecialHandler) = forEach { it(fn) }
infix fun String.static(fn: () -> Any) = serialize(this, fn()).let { resp -> this { resp } }
}