bananaml / potassium

An HTTP serving framework by Banana
Apache License 2.0
97 stars 9 forks source link

CORS Support #28

Open lkskstlr opened 1 year ago

lkskstlr commented 1 year ago

Hi all,

I really like your service, but it seems that CORS is missing (or is there a recommended workaround)? I think it shouldn't be hard to add it, given that you have a thin wrapper around Flask. Is this on your timeline and if so, what would be the approximate timing?

Thanks Lukas

jaxball commented 1 year ago

@lkskstlr I was also trying to find this option. Since CORS is likely intended for local development, you can use the following monkey patch to achieve the same effect:

from potassium import Potassium, Request, Response
from flask import Flask

def new_create_flask_app(self):
    flask_app = original_create_flask_app(self)

    @flask_app.after_request
    def add_cors_headers(response):
        response.headers.add('Access-Control-Allow-Origin', 'http://localhost:3000')
        response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
        response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS')
        return response
    return flask_app

# monkey patch the new method
original_create_flask_app = Potassium._create_flask_app
Potassium._create_flask_app = new_create_flask_app
wimvanhenden-tool commented 1 year ago

Hi all,

I am having the same issues with Cors. Outside of Postman I can not make any calls to my Banana instance. Not on localhost, not even on a AWS Cloudfront/s3 with https.

@jaxball how exactly does your code tie into creating an app with Potassium like this: app = Potassium("my_app")

Thank you so much,

jgentes commented 10 months ago

Thanks @jaxball that works! Here's an example of the code (note the Flask import isn't required):

from potassium import Potassium, Request, Response

# for CORS support
def new_create_flask_app(self):
    flask_app = original_create_flask_app(self)

    @flask_app.after_request
    def add_cors_headers(response):
        response.headers.add('Access-Control-Allow-Origin', '*')
        response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
        return response
    return flask_app

# monkey patch the new method
original_create_flask_app = Potassium._create_flask_app
Potassium._create_flask_app = new_create_flask_app

app = Potassium("my_app")