Skip to content

Commit 23da7a3

Browse files
Merge pull request #35 from suvanbanerjee/dev
Add CORS middleware for STT web services
1 parent 1c0c493 commit 23da7a3

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CORS_ORIGINS=https://example.com,http://localhost:3000

ovos_stt_http_server/__init__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
# See the License for the specific language governing permissions and
1111
# limitations under the License.
1212
#
13+
import os
1314
from tempfile import NamedTemporaryFile
1415

1516
from typing import List, Tuple, Optional, Set, Union
1617
from fastapi import FastAPI
18+
from fastapi.middleware.cors import CORSMiddleware
1719
from fastapi.responses import PlainTextResponse
1820
from ovos_config import Configuration
1921
from ovos_plugin_manager.audio_transformers import load_audio_transformer_plugin, AudioLanguageDetector
@@ -105,6 +107,15 @@ def bytes2audiodata(data: bytes) -> AudioData:
105107

106108
def create_app(stt_plugin, lang_plugin=None, multi=False, has_gradio=False):
107109
app = FastAPI()
110+
cors_origins = os.environ.get("CORS_ORIGINS", "*")
111+
origins = [origin.strip() for origin in cors_origins.split(",")] if cors_origins != "*" else ["*"]
112+
app.add_middleware(
113+
CORSMiddleware,
114+
allow_origins=origins,
115+
allow_credentials=True,
116+
allow_methods=["*"],
117+
allow_headers=["*"],
118+
)
108119
if multi:
109120
model = MultiModelContainer(stt_plugin, lang_plugin)
110121
else:

0 commit comments

Comments
 (0)