Skip to content

Commit d7cfc00

Browse files
Fix localhost cors bug (#16)
1 parent 73cbc6b commit d7cfc00

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/solace_ai_connector_web/backend/server_base.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from flask_cors import CORS
44
from solace_ai_connector.components.component_base import ComponentBase
55
from flask_wtf import CSRFProtect
6+
from urllib.parse import urlparse
67
import os
78

89
info = {
@@ -216,13 +217,24 @@ def init_app(self):
216217
self.app.config['SECRET_KEY'] = self.csrf_key
217218
csrf.init_app(self.app)
218219

220+
frontend_origins = [self.frontend_url]
221+
222+
# if frontend_url is using localhost or 127.0.0.1, then add them both to cors origins
223+
if "localhost" in self.frontend_url or "127.0.0.1" in self.frontend_url:
224+
parsed_url = urlparse(self.frontend_url)
225+
scheme = parsed_url.scheme
226+
port = f":{parsed_url.port}" if parsed_url.port else ""
227+
228+
if "localhost" in self.frontend_url:
229+
frontend_origins.append(f"{scheme}://127.0.0.1{port}")
230+
else:
231+
frontend_origins.append(f"{scheme}://localhost{port}")
232+
219233
CORS(
220234
self.app,
221235
resources={
222236
r"/*": {
223-
"origins": [
224-
self.frontend_url,
225-
],
237+
"origins": frontend_origins,
226238
"supports_credentials": True
227239
},
228240
},

0 commit comments

Comments
 (0)