mirror of
https://github.com/tursom/TursomServer.git
synced 2025-03-03 22:30:09 +08:00
重构结构
This commit is contained in:
parent
f3ac61c346
commit
d008daef2d
@ -1,6 +1,6 @@
|
||||
dependencies {
|
||||
compileOnly 'com.alibaba:fastjson:1.2.62'
|
||||
compileOnly group: 'com.google.code.gson', name: 'gson', version: '2.8.6'
|
||||
compileOnly group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.10.1'
|
||||
compileOnly group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.10.1'
|
||||
api 'com.alibaba:fastjson:1.2.62'
|
||||
api group: 'com.google.code.gson', name: 'gson', version: '2.8.6'
|
||||
api group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.10.1'
|
||||
api group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.10.1'
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
dependencies {
|
||||
implementation group: 'org.jetbrains.kotlin', name: 'kotlin-reflect', version: '1.3.61'
|
||||
compileOnly group: 'org.slf4j', name: 'slf4j-api', version: '1.7.29'
|
||||
api group: 'org.slf4j', name: 'slf4j-api', version: '1.7.29'
|
||||
}
|
@ -161,7 +161,7 @@ class StringRadixTree<T> {
|
||||
return null
|
||||
}
|
||||
|
||||
fun listGet(route: String): List<Pair<T?, Int>>? {
|
||||
fun listGet(route: String): List<Pair<T?, Int>> {
|
||||
val context = Context(route)
|
||||
var node: Node<T>? = root
|
||||
val result = ArrayList<Pair<T?, Int>>()
|
||||
@ -272,15 +272,18 @@ class StringRadixTree<T> {
|
||||
// println()
|
||||
// }
|
||||
//
|
||||
// list.forEach {
|
||||
// tree[it] = null
|
||||
// println(tree)
|
||||
// println()
|
||||
// }
|
||||
// println(listOf<String>().last())
|
||||
// println(tree.listGet("roman"))
|
||||
//
|
||||
// list.forEachIndexed { index, s ->
|
||||
// tree[s] = index + 1
|
||||
// println(tree)
|
||||
// println()
|
||||
// }
|
||||
// //list.forEach {
|
||||
// // tree[it] = null
|
||||
// // println(tree)
|
||||
// // println()
|
||||
// //}
|
||||
// //
|
||||
// //list.forEachIndexed { index, s ->
|
||||
// // tree[s] = index + 1
|
||||
// // println(tree)
|
||||
// // println()
|
||||
// //}
|
||||
//}
|
||||
|
@ -1,5 +1,5 @@
|
||||
dependencies {
|
||||
implementation project(":")
|
||||
compileOnly project(":json")
|
||||
compileOnly group: 'org.slf4j', name: 'slf4j-api', version: '1.7.29'
|
||||
api project(":json")
|
||||
api group: 'org.slf4j', name: 'slf4j-api', version: '1.7.29'
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
package cn.tursom.web.netty
|
||||
|
||||
import cn.tursom.core.buffer.ByteBuffer
|
||||
import cn.tursom.web.AdvanceHttpContent
|
||||
import cn.tursom.web.MutableHttpContent
|
||||
import cn.tursom.web.utils.Chunked
|
||||
import io.netty.buffer.ByteBuf
|
||||
import io.netty.buffer.CompositeByteBuf
|
||||
@ -17,7 +17,7 @@ import kotlin.collections.set
|
||||
open class NettyHttpContent(
|
||||
val ctx: ChannelHandlerContext,
|
||||
val msg: FullHttpRequest
|
||||
) : AdvanceHttpContent, NettyResponseHeaderAdapter() {
|
||||
) : MutableHttpContent, NettyResponseHeaderAdapter() {
|
||||
override var finished: Boolean = false
|
||||
override val uri: String by lazy {
|
||||
var uri = msg.uri()
|
||||
|
@ -1,5 +1,5 @@
|
||||
package cn.tursom.web
|
||||
|
||||
interface AdvanceHttpContent : HttpContent {
|
||||
interface MutableHttpContent : HttpContent {
|
||||
fun addParam(key: String, value: String)
|
||||
}
|
@ -5,6 +5,7 @@ import cn.tursom.json.JsonWorkerImpl
|
||||
import cn.tursom.web.ExceptionContent
|
||||
import cn.tursom.web.HttpContent
|
||||
import cn.tursom.web.HttpHandler
|
||||
import cn.tursom.web.MutableHttpContent
|
||||
import cn.tursom.web.router.impl.SimpleRouter
|
||||
import cn.tursom.web.mapping.*
|
||||
import cn.tursom.web.result.Html
|
||||
@ -24,21 +25,25 @@ import java.lang.reflect.Method
|
||||
* 如果加了 @Mapping 注解,会依据注解提供的路由路径注册
|
||||
*/
|
||||
@Suppress("MemberVisibilityCanBePrivate", "unused")
|
||||
open class RoutedHttpHandler<T : HttpContent, in E : ExceptionContent>(
|
||||
open class RoutedHttpHandler(
|
||||
target: Any? = null,
|
||||
val routerMaker: () -> Router<(T) -> Unit> = { SimpleRouter() }
|
||||
) : HttpHandler<T, E> {
|
||||
protected val router: Router<(T) -> Unit> = routerMaker()
|
||||
protected val routerMap: HashMap<String, Router<(T) -> Unit>> = HashMap()
|
||||
val routerMaker: () -> Router<(HttpContent) -> Unit> = { SimpleRouter() }
|
||||
) : HttpHandler<HttpContent, ExceptionContent> {
|
||||
protected val router: Router<(HttpContent) -> Unit> = routerMaker()
|
||||
protected val routerMap: HashMap<String, Router<(HttpContent) -> Unit>> = HashMap()
|
||||
|
||||
init {
|
||||
@Suppress("LeakingThis")
|
||||
addRouter(target ?: this)
|
||||
}
|
||||
|
||||
override fun handle(content: T) = handle(content, getHandler(content.method, content.uri))
|
||||
override fun handle(content: HttpContent) = if (content is MutableHttpContent) {
|
||||
handle(content, getHandler(content, content.method, content.uri))
|
||||
} else {
|
||||
handle(content, getHandler(content.method, content.uri).first)
|
||||
}
|
||||
|
||||
open fun handle(content: T, handler: ((T) -> Unit)?) {
|
||||
open fun handle(content: HttpContent, handler: ((HttpContent) -> Unit)?) {
|
||||
if (handler != null) {
|
||||
handler(content)
|
||||
} else {
|
||||
@ -46,7 +51,7 @@ open class RoutedHttpHandler<T : HttpContent, in E : ExceptionContent>(
|
||||
}
|
||||
}
|
||||
|
||||
open fun notFound(content: T) {
|
||||
open fun notFound(content: HttpContent) {
|
||||
content.finish(404)
|
||||
}
|
||||
|
||||
@ -64,16 +69,28 @@ open class RoutedHttpHandler<T : HttpContent, in E : ExceptionContent>(
|
||||
}
|
||||
}
|
||||
|
||||
fun addRouter(route: String, handler: (T) -> Unit) {
|
||||
fun addRouter(route: String, handler: (HttpContent) -> Unit) {
|
||||
router[safeRoute(route)] = handler
|
||||
}
|
||||
|
||||
fun addRouter(method: String, route: String, handler: (T) -> Unit) {
|
||||
fun addRouter(method: String, route: String, handler: (HttpContent) -> Unit) {
|
||||
getRouter(method)[safeRoute(route)] = handler
|
||||
}
|
||||
|
||||
fun getHandler(method: String, route: String): ((T) -> Unit)? =
|
||||
getRouter(method)[route].first ?: this.router[route].first
|
||||
fun getHandler(content: MutableHttpContent, method: String, route: String): ((HttpContent) -> Unit)? {
|
||||
val router = getHandler(method, route)
|
||||
if (router.first != null) {
|
||||
router.second.forEach { (k, v) ->
|
||||
content.addParam(k, v)
|
||||
}
|
||||
}
|
||||
return router.first ?: this.router[route].first
|
||||
}
|
||||
|
||||
fun getHandler(method: String, route: String): Pair<((HttpContent) -> Unit)?, List<Pair<String, String>>> {
|
||||
val router = getRouter(method)[route]
|
||||
return if (router.first != null) router else this.router[route]
|
||||
}
|
||||
|
||||
fun deleteRouter(route: String, method: String) {
|
||||
getRouter(method).delRoute(safeRoute(route))
|
||||
@ -82,7 +99,7 @@ open class RoutedHttpHandler<T : HttpContent, in E : ExceptionContent>(
|
||||
protected fun insertMapping(obj: Any, method: Method) {
|
||||
method.annotations.forEach { annotation ->
|
||||
val routes: Array<out String>
|
||||
val router: Router<(T) -> Unit>
|
||||
val router: Router<(HttpContent) -> Unit>
|
||||
when (annotation) {
|
||||
is Mapping -> {
|
||||
routes = annotation.route
|
||||
@ -148,7 +165,7 @@ open class RoutedHttpHandler<T : HttpContent, in E : ExceptionContent>(
|
||||
}
|
||||
}
|
||||
|
||||
protected fun getRouter(method: String): Router<(T) -> Unit> = when {
|
||||
protected fun getRouter(method: String): Router<(HttpContent) -> Unit> = when {
|
||||
method.isEmpty() -> router
|
||||
else -> {
|
||||
val upperCaseMethod = method.toUpperCase()
|
||||
|
@ -1,15 +1,17 @@
|
||||
package cn.tursom.web.router.impl
|
||||
|
||||
import cn.tursom.core.datastruct.StringRadixTree
|
||||
import cn.tursom.web.router.Router
|
||||
|
||||
/**
|
||||
* 匹配类似
|
||||
* /java/{mod}
|
||||
* /java/{mod}/{id}
|
||||
* /java/a{mod}/id
|
||||
* /java/a{mod}_i/id
|
||||
* /java/:mod
|
||||
* /java/:mod/:id
|
||||
* 不支持在参数后加静态路径
|
||||
*/
|
||||
class CurlyBracesRouter<T> : Router<T> {
|
||||
private val router = StringRadixTree<Pair<T, List<String>>>()
|
||||
|
||||
override fun addSubRoute(route: String, value: T?, onDestroy: ((oldValue: T) -> Unit)?) {
|
||||
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
|
||||
}
|
||||
@ -19,6 +21,19 @@ class CurlyBracesRouter<T> : Router<T> {
|
||||
}
|
||||
|
||||
override fun get(route: String): Pair<T?, List<Pair<String, String>>> {
|
||||
val t = router.listGet(route)
|
||||
if (t.isEmpty()) {
|
||||
return null to listOf()
|
||||
}
|
||||
val pair = t.last()
|
||||
if (pair.first == null || pair.second != route.length) {
|
||||
return null to listOf()
|
||||
}
|
||||
|
||||
val list = ArrayList<Pair<String, String>>()
|
||||
pair.first!!.second.forEach {
|
||||
}
|
||||
|
||||
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
|
||||
}
|
||||
}
|
||||
@ -59,7 +74,7 @@ interface ICurlyBracesNode {
|
||||
|
||||
fun matchesAndParse(context: RouteContext): List<Pair<String, String>>? {
|
||||
val list = ArrayList<Pair<String, String>>()
|
||||
return if (matchesAndParse(context, list)) {
|
||||
return if (matchesAndParse(context, list as MutableList<Pair<String, String>>)) {
|
||||
list
|
||||
} else {
|
||||
null
|
||||
@ -78,4 +93,5 @@ class CurlyBracesNode(private val nodeContext: NodeContext) : ICurlyBracesNode {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class PlaceholderCurlyBracesNode
|
Loading…
Reference in New Issue
Block a user