1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
|
/*
* Copyright (c) 2017-present Robert Jaros
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package pl.treksoft.kvision.remote
import com.fasterxml.jackson.databind.module.SimpleModule
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import com.google.inject.Injector
import io.javalin.Javalin
import io.javalin.core.security.Role
import io.javalin.http.Context
import io.javalin.websocket.WsContext
import io.javalin.websocket.WsHandler
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.ReceiveChannel
import kotlinx.coroutines.channels.SendChannel
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.future.future
import kotlinx.coroutines.launch
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import pl.treksoft.kvision.types.*
import java.math.BigDecimal
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.LocalTime
import java.time.OffsetDateTime
import java.time.OffsetTime
import kotlin.reflect.KClass
/**
* Multiplatform service manager for Javalin.
*/
@Suppress("LargeClass", "TooManyFunctions", "BlockingMethodInNonBlockingContext")
actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: KClass<T>) {
companion object {
val LOG: Logger = LoggerFactory.getLogger(KVServiceManager::class.java.name)
const val KV_WS_INCOMING_KEY = "pl.treksoft.kvision.ws.incoming.key"
const val KV_WS_OUTGOING_KEY = "pl.treksoft.kvision.ws.outgoing.key"
}
val getRequests: MutableMap<String, (Context) -> Unit> = mutableMapOf()
val postRequests: MutableMap<String, (Context) -> Unit> = mutableMapOf()
val putRequests: MutableMap<String, (Context) -> Unit> = mutableMapOf()
val deleteRequests: MutableMap<String, (Context) -> Unit> =
mutableMapOf()
val optionsRequests: MutableMap<String, (Context) -> Unit> =
mutableMapOf()
val webSocketRequests: MutableMap<String, (WsHandler) -> Unit> =
mutableMapOf()
val mapper = jacksonObjectMapper().apply {
val module = SimpleModule()
module.addSerializer(LocalDateTime::class.java, LocalDateTimeSerializer())
module.addSerializer(LocalDate::class.java, LocalDateSerializer())
module.addSerializer(LocalTime::class.java, LocalTimeSerializer())
module.addSerializer(OffsetDateTime::class.java, OffsetDateTimeSerializer())
module.addSerializer(OffsetTime::class.java, OffsetTimeSerializer())
module.addSerializer(BigDecimal::class.java, BigDecimalSerializer())
module.addDeserializer(LocalDateTime::class.java, LocalDateTimeDeserializer())
module.addDeserializer(LocalDate::class.java, LocalDateDeserializer())
module.addDeserializer(LocalTime::class.java, LocalTimeDeserializer())
module.addDeserializer(OffsetDateTime::class.java, OffsetDateTimeDeserializer())
module.addDeserializer(OffsetTime::class.java, OffsetTimeDeserializer())
module.addDeserializer(BigDecimal::class.java, BigDecimalDeserializer())
this.registerModule(module)
}
var counter: Int = 0
/**
* @suppress internal function
*/
@Suppress("DEPRECATION")
fun initializeService(service: T, ctx: Context) {
if (service is WithContext) {
service.ctx = ctx
}
if (service is WithHttpSession) {
service.httpSession = ctx.req.session
}
}
/**
* @suppress internal function
*/
@Suppress("DEPRECATION")
fun initializeWsService(service: T, wsCtx: WsContext) {
if (service is WithWsContext) {
service.wsCtx = wsCtx
}
if (service is WithWsSession) {
service.wsSession = wsCtx.session
}
}
/**
* Binds a given route with a function of the receiver.
* @param function a function of the receiver
* @param method a HTTP method
* @param route a route
*/
@Suppress("TooGenericExceptionCaught")
protected actual inline fun <reified RET> bind(
noinline function: suspend T.() -> RET,
method: HttpMethod, route: String?
) {
val routeDef = route ?: "route${this::class.simpleName}${counter++}"
addRoute(method, "/kv/$routeDef") { ctx ->
val jsonRpcRequest = if (method == HttpMethod.GET) {
JsonRpcRequest(ctx.queryParam<Int>("id").get(), "", listOf())
} else {
ctx.body<JsonRpcRequest>()
}
val injector = ctx.attribute<Injector>(KV_INJECTOR_KEY)!!
val service = injector.getInstance(serviceClass.java)
initializeService(service, ctx)
val future = GlobalScope.future {
try {
val result = function.invoke(service)
JsonRpcResponse(
id = jsonRpcRequest.id,
result = mapper.writeValueAsString(result)
)
} catch (e: Exception) {
if (e !is ServiceException) LOG.error(e.message, e)
JsonRpcResponse(
id = jsonRpcRequest.id, error = e.message ?: "Error",
exceptionType = e.javaClass.canonicalName
)
}
}
ctx.json(future)
}
}
/**
* Binds a given route with a function of the receiver.
* @param function a function of the receiver
* @param method a HTTP method
* @param route a route
*/
@Suppress("TooGenericExceptionCaught")
protected actual inline fun <reified PAR, reified RET> bind(
noinline function: suspend T.(PAR) -> RET,
method: HttpMethod, route: String?
) {
if (method == HttpMethod.GET)
throw UnsupportedOperationException("GET method is only supported for methods without parameters")
val routeDef = route ?: "route${this::class.simpleName}${counter++}"
addRoute(method, "/kv/$routeDef") { ctx ->
val jsonRpcRequest = ctx.body<JsonRpcRequest>()
if (jsonRpcRequest.params.size == 1) {
val param = getParameter<PAR>(jsonRpcRequest.params[0])
val injector = ctx.attribute<Injector>(KV_INJECTOR_KEY)!!
val service = injector.getInstance(serviceClass.java)
initializeService(service, ctx)
val future = GlobalScope.future {
try {
val result = function.invoke(service, param)
JsonRpcResponse(
id = jsonRpcRequest.id,
result = mapper.writeValueAsString(result)
)
} catch (e: Exception) {
if (e !is ServiceException) LOG.error(e.message, e)
JsonRpcResponse(
id = jsonRpcRequest.id, error = e.message ?: "Error",
exceptionType = e.javaClass.canonicalName
)
}
}
ctx.json(future)
} else {
ctx.json(JsonRpcResponse(id = jsonRpcRequest.id, error = "Invalid parameters"))
}
}
}
/**
* Binds a given route with a function of the receiver.
* @param function a function of the receiver
* @param method a HTTP method
* @param route a route
*/
@Suppress("TooGenericExceptionCaught")
protected actual inline fun <reified PAR1, reified PAR2, reified RET> bind(
noinline function: suspend T.(PAR1, PAR2) -> RET,
method: HttpMethod, route: String?
) {
if (method == HttpMethod.GET)
throw UnsupportedOperationException("GET method is only supported for methods without parameters")
val routeDef = route ?: "route${this::class.simpleName}${counter++}"
addRoute(method, "/kv/$routeDef") { ctx ->
val jsonRpcRequest = ctx.body<JsonRpcRequest>()
if (jsonRpcRequest.params.size == 2) {
val param1 = getParameter<PAR1>(jsonRpcRequest.params[0])
val param2 = getParameter<PAR2>(jsonRpcRequest.params[1])
val injector = ctx.attribute<Injector>(KV_INJECTOR_KEY)!!
val service = injector.getInstance(serviceClass.java)
initializeService(service, ctx)
val future = GlobalScope.future {
try {
val result = function.invoke(service, param1, param2)
JsonRpcResponse(
id = jsonRpcRequest.id,
result = mapper.writeValueAsString(result)
)
} catch (e: Exception) {
if (e !is ServiceException) LOG.error(e.message, e)
JsonRpcResponse(
id = jsonRpcRequest.id, error = e.message ?: "Error",
exceptionType = e.javaClass.canonicalName
)
}
}
ctx.json(future)
} else {
ctx.json(JsonRpcResponse(id = jsonRpcRequest.id, error = "Invalid parameters"))
}
}
}
/**
* Binds a given route with a function of the receiver.
* @param function a function of the receiver
* @param method a HTTP method
* @param route a route
*/
@Suppress("TooGenericExceptionCaught")
protected actual inline fun <reified PAR1, reified PAR2, reified PAR3, reified RET> bind(
noinline function: suspend T.(PAR1, PAR2, PAR3) -> RET,
method: HttpMethod, route: String?
) {
if (method == HttpMethod.GET)
throw UnsupportedOperationException("GET method is only supported for methods without parameters")
val routeDef = route ?: "route${this::class.simpleName}${counter++}"
addRoute(method, "/kv/$routeDef") { ctx ->
val jsonRpcRequest = ctx.body<JsonRpcRequest>()
@Suppress("MagicNumber")
if (jsonRpcRequest.params.size == 3) {
val param1 = getParameter<PAR1>(jsonRpcRequest.params[0])
val param2 = getParameter<PAR2>(jsonRpcRequest.params[1])
val param3 = getParameter<PAR3>(jsonRpcRequest.params[2])
val injector = ctx.attribute<Injector>(KV_INJECTOR_KEY)!!
val service = injector.getInstance(serviceClass.java)
initializeService(service, ctx)
val future = GlobalScope.future {
try {
val result = function.invoke(service, param1, param2, param3)
JsonRpcResponse(
id = jsonRpcRequest.id,
result = mapper.writeValueAsString(result)
)
} catch (e: Exception) {
if (e !is ServiceException) LOG.error(e.message, e)
JsonRpcResponse(
id = jsonRpcRequest.id, error = e.message ?: "Error",
exceptionType = e.javaClass.canonicalName
)
}
}
ctx.json(future)
} else {
ctx.json(JsonRpcResponse(id = jsonRpcRequest.id, error = "Invalid parameters"))
}
}
}
/**
* Binds a given route with a function of the receiver.
* @param function a function of the receiver
* @param method a HTTP method
* @param route a route
*/
@Suppress("TooGenericExceptionCaught")
protected actual inline fun <reified PAR1, reified PAR2, reified PAR3, reified PAR4, reified RET> bind(
noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4) -> RET,
method: HttpMethod, route: String?
) {
if (method == HttpMethod.GET)
throw UnsupportedOperationException("GET method is only supported for methods without parameters")
val routeDef = route ?: "route${this::class.simpleName}${counter++}"
addRoute(method, "/kv/$routeDef") { ctx ->
val jsonRpcRequest = ctx.body<JsonRpcRequest>()
@Suppress("MagicNumber")
if (jsonRpcRequest.params.size == 4) {
val param1 = getParameter<PAR1>(jsonRpcRequest.params[0])
val param2 = getParameter<PAR2>(jsonRpcRequest.params[1])
val param3 = getParameter<PAR3>(jsonRpcRequest.params[2])
val param4 = getParameter<PAR4>(jsonRpcRequest.params[3])
val injector = ctx.attribute<Injector>(KV_INJECTOR_KEY)!!
val service = injector.getInstance(serviceClass.java)
initializeService(service, ctx)
val future = GlobalScope.future {
try {
val result = function.invoke(service, param1, param2, param3, param4)
JsonRpcResponse(
id = jsonRpcRequest.id,
result = mapper.writeValueAsString(result)
)
} catch (e: Exception) {
if (e !is ServiceException) LOG.error(e.message, e)
JsonRpcResponse(
id = jsonRpcRequest.id, error = e.message ?: "Error",
exceptionType = e.javaClass.canonicalName
)
}
}
ctx.json(future)
} else {
ctx.json(JsonRpcResponse(id = jsonRpcRequest.id, error = "Invalid parameters"))
}
}
}
/**
* Binds a given route with a function of the receiver.
* @param function a function of the receiver
* @param method a HTTP method
* @param route a route
*/
@Suppress("TooGenericExceptionCaught")
protected actual inline fun <reified PAR1, reified PAR2, reified PAR3,
reified PAR4, reified PAR5, reified RET> bind(
noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, PAR5) -> RET,
method: HttpMethod, route: String?
) {
if (method == HttpMethod.GET)
throw UnsupportedOperationException("GET method is only supported for methods without parameters")
val routeDef = route ?: "route${this::class.simpleName}${counter++}"
addRoute(method, "/kv/$routeDef") { ctx ->
val jsonRpcRequest = ctx.body<JsonRpcRequest>()
@Suppress("MagicNumber")
if (jsonRpcRequest.params.size == 5) {
val param1 = getParameter<PAR1>(jsonRpcRequest.params[0])
val param2 = getParameter<PAR2>(jsonRpcRequest.params[1])
val param3 = getParameter<PAR3>(jsonRpcRequest.params[2])
val param4 = getParameter<PAR4>(jsonRpcRequest.params[3])
val param5 = getParameter<PAR5>(jsonRpcRequest.params[4])
val injector = ctx.attribute<Injector>(KV_INJECTOR_KEY)!!
val service = injector.getInstance(serviceClass.java)
initializeService(service, ctx)
val future = GlobalScope.future {
try {
val result = function.invoke(service, param1, param2, param3, param4, param5)
JsonRpcResponse(
id = jsonRpcRequest.id,
result = mapper.writeValueAsString(result)
)
} catch (e: Exception) {
if (e !is ServiceException) LOG.error(e.message, e)
JsonRpcResponse(
id = jsonRpcRequest.id, error = e.message ?: "Error",
exceptionType = e.javaClass.canonicalName
)
}
}
ctx.json(future)
} else {
ctx.json(JsonRpcResponse(id = jsonRpcRequest.id, error = "Invalid parameters"))
}
}
}
/**
* Binds a given route with a function of the receiver.
* @param function a function of the receiver
* @param method a HTTP method
* @param route a route
*/
@Suppress("TooGenericExceptionCaught")
protected actual inline fun <reified PAR1, reified PAR2, reified PAR3,
reified PAR4, reified PAR5, reified PAR6, reified RET> bind(
noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, PAR5, PAR6) -> RET,
method: HttpMethod, route: String?
) {
if (method == HttpMethod.GET)
throw UnsupportedOperationException("GET method is only supported for methods without parameters")
val routeDef = route ?: "route${this::class.simpleName}${counter++}"
addRoute(method, "/kv/$routeDef") { ctx ->
val jsonRpcRequest = ctx.body<JsonRpcRequest>()
@Suppress("MagicNumber")
if (jsonRpcRequest.params.size == 6) {
val param1 = getParameter<PAR1>(jsonRpcRequest.params[0])
val param2 = getParameter<PAR2>(jsonRpcRequest.params[1])
val param3 = getParameter<PAR3>(jsonRpcRequest.params[2])
val param4 = getParameter<PAR4>(jsonRpcRequest.params[3])
val param5 = getParameter<PAR5>(jsonRpcRequest.params[4])
val param6 = getParameter<PAR6>(jsonRpcRequest.params[5])
val injector = ctx.attribute<Injector>(KV_INJECTOR_KEY)!!
val service = injector.getInstance(serviceClass.java)
initializeService(service, ctx)
val future = GlobalScope.future {
try {
val result = function.invoke(service, param1, param2, param3, param4, param5, param6)
JsonRpcResponse(
id = jsonRpcRequest.id,
result = mapper.writeValueAsString(result)
)
} catch (e: Exception) {
if (e !is ServiceException) LOG.error(e.message, e)
JsonRpcResponse(
id = jsonRpcRequest.id, error = e.message ?: "Error",
exceptionType = e.javaClass.canonicalName
)
}
}
ctx.json(future)
} else {
ctx.json(JsonRpcResponse(id = jsonRpcRequest.id, error = "Invalid parameters"))
}
}
}
/**
* Binds a given web socket connection with a function of the receiver.
* @param function a function of the receiver
* @param route a route
*/
@OptIn(ExperimentalCoroutinesApi::class)
protected actual inline fun <reified PAR1 : Any, reified PAR2 : Any> bind(
noinline function: suspend T.(ReceiveChannel<PAR1>, SendChannel<PAR2>) -> Unit,
route: String?
) {
val routeDef = route ?: "route${this::class.simpleName}${counter++}"
webSocketRequests["/kvws/$routeDef"] = { ws ->
ws.onConnect { ctx ->
val incoming = Channel<String>()
val outgoing = Channel<String>()
ctx.attribute(KV_WS_INCOMING_KEY, incoming)
ctx.attribute(KV_WS_OUTGOING_KEY, outgoing)
val injector = ctx.attribute<Injector>(KV_INJECTOR_KEY)!!
val service = injector.getInstance(serviceClass.java)
initializeWsService(service, ctx)
GlobalScope.launch {
coroutineScope {
launch(Dispatchers.IO) {
for (text in outgoing) {
ctx.send(text)
}
ctx.session.close()
}
launch {
val requestChannel = Channel<PAR1>()
val responseChannel = Channel<PAR2>()
coroutineScope {
launch {
for (p in incoming) {
val jsonRpcRequest = getParameter<JsonRpcRequest>(p)
if (jsonRpcRequest.params.size == 1) {
val par = getParameter<PAR1>(jsonRpcRequest.params[0])
requestChannel.send(par)
}
}
requestChannel.close()
}
launch(Dispatchers.IO) {
for (p in responseChannel) {
val text = mapper.writeValueAsString(
JsonRpcResponse(
id = 0,
result = mapper.writeValueAsString(p)
)
)
outgoing.send(text)
}
}
launch {
function.invoke(service, requestChannel, responseChannel)
if (!responseChannel.isClosedForReceive) responseChannel.close()
}
}
if (!outgoing.isClosedForReceive) outgoing.close()
}
}
}
}
ws.onClose { ctx ->
GlobalScope.launch {
val outgoing = ctx.attribute<Channel<String>>(KV_WS_OUTGOING_KEY)!!
val incoming = ctx.attribute<Channel<String>>(KV_WS_INCOMING_KEY)!!
outgoing.close()
incoming.close()
}
}
ws.onMessage { ctx ->
GlobalScope.launch {
val incoming = ctx.attribute<Channel<String>>(KV_WS_INCOMING_KEY)!!
incoming.send(ctx.message())
}
}
}
}
/**
* Binds a given function of the receiver as a tabulator component source
* @param function a function of the receiver
*/
@Suppress("TooGenericExceptionCaught")
protected actual inline fun <reified RET> bindTabulatorRemote(
noinline function: suspend T.(Int?, Int?, List<RemoteFilter>?, List<RemoteSorter>?, String?) -> RemoteData<RET>,
route: String?
) {
val routeDef = route ?: "route${this::class.simpleName}${counter++}"
addRoute(HttpMethod.POST, "/kv/$routeDef") { ctx ->
val jsonRpcRequest = ctx.body<JsonRpcRequest>()
@Suppress("MagicNumber")
if (jsonRpcRequest.params.size == 5) {
val param1 = getParameter<Int?>(jsonRpcRequest.params[0])
val param2 = getParameter<Int?>(jsonRpcRequest.params[1])
val param3 = getParameter<List<RemoteFilter>?>(jsonRpcRequest.params[2])
@Suppress("MagicNumber")
val param4 = getParameter<List<RemoteSorter>?>(jsonRpcRequest.params[3])
@Suppress("MagicNumber")
val param5 = getParameter<String?>(jsonRpcRequest.params[4])
val injector = ctx.attribute<Injector>(KV_INJECTOR_KEY)!!
val service = injector.getInstance(serviceClass.java)
initializeService(service, ctx)
val future = GlobalScope.future {
try {
val result = function.invoke(service, param1, param2, param3, param4, param5)
JsonRpcResponse(
id = jsonRpcRequest.id,
result = mapper.writeValueAsString(result)
)
} catch (e: Exception) {
if (e !is ServiceException) LOG.error(e.message, e)
JsonRpcResponse(
id = jsonRpcRequest.id, error = e.message ?: "Error",
exceptionType = e.javaClass.canonicalName
)
}
}
ctx.json(future)
} else {
ctx.json(JsonRpcResponse(id = jsonRpcRequest.id, error = "Invalid parameters"))
}
}
}
/**
* @suppress Internal method
*/
fun addRoute(
method: HttpMethod,
path: String,
handler: (Context) -> Unit
) {
when (method) {
HttpMethod.GET -> getRequests[path] = handler
HttpMethod.POST -> postRequests[path] = handler
HttpMethod.PUT -> putRequests[path] = handler
HttpMethod.DELETE -> deleteRequests[path] = handler
HttpMethod.OPTIONS -> optionsRequests[path] = handler
}
}
/**
* @suppress Internal method
*/
protected inline fun <reified T> getParameter(str: String?): T {
return str?.let {
if (T::class == String::class) {
str as T
} else {
mapper.readValue(str)
}
} ?: null as T
}
}
/**
* A function to generate routes based on definitions from the service manager.
*/
fun <T : Any> Javalin.applyRoutes(serviceManager: KVServiceManager<T>, roles: Set<Role> = setOf()) {
serviceManager.getRequests.forEach { (path, handler) ->
get(path, handler, roles)
}
serviceManager.postRequests.forEach { (path, handler) ->
post(path, handler, roles)
}
serviceManager.putRequests.forEach { (path, handler) ->
put(path, handler, roles)
}
serviceManager.deleteRequests.forEach { (path, handler) ->
delete(path, handler, roles)
}
serviceManager.optionsRequests.forEach { (path, handler) ->
options(path, handler, roles)
}
serviceManager.webSocketRequests.forEach { (path, handler) ->
ws(path, handler, roles)
}
}
|