Skip to content

Commit

Permalink
Add CORS support
Browse files Browse the repository at this point in the history
  • Loading branch information
sdeleuze committed Nov 7, 2017
1 parent 1e789b4 commit 4556da1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
7 changes: 6 additions & 1 deletion src/main/kotlin/functional/Application.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package functional

import org.springframework.beans.factory.getBean
import org.springframework.context.support.GenericApplicationContext
import org.springframework.http.server.reactive.HttpHandler
import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter
import org.springframework.web.cors.reactive.CorsWebFilter
import org.springframework.web.server.adapter.WebHttpHandlerBuilder
import reactor.ipc.netty.http.server.HttpServer
import reactor.ipc.netty.tcp.BlockingNettyContext
Expand All @@ -22,7 +24,10 @@ class Application {
}

server = HttpServer.create(port)
httpHandler = WebHttpHandlerBuilder.applicationContext(context).build()
httpHandler = WebHttpHandlerBuilder
.applicationContext(context)
.apply { if (context.containsBean("corsFilter")) filter(context.getBean<CorsWebFilter>()) }
.build()
}

fun start() {
Expand Down
10 changes: 6 additions & 4 deletions src/main/kotlin/functional/Beans.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import functional.web.view.MustacheResourceTemplateLoader
import functional.web.view.MustacheViewResolver
import org.springframework.context.support.ReloadableResourceBundleMessageSource
import org.springframework.context.support.beans
import org.springframework.web.cors.CorsConfiguration
import org.springframework.web.cors.reactive.CorsWebFilter
import org.springframework.web.reactive.function.server.HandlerStrategies
import org.springframework.web.reactive.function.server.RouterFunctions

Expand All @@ -31,9 +33,9 @@ fun beans() = beans {
setSuffix(suffix)
}
}
profile("foo") {
bean<Foo>()
profile("cors") {
bean("corsFilter") {
CorsWebFilter { CorsConfiguration().applyPermitDefaultValues() }
}
}
}

class Foo

0 comments on commit 4556da1

Please sign in to comment.