varabyte / kobweb

A modern framework for full stack web apps in Kotlin, built upon Compose HTML
https://kobweb.varabyte.com
Apache License 2.0
1.51k stars 66 forks source link

Feature Request: Modify Default CORS Hosts #280

Closed DAlba-sudo closed 1 year ago

DAlba-sudo commented 1 year ago

Feature Request

First and foremost, thanks for your work.

I have spent most of my time under a rock, and found myself going away from my usual frameworks/languages. This is truly a beautiful library you've written.

I am an avid chrome extension developer and I was hoping to use your library's simplified API Streaming library in conjunction with a chrome extension for a personal project. That said, it seems that the extension is not being allowed to connect. Here's a summary of the issue, what I've tried, and my hypothesis - for what it's worth.

Issue: Chrome extension websocket connection to an API endpoint is not connecting when the request is made from a different origin. Hypothesis: I believe this may be due to existing CORS policies, most likely due to the KTOR library (take with a grain of salt though, not well versed in either KTOR or Kobweb).

Potential Solutions

I am aware that Kobweb does not support websockets out of the box, in the sense that the Kobweb server will generate one websocket for all streams (at least the way that I understand it). I am able to create a websocket that interfaces with the API Stream and send/receive messages, from any site that has the same host (i.e., localhost:8080) by starting the websocket to the ws://localhost:8080/api/kobweb-streams endpoint and sending the proper data.

I would love to help out with this by the way - if you (or anyone that might know how to help) have a lot on your plate. Just need a bit of guidance as to where to start digging, and if this is a KTOR issue that can be configured by downloading the KTOR library.

DAlba-sudo commented 1 year ago

May be a duplicate #254.

bitspittle commented 1 year ago

Thank you for the nice comment. I'm glad you're finding the experience with Kobweb overall enjoyable.

CORS is a PITA. It is currently configurable in the .kobweb/conf.yaml file. Take a look at my blog post for a concrete example: https://bitspittle.dev/blog/2023/clouddeploy#configure-cors

For more details about the conf.yaml configuration values, see these related lines:

https://github.com/varabyte/kobweb/blob/0f0c6a48d68efadeda4236263a075329c5583446/common/kobweb-common/src/main/kotlin/com/varabyte/kobweb/project/conf/KobwebConf.kt#L127

https://github.com/varabyte/kobweb/blob/0f0c6a48d68efadeda4236263a075329c5583446/common/kobweb-common/src/main/kotlin/com/varabyte/kobweb/project/conf/KobwebConf.kt#L188

If it is a CORS issue, you should hopefully have access to your server logs to get more information about what's going on. They can be found in .kobweb/server/logs/kobweb-server.log. You may need to configure the logging level to TRACE (which I actually temporarily made the default in 0.13.9 but later learned that was the wrong call, and am going to move back to DEBUG in 0.13.10)