bootique-kotlin

Provides extension function and features for smooth development with Bootique and Kotlin.

View the Project on GitHub bootique/bootique-kotlin

Kotlin Extensions for Bootique and Bootique Modules

Overview

bootique-kotlin contains following modules:

  1. Kotlin APIs and extensions for Bootique;
  2. Kotlin Script Configuration Module;
  3. Configuration and Extensions for Bootique Modules;
  4. JacksonService which provides ObjectMapper with enabled KotlinModule.

TL;DR;

Getting started

Kotlin 1.2.x used in project.

Latest stable version: Maven Central

// Kotlin Extensions for Bootique
compile("io.bootique.kotlin:bootique-kotlin:0.25")

// Kotlin Configuration Module
compile("io.bootique.kotlin:bootique-kotlin-config:0.25")

// Kotlin Configuration Module
compile("io.bootique.kotlin:bootique-kotlin-jackson:0.25")

// Kotlin Configuration and Extensions for Jetty. Also this adds dependency to bootique-jetty module.
compile("io.bootique.kotlin:bootique-kotlin-jetty:0.25")

// Kotlin Configuration and Extensions for Logback. Also this adds dependency to bootique-logback module.
compile("io.bootique.kotlin:bootique-kotlin-logback:0.25")

// Kotlin Configuration and Extensions for $moduleName$. Also this adds dependency to bootique-$moduleName$ module.
compile("io.bootique.kotlin:bootique-kotlin-$moduleName$:0.25")

Bootique

Replacement for Bootique

bootique-kotlin provides replacement for Bootique class - KotlinBootique:

fun main(args: Array<String>) {
    KotlinBootique(args)
        .module(ApplicationModule::class)
        .exec()
        .exit()
}

So no need for extensions for Bootique class, KotlinBootique provides best experience for developing Bootique apps with Kotlin.

KotlinBQModuleProvider

KotlinBQModuleProvider - interface to implement in Bootique Kotlin application instead of BQModuleProvider.

class ApplicationModuleProvider : KotlinBQModuleProvider {
    override val module = ApplicationModule()
    override val overrides = listOf(BQCoreModule::class)
    override val dependencies = listOf(KotlinConfigModule::class)
}

You can see how declarative become module provider.

Extension: ConfigurationFactory.config

// Using Java Api
configurationFactory.config(SampleFactory::class.java, "sample")

// With Extension
configurationFactory.config(SampleFactory::class, "sample")

// With Extension, reified generics
configurationFactory.config<SampleFactory>("sample")

// Type Inference
@Singleton
@Provides
fun createAppConfiguration(configurationFactory: ConfigurationFactory): SampleFactory {
    return configurationFactory.config/* No Type Here */(configPrefix)
}

Extension: BQCoreModuleExtender.addCommand

Straightforward and easy to use extension for contributing commands.

BQCoreModule
    .extend(binder)
    .addCommand(ApplicationCommand::class)

Extension: BQCoreModuleExtender.setDefaultCommand

Also extension for setDefaultCommand available.

BQCoreModule
    .extend(binder)
    .setDefaultCommand(ApplicationCommand::class)

Extensions:

See Extensions.kt for sources.

Deprecated Extensions:

These extensions deprecated and deleted in 0.25 in favor of KotlinModule and KotlinBootique.

Guice

KotlinModule

bootique-kotlin introduces new module interface to use with kotlin: KotlinModule

class ApplicationModule : KotlinModule {
    override fun configure(binder: KotlinBinder) {
        binder.bind(ShareCountService::class).to(DefaultShareCountService::class).asSingleton()
        binder.bind(HttpClient::class).to(DefaultHttpClient::class).asSingleton()
    }
}

Extensions

There are few function to help work with TypeLiteral and Key.

// TypeLiteral
typeLiteral<Array<String>>()

// Key
key<List<Callable<A>>>()

Configuration Module

Use Kotlin Script for configuration really simple:

  1. Create script
  2. Override ConfigurationFactory

Configuration with Kotlin can be defined in Kotlin Script file:

import io.bootique.kotlin.config.modules.config
import io.bootique.kotlin.config.modules.httpConnector
import io.bootique.kotlin.config.modules.jetty

config {
    jetty {
        httpConnector {
            port = 4242
            host = "0.0.0.0"
        }
    }
}

Enable Kotlin Script Configuration in Bootique:

With extension:

fun main(args: Array<String>) {
    KotlinBootique(args)
        .withKotlinConfig() // Extension function
        .autoLoadModules()
        .exec()
        .exit()
}

Using BQModuleProvider:

fun main(args: Array<String>) {
    KotlinBootique(args)
        .module(KotlinConfigModuleProvider())
        .autoLoadModules()
        .exec()
        .exit()
}

You can pass this file as always to bootique:

./bin/application --config=classpath:config.kts --server

It’s even support multiple files (each file contains map of configs):

./bin/application --config=classpath:config.kts --config=classpath:config1.kts --server

That’s it! You get autocomplete in IDE, and code for configuration!

Bootique Jetty

Define empty config:

config {
    jetty {

    }
}

Use autocompletion to define configuration.

Use httpConnector/httpsConnector extensions to define connectors:

jetty {
    httpConnector {
        port = 4242
        host = "192.168.0.1"
        responseHeaderSize = 42
        requestHeaderSize = 13
    }
}

Bootique Logback

Define empty config:

config {
    logback {

    }
}

Number of extensions available inside config:

logback {
    useLogbackConfig = false
    debug = false
    level = LogbackLevel.warn
    logger(FactoryDSL::class, LogbackLevel.error)
    consoleAppender {
        target = ConsoleTarget.stderr
        logFormat = "[%d{dd/MMM/yyyy:HH:mm:ss}] %t %-5p %c{1}: %m%n"
    }
    appender(ConsoleAppenderFactory().apply {
        target = ConsoleTarget.stdout
    })
    fileAppender {
        file = "abc"
        timeBasedRollingPolicy {
            setFileNamePattern("Abc_%d")
        }
        fixedWindowRollingPolicy {
            setFileNamePattern("Abc_%d")
            setFileSize("10mb")
        }
        sizeAndTimeRollingPolicy {
            setFileNamePattern("mylog-%d{yyyy-MM-dd}.%i.txt")
            setFileSize("10mb")
        }
    }
}

Bootique Bom

Bootique Undertow

Bootique Cayenne

Bootique Jdbc

Bootique Jersey Client

Bootique Linkrest

Bootique Mvc

Bootique Swagger

Bootique Jersey

Bootique Shiro

Bootique Linkmove

Bootique Tapestry

Bootique Jooq

Bootique Liquibase

Bootique Flyway

Bootique Job

Bootique Kafka Client

Bootique Jcache

Bootique Curator

Bootique Metrics

Bootique Rabbitmq Client

WIP