Skip to content

Commit

Permalink
lyrebird support CORS (#818)
Browse files Browse the repository at this point in the history
Co-authored-by: yangfan100 <[email protected]>
  • Loading branch information
echoyang7 and yangfan100 authored Jan 8, 2024
1 parent f61fb3d commit 29979fd
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lyrebird/mock/mock_server.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from flask import Flask, redirect, url_for, request
from flask_cors import CORS
from . import context
from .blueprints.apis import api
from .blueprints.ui import ui
Expand Down Expand Up @@ -45,8 +46,7 @@ def __init__(self):
self.debug = False
self.port = 9090
self._working_thread = None
self.app = Flask('MOCK')

self.app = self.create_app()
# async_mode = threading / eventlet / gevent / gevent_uwsgi
self.socket_io = SocketIO(self.app, async_mode='threading', logger=False, cors_allowed_origins='*')

Expand Down Expand Up @@ -87,6 +87,16 @@ def on_app_error(error):

return application.make_fail_response(f'Mock server error:\n {trace}')

def create_app(self):
app = Flask('MOCK')
cors_resources = application.config.get('mock.server.cors.resources')
if cors_resources and isinstance(cors_resources, dict):
try:
CORS(app, resources=cors_resources)
except Exception as e:
_logger.warning(f"An error occurred while setting CORS. The default mode does not support CORS. Error msg: {e}")
return app

def run(self):
server_ip = application.config.get('ip')
_logger.log(60, f'Core start on http://{server_ip}:{self.port}')
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ SQLAlchemy==1.3.22
aiohttp==3.8.3
netifaces==0.11.0
jsonschema==4.17.0
Flask-Cors==4.0.0
1 change: 1 addition & 0 deletions requirements.txt.lock
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ colorama==0.4.1
Flask==2.2.2
Flask-RESTful==0.3.9
Flask-SocketIO==5.3.0
Flask-Cors==4.0.0
frozenlist==1.3.1
idna==2.8
importlib-metadata==5.0.0
Expand Down

0 comments on commit 29979fd

Please sign in to comment.