All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.javalin.community.routing.dsl.DslRouting.kt Maven / Gradle / Ivy

package io.javalin.community.routing.dsl

import io.javalin.community.routing.dsl.defaults.DefaultDsl
import io.javalin.community.routing.dsl.defaults.DefaultDsl.DefaultConfiguration
import io.javalin.community.routing.dsl.defaults.DefaultDsl.DefaultScope
import io.javalin.community.routing.dsl.defaults.DefaultRoute
import io.javalin.community.routing.invokeAsSamWithReceiver
import io.javalin.community.routing.sortRoutes
import io.javalin.config.JavalinConfig
import io.javalin.http.HandlerType
import io.javalin.router.Endpoint
import io.javalin.router.InternalRouter
import io.javalin.router.RoutingApiInitializer
import io.javalin.router.RoutingSetupScope

open class DslRouting<
    CONFIG : RoutingDslConfiguration,
    ROUTE : DslRoute,
    CONTEXT,
    RESPONSE : Any
>(
    private val factory: RoutingDslFactory
) : RoutingApiInitializer {

    companion object {
        object Dsl : DslRouting(DefaultDsl)
    }

    override fun initialize(cfg: JavalinConfig, internalRouter: InternalRouter, setup: RoutingSetupScope) {
        val dslConfig = factory.createConfiguration()
        setup.invokeAsSamWithReceiver(dslConfig)

        dslConfig.routes
            .sortRoutes()
            .map { route -> route to factory.createHandler(route) }
            .forEach { (route, handler) ->
                internalRouter.addHttpEndpoint(
                    Endpoint(
                        method = HandlerType.valueOf(route.method.toString()),
                        path = route.path,
                        handler = handler
                    )
                )
            }

        dslConfig.exceptionHandlers.forEach { (exceptionClass, handler) ->
            internalRouter.addHttpExceptionHandler(exceptionClass.java, factory.createExceptionHandler(handler))
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy