Skip to content

Commit 2a0f03a

Browse files
refactor: extract cors configs into dify config and cleanup the config class (langgenius#5507)
Co-authored-by: takatost <[email protected]>
1 parent ec1d3dd commit 2a0f03a

File tree

8 files changed

+51
-55
lines changed

8 files changed

+51
-55
lines changed

.github/workflows/api-tests.yml

-2
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ jobs:
7777
docker/docker-compose.pgvecto-rs.yaml
7878
docker/docker-compose.pgvector.yaml
7979
docker/docker-compose.chroma.yaml
80-
docker/docker-compose.oracle.yaml
8180
services: |
8281
weaviate
8382
qdrant
@@ -87,7 +86,6 @@ jobs:
8786
pgvecto-rs
8887
pgvector
8988
chroma
90-
oracle
9189
9290
- name: Test Vector Stores
9391
run: poetry run -C api bash dev/pytest/pytest_vdb.sh

api/app.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
from werkzeug.exceptions import Unauthorized
2525

2626
from commands import register_commands
27-
from config import Config
2827

2928
# DO NOT REMOVE BELOW
3029
from events import event_handlers
@@ -82,7 +81,6 @@ def create_flask_app_with_configs() -> Flask:
8281
with configs loaded from .env file
8382
"""
8483
dify_app = DifyApp(__name__)
85-
dify_app.config.from_object(Config())
8684
dify_app.config.from_mapping(DifyConfig().model_dump())
8785
return dify_app
8886

@@ -232,7 +230,7 @@ def register_blueprints(app):
232230
app = create_app()
233231
celery = app.extensions["celery"]
234232

235-
if app.config['TESTING']:
233+
if app.config.get('TESTING'):
236234
print("App is running in TESTING mode")
237235

238236

api/config.py

-42
This file was deleted.

api/configs/deploy/__init__.py

+10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ class DeploymentConfig(BaseModel):
55
"""
66
Deployment configs
77
"""
8+
APPLICATION_NAME: str = Field(
9+
description='application name',
10+
default='langgenius/dify',
11+
)
12+
13+
TESTING: bool = Field(
14+
description='',
15+
default=False,
16+
)
17+
818
EDITION: str = Field(
919
description='deployment edition',
1020
default='SELF_HOSTED',

api/configs/feature/__init__.py

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import Optional
22

3-
from pydantic import AliasChoices, BaseModel, Field, NonNegativeInt, PositiveInt
3+
from pydantic import AliasChoices, BaseModel, Field, NonNegativeInt, PositiveInt, computed_field
44

55
from configs.feature.hosted_service import HostedServiceConfig
66

@@ -125,6 +125,28 @@ class HttpConfig(BaseModel):
125125
default=False,
126126
)
127127

128+
inner_CONSOLE_CORS_ALLOW_ORIGINS: str = Field(
129+
description='',
130+
validation_alias=AliasChoices('CONSOLE_CORS_ALLOW_ORIGINS', 'CONSOLE_WEB_URL'),
131+
default='',
132+
)
133+
134+
@computed_field
135+
@property
136+
def CONSOLE_CORS_ALLOW_ORIGINS(self) -> list[str]:
137+
return self.inner_CONSOLE_CORS_ALLOW_ORIGINS.split(',')
138+
139+
inner_WEB_API_CORS_ALLOW_ORIGINS: Optional[str] = Field(
140+
description='',
141+
validation_alias=AliasChoices('WEB_API_CORS_ALLOW_ORIGINS'),
142+
default='*',
143+
)
144+
145+
@computed_field
146+
@property
147+
def WEB_API_CORS_ALLOW_ORIGINS(self) -> list[str]:
148+
return self.inner_WEB_API_CORS_ALLOW_ORIGINS.split(',')
149+
128150

129151
class InnerAPIConfig(BaseModel):
130152
"""

api/core/helper/code_executor/code_executor.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
import os
23
import time
34
from enum import Enum
45
from threading import Lock
@@ -8,7 +9,6 @@
89
from pydantic import BaseModel
910
from yarl import URL
1011

11-
from config import get_env
1212
from core.helper.code_executor.entities import CodeDependency
1313
from core.helper.code_executor.javascript.javascript_transformer import NodeJsTemplateTransformer
1414
from core.helper.code_executor.jinja2.jinja2_transformer import Jinja2TemplateTransformer
@@ -18,8 +18,8 @@
1818
logger = logging.getLogger(__name__)
1919

2020
# Code Executor
21-
CODE_EXECUTION_ENDPOINT = get_env('CODE_EXECUTION_ENDPOINT')
22-
CODE_EXECUTION_API_KEY = get_env('CODE_EXECUTION_API_KEY')
21+
CODE_EXECUTION_ENDPOINT = os.environ.get('CODE_EXECUTION_ENDPOINT', 'http://sandbox:8194')
22+
CODE_EXECUTION_API_KEY = os.environ.get('CODE_EXECUTION_API_KEY', 'dify-sandbox')
2323

2424
CODE_EXECUTION_TIMEOUT= (10, 60)
2525

api/tests/unit_tests/configs/test_dify_config.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def example_env_file(tmp_path, monkeypatch) -> str:
1515
file_path.write_text(dedent(
1616
"""
1717
CONSOLE_API_URL=https://example.com
18+
CONSOLE_WEB_URL=https://example.com
1819
"""))
1920
return str(file_path)
2021

@@ -47,14 +48,13 @@ def test_flask_configs(example_env_file):
4748
flask_app.config.from_mapping(DifyConfig(_env_file=example_env_file).model_dump())
4849
config = flask_app.config
4950

50-
# configs read from dotenv directly
51-
assert config['LOG_LEVEL'] == 'INFO'
52-
5351
# configs read from pydantic-settings
52+
assert config['LOG_LEVEL'] == 'INFO'
5453
assert config['COMMIT_SHA'] == ''
5554
assert config['EDITION'] == 'SELF_HOSTED'
5655
assert config['API_COMPRESSION_ENABLED'] is False
5756
assert config['SENTRY_TRACES_SAMPLE_RATE'] == 1.0
57+
assert config['TESTING'] == False
5858

5959
# value from env file
6060
assert config['CONSOLE_API_URL'] == 'https://example.com'
@@ -71,3 +71,7 @@ def test_flask_configs(example_env_file):
7171
'pool_recycle': 3600,
7272
'pool_size': 30,
7373
}
74+
75+
assert config['CONSOLE_WEB_URL']=='https://example.com'
76+
assert config['CONSOLE_CORS_ALLOW_ORIGINS']==['https://example.com']
77+
assert config['WEB_API_CORS_ALLOW_ORIGINS'] == ['*']

dev/pytest/pytest_vdb.sh

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
#!/bin/bash
22
set -x
33

4-
pytest api/tests/integration_tests/vdb/
4+
pytest api/tests/integration_tests/vdb/chroma \
5+
api/tests/integration_tests/vdb/milvus \
6+
api/tests/integration_tests/vdb/pgvecto_rs \
7+
api/tests/integration_tests/vdb/pgvector \
8+
api/tests/integration_tests/vdb/qdrant \
9+
api/tests/integration_tests/vdb/weaviate \
10+
api/tests/integration_tests/vdb/test_vector_store.py

0 commit comments

Comments
 (0)