|
5 | 5 | */ |
6 | 6 | package io.jooby |
7 | 7 |
|
8 | | -import kotlinx.coroutines.CoroutineExceptionHandler |
9 | | -import kotlinx.coroutines.CoroutineScope |
10 | | -import kotlinx.coroutines.CoroutineStart |
11 | | -import kotlinx.coroutines.asCoroutineDispatcher |
12 | | -import kotlinx.coroutines.launch |
| 8 | +import io.jooby.Router.* |
| 9 | +import kotlinx.coroutines.* |
13 | 10 | import kotlin.coroutines.CoroutineContext |
14 | 11 |
|
15 | | -internal class RouterCoroutineScope(coroutineContext: CoroutineContext) : CoroutineScope { |
16 | | - override val coroutineContext = coroutineContext |
17 | | -} |
| 12 | +internal class RouterCoroutineScope(override val coroutineContext: CoroutineContext) : CoroutineScope |
18 | 13 |
|
19 | 14 | class CoroutineRouter(val coroutineStart: CoroutineStart, val router: Router) { |
20 | 15 |
|
21 | 16 | val coroutineScope: CoroutineScope by lazy { |
22 | 17 | RouterCoroutineScope(router.worker.asCoroutineDispatcher()) |
23 | 18 | } |
24 | 19 |
|
25 | | - @RouterDsl |
26 | | - fun get(pattern: String, handler: suspend HandlerContext.() -> Any): Route { |
27 | | - return route(Router.GET, pattern, handler) |
| 20 | + private var extendCoroutineContext: (CoroutineContext) -> CoroutineContext = { it } |
| 21 | + fun launchContext(block: (CoroutineContext) -> CoroutineContext) { |
| 22 | + extendCoroutineContext = block |
28 | 23 | } |
29 | 24 |
|
30 | 25 | @RouterDsl |
31 | | - fun post(pattern: String, handler: suspend HandlerContext.() -> Any): Route { |
32 | | - return route(Router.POST, pattern, handler) |
33 | | - } |
| 26 | + fun get(pattern: String, handler: suspend HandlerContext.() -> Any) = |
| 27 | + route(GET, pattern, handler) |
34 | 28 |
|
35 | 29 | @RouterDsl |
36 | | - fun put(pattern: String, handler: suspend HandlerContext.() -> Any): Route { |
37 | | - return route(Router.PUT, pattern, handler) |
38 | | - } |
| 30 | + fun post(pattern: String, handler: suspend HandlerContext.() -> Any) = |
| 31 | + route(POST, pattern, handler) |
39 | 32 |
|
40 | 33 | @RouterDsl |
41 | | - fun delete(pattern: String, handler: suspend HandlerContext.() -> Any): Route { |
42 | | - return route(Router.DELETE, pattern, handler) |
43 | | - } |
| 34 | + fun put(pattern: String, handler: suspend HandlerContext.() -> Any) = |
| 35 | + route(PUT, pattern, handler) |
44 | 36 |
|
45 | 37 | @RouterDsl |
46 | | - fun patch(pattern: String, handler: suspend HandlerContext.() -> Any): Route { |
47 | | - return route(Router.PATCH, pattern, handler) |
48 | | - } |
| 38 | + fun delete(pattern: String, handler: suspend HandlerContext.() -> Any) = |
| 39 | + route(DELETE, pattern, handler) |
49 | 40 |
|
50 | 41 | @RouterDsl |
51 | | - fun head(pattern: String, handler: suspend HandlerContext.() -> Any): Route { |
52 | | - return route(Router.HEAD, pattern, handler) |
53 | | - } |
| 42 | + fun patch(pattern: String, handler: suspend HandlerContext.() -> Any) = |
| 43 | + route(PATCH, pattern, handler) |
54 | 44 |
|
55 | 45 | @RouterDsl |
56 | | - fun trace(pattern: String, handler: suspend HandlerContext.() -> Any): Route { |
57 | | - return route(Router.TRACE, pattern, handler) |
58 | | - } |
| 46 | + fun head(pattern: String, handler: suspend HandlerContext.() -> Any) = |
| 47 | + route(HEAD, pattern, handler) |
59 | 48 |
|
60 | 49 | @RouterDsl |
61 | | - fun options(pattern: String, handler: suspend HandlerContext.() -> Any): Route { |
62 | | - return route(Router.OPTIONS, pattern, handler) |
63 | | - } |
| 50 | + fun trace(pattern: String, handler: suspend HandlerContext.() -> Any) = |
| 51 | + route(TRACE, pattern, handler) |
64 | 52 |
|
65 | | - fun route(method: String, pattern: String, handler: suspend HandlerContext.() -> Any): Route { |
66 | | - return router.route(method, pattern) { ctx -> |
67 | | - val xhandler = CoroutineExceptionHandler { _, x -> |
68 | | - ctx.sendError(x) |
69 | | - } |
70 | | - coroutineScope.launch(xhandler, coroutineStart) { |
| 53 | + @RouterDsl |
| 54 | + fun options(pattern: String, handler: suspend HandlerContext.() -> Any) = |
| 55 | + route(OPTIONS, pattern, handler) |
| 56 | + |
| 57 | + fun route(method: String, pattern: String, handler: suspend HandlerContext.() -> Any): Route = |
| 58 | + router.route(method, pattern) { ctx -> |
| 59 | + launch(ctx) { |
71 | 60 | val result = handler(HandlerContext(ctx)) |
72 | 61 | if (result != ctx) { |
73 | 62 | ctx.render(result) |
74 | 63 | } |
75 | 64 | } |
76 | | - }.setHandle(handler) |
77 | | - .attribute("coroutine", true) |
| 65 | + }.setHandle(handler).attribute("coroutine", true) |
| 66 | + |
| 67 | + internal fun launch(ctx: Context, block: suspend CoroutineScope.() -> Unit) { |
| 68 | + val exceptionHandler = CoroutineExceptionHandler { _, x -> ctx.sendError(x) } |
| 69 | + coroutineScope.launch(extendCoroutineContext(exceptionHandler), coroutineStart, block) |
78 | 70 | } |
79 | 71 | } |
0 commit comments