forked from spring-projects-experimental/spring-fu
/
WebFluxServerDsl.kt
285 lines (248 loc) · 10.8 KB
/
WebFluxServerDsl.kt
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
package org.springframework.fu.kofu.webflux
import org.springframework.beans.factory.ObjectProvider
import org.springframework.beans.factory.getBeanProvider
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils.uniqueBeanName
import org.springframework.boot.autoconfigure.web.ResourceProperties
import org.springframework.boot.autoconfigure.web.ServerProperties
import org.springframework.boot.autoconfigure.web.reactive.*
import org.springframework.boot.web.embedded.jetty.JettyReactiveWebServerFactory
import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory
import org.springframework.boot.web.embedded.tomcat.TomcatReactiveWebServerFactory
import org.springframework.boot.web.embedded.undertow.UndertowReactiveWebServerFactory
import org.springframework.boot.web.reactive.server.ConfigurableReactiveWebServerFactory
import org.springframework.context.ApplicationContext
import org.springframework.context.support.GenericApplicationContext
import org.springframework.context.support.registerBean
import org.springframework.core.env.get
import org.springframework.fu.kofu.AbstractDsl
import org.springframework.fu.kofu.ConfigurationDsl
import org.springframework.web.reactive.function.server.CoRouterFunctionDsl
import org.springframework.web.reactive.function.server.RouterFunctionDsl
import org.springframework.web.server.WebFilter
/**
* Kofu DSL for WebFlux server.
*
* This DSL to be used in [org.springframework.fu.kofu.application] and a
* [org.springframework.boot.WebApplicationType.REACTIVE] parameter configures a
* [WebFlux server](https://docs.spring.io/spring/docs/current/spring-framework-reference/webflux-reactive.html#spring-webflux).
*
* When no codec is configured, `String` and `Resource` ones are configured by default.
* When a `codecs { }` block is declared, no one is configured by default.
*
* You can chose the underlying engine via the [WebFluxServerDsl.engine] parameter.
*
* Required dependencies can be retrieve using `org.springframework.boot:spring-boot-starter-webflux`.
*
* @sample org.springframework.fu.kofu.samples.router
* @see org.springframework.fu.kofu.application
* @see WebFluxServerDsl.codecs
* @see WebFluxServerDsl.cors
* @see WebFluxServerDsl.mustache
* @author Sebastien Deleuze
*/
open class WebFluxServerDsl(private val init: WebFluxServerDsl.() -> Unit): AbstractDsl() {
private val serverProperties = ServerProperties()
private val resourceProperties = ResourceProperties()
private val webFluxProperties = WebFluxProperties()
private var codecsConfigured: Boolean = false
/**
* Define the listening port of the webFlux.
*/
var port: Int = 8080
/**
* Define the underlying engine used.
*
* @see netty
* @see tomcat
* @see jetty
* @see undertow
*/
var engine: ConfigurableReactiveWebServerFactory? = null
override fun initialize(context: GenericApplicationContext) {
super.initialize(context)
init()
if (engine == null) {
engine = netty()
}
engine!!.setPort(port)
if (!codecsConfigured) {
StringCodecInitializer(false, false).initialize(context)
ResourceCodecInitializer(false).initialize(context)
}
if (context.containsBeanDefinition("webHandler")) {
throw IllegalStateException("Only one webFlux per application is supported")
}
ReactiveWebServerInitializer(serverProperties, resourceProperties, webFluxProperties, engine).initialize(context)
}
/**
* Configure codecs via a [dedicated DSL][WebFluxServerCodecDsl].
*/
fun codecs(init: WebFluxServerCodecDsl.() -> Unit = {}) {
WebFluxServerCodecDsl(init).initialize(context)
codecsConfigured = true
}
/**
* Define a request filter for this webFlux
*/
inline fun <reified T: WebFilter> filter() {
context.registerBean<T>(uniqueBeanName(T::class.java.name, context))
}
/**
* Configure routes via a [dedicated DSL][RouterFunctionDsl].
* @sample org.springframework.fu.kofu.samples.router
*/
fun router(routes: (RouterFunctionDsl.() -> Unit)) {
context.registerBean(uniqueBeanName(RouterFunctionDsl::class.java.name, context)) { org.springframework.web.reactive.function.server.router(routes) }
}
/**
* Get a reference to the bean by type or type + name with the syntax
* `ref<Foo>()` or `ref<Foo>("foo")`. When leveraging Kotlin type inference
* it could be as short as `ref()` or `ref("foo")`.
* TODO Update when SPR-17648 will be fixed
* @param name the name of the bean to retrieve
* @param T type the bean must match, can be an interface or superclass
*/
inline fun <reified T : Any> RouterFunctionDsl.ref(name: String? = null): T = when (name) {
null -> context.getBean(T::class.java)
else -> context.getBean(name, T::class.java)
}
/**
* Return an provider for the specified bean, allowing for lazy on-demand retrieval
* of instances, including availability and uniqueness options.
* TODO Update when SPR-17648 will be fixed
* @see org.springframework.beans.factory.BeanFactory.getBeanProvider
*/
inline fun <reified T : Any> RouterFunctionDsl.provider() : ObjectProvider<T> = context.getBeanProvider()
/**
* Configure Coroutines routes via a [dedicated DSL][CoRouterFunctionDsl].
* @sample org.springframework.fu.kofu.samples.coRouter
*/
fun coRouter(routes: (CoRouterFunctionDsl.() -> Unit)) {
context.registerBean(uniqueBeanName(CoRouterFunctionDsl::class.java.name, context)) { org.springframework.web.reactive.function.server.coRouter(routes) }
}
/**
* Get a reference to the bean by type or type + name with the syntax
* `ref<Foo>()` or `ref<Foo>("foo")`. When leveraging Kotlin type inference
* it could be as short as `ref()` or `ref("foo")`.
* TODO Update when SPR-17648 will be fixed
* @param name the name of the bean to retrieve
* @param T type the bean must match, can be an interface or superclass
*/
inline fun <reified T : Any> CoRouterFunctionDsl.ref(name: String? = null): T = when (name) {
null -> context.getBean(T::class.java)
else -> context.getBean(name, T::class.java)
}
/**
* Return an provider for the specified bean, allowing for lazy on-demand retrieval
* of instances, including availability and uniqueness options.
* TODO Update when SPR-17648 will be fixed
* @see org.springframework.beans.factory.BeanFactory.getBeanProvider
*/
inline fun <reified T : Any> CoRouterFunctionDsl.provider() : ObjectProvider<T> = context.getBeanProvider()
/**
* Netty engine.
* TODO Use lazy val when supported by GraalVM
* @see engine
*/
fun netty() = NettyDelegate().invoke()
/**
* Tomcat engine.
* TODO Use lazy val when supported by GraalVM
* @see engine
*/
fun tomcat() = TomcatDelegate().invoke()
/**
* Jetty engine.
* TODO Use lazy val when supported by GraalVM
* @see engine
*/
fun jetty() = JettyDelegate().invoke()
/**
* Undertow engine.
* TODO Use lazy val when supported by GraalVM
* @see engine
*/
fun undertow() = UndertowDelegate().invoke()
private class NettyDelegate: () -> ConfigurableReactiveWebServerFactory {
override fun invoke(): ConfigurableReactiveWebServerFactory {
return NettyReactiveWebServerFactory()
}
}
private class TomcatDelegate: () -> ConfigurableReactiveWebServerFactory {
override fun invoke(): ConfigurableReactiveWebServerFactory {
return TomcatReactiveWebServerFactory()
}
}
private class JettyDelegate: () -> ConfigurableReactiveWebServerFactory {
override fun invoke(): ConfigurableReactiveWebServerFactory {
return JettyReactiveWebServerFactory()
}
}
private class UndertowDelegate: () -> ConfigurableReactiveWebServerFactory {
override fun invoke(): ConfigurableReactiveWebServerFactory {
return UndertowReactiveWebServerFactory()
}
}
class WebFluxServerCodecDsl(private val init: WebFluxServerCodecDsl.() -> Unit) : AbstractDsl() {
override fun initialize(context: GenericApplicationContext) {
super.initialize(context)
init()
}
/**
* Enable [org.springframework.core.codec.CharSequenceEncoder] and [org.springframework.core.codec.StringDecoder]
*/
fun string(textPlainOnly: Boolean = false) {
StringCodecInitializer(false, textPlainOnly).initialize(context)
}
/**
* Enable [org.springframework.http.codec.ResourceHttpMessageWriter] and [org.springframework.core.codec.ResourceDecoder]
*/
fun resource() {
ResourceCodecInitializer(false).initialize(context)
}
/**
* Register an `ObjectMapper` bean and configure a [Jackson](https://github.com/FasterXML/jackson)
* JSON codec on WebFlux server via a [dedicated DSL][JacksonDsl].
*
* Required dependencies can be retrieve using `org.springframework.boot:spring-boot-starter-json`
* (included by default in `spring-boot-starter-webflux`).
*
* @sample org.springframework.fu.kofu.samples.jacksonDsl
*/
fun jackson(dsl: JacksonDsl.() -> Unit = {}) {
JacksonDsl(false, dsl).initialize(context)
}
/**
* Enable [org.springframework.http.codec.protobuf.ProtobufEncoder] and [org.springframework.http.codec.protobuf.ProtobufDecoder]
*
* This codec requires Protobuf 3 or higher with the official `com.google.protobuf:protobuf-java` dependency, and
* supports `application/x-protobuf` and `application/octet-stream`.
*/
fun protobuf() {
ProtobufCodecInitializer(false).initialize(context)
}
/**
* Enable [org.springframework.http.codec.FormHttpMessageWriter] and [org.springframework.http.codec.FormHttpMessageReader]
*/
fun form() {
FormCodecInitializer(false).initialize(context)
}
/**
* Enable [org.springframework.http.codec.multipart.MultipartHttpMessageWriter] and
* [org.springframework.http.codec.multipart.MultipartHttpMessageReader]
*
* This codec requires Synchronoss NIO Multipart library via the `org.synchronoss.cloud:nio-multipart-parser`
* dependency.
*/
fun multipart() {
MultipartCodecInitializer(false).initialize(context)
}
}
}
/**
* Declare a WebFlux server.
* @see WebFluxServerDsl
*/
fun ConfigurationDsl.webFlux(dsl: WebFluxServerDsl.() -> Unit = {}) {
WebFluxServerDsl(dsl).initialize(context)
}