hi I’ve created a PolyHandler with a http and WS (...
# http4k
m
hi I’ve created a PolyHandler with a http and WS (see https://www.http4k.org/guide/howto/serve_websockets/) When testing I’m getting the “has been blocked by CORS policy” error. I’ve found the code normally used to set the CORS policy but I’m struggling to apply it to the PolyHandler, can you help?
Copy code
CorsPolicy(OriginPolicy.AllowAll(), listOf("content-type", "Authorization"), Method.values().toList(), true)
d
Is it a problem with the webaocket or the http?
m
hi This was a problem with the http. The following code is now working ok:
Copy code
val unsafeGlobalPermissive =
    CorsPolicy(OriginPolicy.AllowAll(), listOf("content-type", "Authorization"), Method.values().toList(), true)

val events = defaultEvents()
val apiWithCors = ServerFilters.Cors(unsafeGlobalPermissive)
    .then(CatchAll(events))
    .then(api)

val poly = PolyHandler(apiWithCors, ws)
poly.asServer(Undertow(port)).start()
Issue resolved.
thanks
d
great that you've worked it out.
I was just having a go and was trying to recreate
m
thanks for looking into it 👍
d
there's also a trick you can do with lists of filters:
Copy code
val fil1 = Filter { next ->
        {
            next(it)
        }
    }
    val fil2 = Filter { next ->
        {
            next(it)
        }
    }

    val all: Filter = listOf(fil1, fil2).reduce(Filter::then)
💪 1
you can reduce them! 🙂
m
👍