AsyncRoutedHttpHandler 添加对协程的支持

This commit is contained in:
tursom 2019-12-14 17:16:53 +08:00
parent 024dd8c7cf
commit 597b94f12d
3 changed files with 107 additions and 68 deletions

View File

@ -16,6 +16,7 @@ import org.slf4j.LoggerFactory
import java.io.File
import java.io.RandomAccessFile
import java.lang.reflect.Method
import kotlin.reflect.KCallable
/**
* 自动添加路径映射的处理器
@ -97,11 +98,23 @@ open class RoutedHttpHandler(
}
protected fun insertMapping(obj: Any, method: Method) {
val mapping = obj::class.java.getAnnotation(Mapping::class.java)?.route ?: arrayOf("")
method.annotations.forEach { annotation ->
log?.info("method route {} annotation {}", method, annotation)
val (routes, router) = getRoutes(annotation) ?: return@forEach
log?.info("method route {} mapped to {}", method, routes)
routes.forEach { route ->
if (mapping.isEmpty()) {
addRouter(obj, method, route, router)
} else mapping.forEach {
val base = safeRoute(it)
addRouter(obj, method, base + route, router)
}
}
}
}
fun addRouter(obj: Any, method: Method, route: String, router: Router<Pair<Any?, (HttpContent) -> Unit>>) {
router[safeRoute(route)] = if (method.parameterTypes.isEmpty()) {
obj to when {
method.getAnnotation(Html::class.java) != null -> { content ->
@ -137,8 +150,6 @@ open class RoutedHttpHandler(
}
}
}
}
}
fun deleteRouter(route: String, method: String) {
getRouter(method).delRoute(safeRoute(route))
@ -231,15 +242,25 @@ open class RoutedHttpHandler(
null
}
fun <T> T.repeatUntil(state: (T) -> Boolean, block: (T) -> T): T {
var result = this
while (state(result)) {
result = block(result)
}
return result
}
fun safeRoute(route: String) = (
if (route.startsWith('/')) route else "/$route").let {
if (it.endsWith('/')) it.dropLast(1) else it
}
}.repeatUntil({ it.contains("//") }) { it.replace("//", "/") }
fun autoReturn(result: Any, content: HttpContent) {
log?.debug("{}: autoReturn: {}", content.clientIp, result)
when (result) {
is String -> content.finishText(result.toByteArray())
is StringBuilder -> content.finishText(result.toString().toByteArray())
is StringBuffer -> content.finishText(result.toString().toByteArray())
is ByteArray -> content.finishText(result)
is File -> {
content.autoContextType(result.name)

View File

@ -0,0 +1,7 @@
dependencies {
implementation project(":web")
api project(":json")
api group: 'org.slf4j', name: 'slf4j-api', version: '1.7.29'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.2.1'
compile group: 'org.jetbrains.kotlin', name: 'kotlin-reflect', version: '1.3.61'
}

View File

@ -80,13 +80,25 @@ open class AsyncRoutedHttpHandler(
}
}
@Suppress("UNCHECKED_CAST")
protected fun insertMapping(obj: Any, method: KCallable<*>) {
val mapping = obj::class.java.getAnnotation(Mapping::class.java)?.route ?: arrayOf("")
method.annotations.forEach { annotation ->
log?.info("method route {} annotation {}", method, annotation)
val (routes, router) = getAsyncRoutes(annotation) ?: return@forEach
log?.info("method route {} mapped to {}", method, routes)
routes.forEach { route ->
if (mapping.isEmpty()) {
addRouter(obj, method, route, router)
} else mapping.forEach {
val base = safeRoute(it)
addRouter(obj, method, base + route, router)
}
}
}
}
@Suppress("UNCHECKED_CAST")
fun addRouter(obj: Any, method: KCallable<*>, route: String, router: Router<Pair<Any?, suspend (HttpContent) -> Unit>>) {
router[safeRoute(route)] = if (method.parameters.size == 1) {
obj to when {
method.findAnnotation<Html>() != null -> { content ->
@ -122,8 +134,7 @@ open class AsyncRoutedHttpHandler(
}
}
}
}
}
protected fun getAsyncRoutes(annotation: Annotation) = when (annotation) {
is Mapping -> {