/spring-fu

Incubator for Kofu configuration for Spring Boot

Primary LanguageKotlinApache License 2.0Apache-2.0

Build Status

Kofu configuration for Spring Boot

Spring Fu is an incubator for Kofu (Ko for Kotlin, fu for functional), which provides any API to configure Spring Boot applications programmatically with following characteristics:

  • Explicit configuration via a Kotlin DSL instead of auto-configuration

  • Leverages Spring Framework 5.x Functional bean configuration instead of annotations

  • Allows to define custom configuration slices (useful for more focused and efficient testing)

  • Great discoverability via code auto-complete

  • Web functional routing instead of @Controller available in 3 flavors: Expose the same Web functional routing programming model in 3 flavors:

  • Persistence via Spring Data functional APIs like:

  • Configuration via Spring Security security { } DSL (WIP)

  • GraalVM native image friendly

  • Fast startup and low memory consumption

It is not intended to be used in production yet, but rather to incubate and get feedback and contributions from the community in order to hopefully reach a point where it can be integrated as part of Spring Boot.

Minimal application

Here is a minimal sample application that is leveraging WebMvc.fn:

val app = application(WebApplicationType.SERVLET) {
	beans {
		bean<SampleService>()
	}
	webMvc {
		port = if (profiles.contains("test")) 8181 else 8080
		router {
			val service = ref<SampleService>()
			GET("/") {
				ok().body(service.generateMessage())
			}
			GET("/api") {
				ok().body(Sample(service.generateMessage()))
			}
		}
	}
}

data class Sample(val message: String)

class SampleService {
	fun generateMessage() = "Hello world!"
}

fun main() {
	app.run()
}

To use WebFlux.fn instead

  • Use WebApplicationType.REACTIVE instead of WebApplicationType.SERVLET

  • Use webFlux { } instead of webMvc { }

  • Use spring-boot-starter-webflux starter instead of spring-boot-starter-web

  • Use coRouter { } instead of router { } if you want to use Coroutines instead of Reactor API

Dependencies

Kofu is technically just a dependency you add to your Spring Boot project.

dependencies {
	implementation("org.springframework.fu:spring-fu-kofu:0.x")

	implementation("org.springframework.boot:spring-boot-starter-web")
	testImplementation("org.springframework.boot:spring-boot-starter-webflux")
}

API reference

An overview of Kofu is provided bellow with the related API documentation.

val dataConfig = configuration { (1)
	beans { } (2)
	mongodb { } (3)
	r2dbcH2 { } (4)
	r2dbcPostgresql {} (5)
}

val serverConfig = configuration { (1)
	beans { } (2)
	webFlux { (6)
		engine = (7)
		port = (8)
		router { } (9)
		include() (10)
		coRouter { } (11)
		cors { } (12)
		codecs { } (13)
		mustache() (14)
		filter() (15)
	}
}

val clientConfig = configuration {
	webClient { (16)
		baseUrl = (17)
		codecs { } (18)
	}
}

val app = application(WebApplicationType.REACTIVE) { (19)
	logging { } (20)
	configurationProperties<SampleProperties>() (21)
	listener<SampleEvent> { } (22)
	profile("sample"){ } (23)
	enable(dataConfig) (24)
	enable(serverConfig) (24)
	enable(clientConfig) (24)
}

fun main() {
	app.run() (25)
}

Getting started

  • Create a Spring 2.2.0 (SNAPSHOT) project on start.spring.io with the "Web" or "Reactive web" starter

  • Add the org.springframework.fu:spring-fu-kofu:0.1.BUILD-SNAPSHOT dependency

  • Use latest Kotlin 1.3.x

  • Modify the generated DemoApplication.kt file as following:

package com.sample

import org.springframework.fu.kofu.application

val app = application(...) {
	...
}

fun main() {
	app.run()
}

Samples

kofu-reactive-minimal

This is a sample project for a Spring Boot Reactive web application with Kofu configuration which provides a http://localhost:8080/ endpoint that displays "Hello world!" and an http://localhost:8080/api with a JSON endpoint.

You can run compile and run it as a Graal native image (GraalVM 1.0 RC10+) by running ./build.sh then ./com.sample.applicationkt.

kofu-reactive-mongodb

This is a sample project for a Spring Boot Reactive web application with Kofu configuration and a Reactive MongoDB backend.

kofu-reactive-r2dbc

This is a sample project for a Spring Boot Reactive web application with Kofu configuration and a R2DBC backend.

kofu-reactive-validation

This is a sample project for a Spring Boot Reactive web application with Kofu configuration and validation using YAVI.

kofu-coroutines-mongodb

This is a sample project for a Spring Boot Coroutines web application with Kofu configuration and a Reactive MongoDB backend.

kofu-coroutines-r2dbc

This is a sample project for a Spring Boot Coroutines web application with Kofu configuration and a R2DBC backend.

kofu-coroutines-validation

This is a sample project for a Spring Boot Coroutines web application with Kofu configuration and validation using YAVI.

kofu-servlet-minimal

This is a sample project for a Spring Boot web application based on WebMvc.fn (functional variant of Spring MVC) with Kofu configuration which provides a http://localhost:8080/ endpoint that displays "Hello world!" and an http://localhost:8080/api with a JSON endpoint.

Credits

In addition to the whole Spring and Reactor teams, special credits to: