Skip to content

Commit

Permalink
app configs
Browse files Browse the repository at this point in the history
  • Loading branch information
bowenliang123 committed Jun 15, 2024
1 parent 4f0488a commit e584d37
Show file tree
Hide file tree
Showing 15 changed files with 248 additions and 60 deletions.
17 changes: 13 additions & 4 deletions api/app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os

from configs.app_configs import AppConfigs

if not os.environ.get("DEBUG") or os.environ.get("DEBUG").lower() != 'true':
from gevent import monkey

Expand All @@ -25,7 +27,6 @@
from config import Config

# DO NOT REMOVE BELOW
from events import event_handlers
from extensions import (
ext_celery,
ext_code_based_extension,
Expand All @@ -42,7 +43,6 @@
from extensions.ext_database import db
from extensions.ext_login import login_manager
from libs.passport import PassportService
from models import account, dataset, model, source, task, tool, tools, web
from services.account_service import AccountService

# DO NOT REMOVE ABOVE
Expand Down Expand Up @@ -74,10 +74,19 @@ class DifyApp(Flask):
# Application Factory Function
# ----------------------------

def create_flask_app() -> Flask:
"""
create a raw flask app
with configs loaded from .env file
"""
dify_app = DifyApp(__name__)
dify_app.config.from_object(Config())
dify_app.config.from_mapping(AppConfigs().dict())
return dify_app


def create_app() -> Flask:
app = DifyApp(__name__)
app.config.from_object(Config())
app = create_flask_app()

app.secret_key = app.config['SECRET_KEY']

Expand Down
56 changes: 2 additions & 54 deletions api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
dotenv.load_dotenv()

DEFAULTS = {
'EDITION': 'SELF_HOSTED',
'DB_USERNAME': 'postgres',
'DB_PASSWORD': '',
'DB_HOST': 'localhost',
Expand All @@ -18,18 +17,12 @@
'REDIS_USE_SSL': 'False',
'OAUTH_REDIRECT_PATH': '/console/api/oauth/authorize',
'OAUTH_REDIRECT_INDEX_PATH': '/',
'CONSOLE_WEB_URL': 'https://cloud.dify.ai',
'CONSOLE_API_URL': 'https://cloud.dify.ai',
'SERVICE_API_URL': 'https://api.dify.ai',
'APP_WEB_URL': 'https://udify.app',
'FILES_URL': '',
'FILES_ACCESS_TIMEOUT': 300,
'S3_USE_AWS_MANAGED_IAM': 'False',
'S3_ADDRESS_STYLE': 'auto',
'STORAGE_TYPE': 'local',
'STORAGE_LOCAL_PATH': 'storage',
'CHECK_UPDATE_URL': 'https://updates.dify.ai',
'DEPLOY_ENV': 'PRODUCTION',
'SQLALCHEMY_DATABASE_URI_SCHEME': 'postgresql',
'SQLALCHEMY_POOL_SIZE': 30,
'SQLALCHEMY_MAX_OVERFLOW': 10,
Expand All @@ -44,10 +37,6 @@
'QDRANT_GRPC_ENABLED': 'False',
'QDRANT_GRPC_PORT': '6334',
'CELERY_BACKEND': 'database',
'LOG_LEVEL': 'INFO',
'LOG_FILE': '',
'LOG_FORMAT': '%(asctime)s.%(msecs)03d %(levelname)s [%(threadName)s] [%(filename)s:%(lineno)d] - %(message)s',
'LOG_DATEFORMAT': '%Y-%m-%d %H:%M:%S',
'HOSTED_OPENAI_QUOTA_LIMIT': 200,
'HOSTED_OPENAI_TRIAL_ENABLED': 'False',
'HOSTED_OPENAI_TRIAL_MODELS': 'gpt-3.5-turbo,gpt-3.5-turbo-1106,gpt-3.5-turbo-instruct,gpt-3.5-turbo-16k,gpt-3.5-turbo-16k-0613,gpt-3.5-turbo-0613,gpt-3.5-turbo-0125,text-davinci-003',
Expand Down Expand Up @@ -114,65 +103,24 @@ class Config:
"""Application configuration class."""

def __init__(self):
# ------------------------
# General Configurations.
# ------------------------
self.CURRENT_VERSION = "0.6.11"
self.COMMIT_SHA = get_env('COMMIT_SHA')
self.EDITION = get_env('EDITION')
self.DEPLOY_ENV = get_env('DEPLOY_ENV')
self.TESTING = False
self.LOG_LEVEL = get_env('LOG_LEVEL')
self.LOG_FILE = get_env('LOG_FILE')
self.LOG_FORMAT = get_env('LOG_FORMAT')
self.LOG_DATEFORMAT = get_env('LOG_DATEFORMAT')
self.API_COMPRESSION_ENABLED = get_bool_env('API_COMPRESSION_ENABLED')

# The backend URL prefix of the console API.
# used to concatenate the login authorization callback or notion integration callback.
self.CONSOLE_API_URL = get_env('CONSOLE_API_URL')

# The front-end URL prefix of the console web.
# used to concatenate some front-end addresses and for CORS configuration use.
self.CONSOLE_WEB_URL = get_env('CONSOLE_WEB_URL')

# WebApp Url prefix.
# used to display WebAPP API Base Url to the front-end.
self.APP_WEB_URL = get_env('APP_WEB_URL')

# Service API Url prefix.
# used to display Service API Base Url to the front-end.
self.SERVICE_API_URL = get_env('SERVICE_API_URL')

# File preview or download Url prefix.
# used to display File preview or download Url to the front-end or as Multi-model inputs;
# Url is signed and has expiration time.
self.FILES_URL = get_env('FILES_URL') if get_env('FILES_URL') else self.CONSOLE_API_URL

# File Access Time specifies a time interval in seconds for the file to be accessed.
# The default value is 300 seconds.
self.FILES_ACCESS_TIMEOUT = int(get_env('FILES_ACCESS_TIMEOUT'))

# Your App secret key will be used for securely signing the session cookie
# Make sure you are changing this key for your deployment with a strong key.
# You can generate a strong key using `openssl rand -base64 42`.
# Alternatively you can set it with `SECRET_KEY` environment variable.
self.SECRET_KEY = get_env('SECRET_KEY')

# Enable or disable the inner API.
self.INNER_API = get_bool_env('INNER_API')
# The inner API key is used to authenticate the inner API.
self.INNER_API_KEY = get_env('INNER_API_KEY')

# cors settings
self.CONSOLE_CORS_ALLOW_ORIGINS = get_cors_allow_origins(
'CONSOLE_CORS_ALLOW_ORIGINS', self.CONSOLE_WEB_URL)
'CONSOLE_CORS_ALLOW_ORIGINS', get_env('CONSOLE_WEB_URL'))
self.WEB_API_CORS_ALLOW_ORIGINS = get_cors_allow_origins(
'WEB_API_CORS_ALLOW_ORIGINS', '*')

# check update url
self.CHECK_UPDATE_URL = get_env('CHECK_UPDATE_URL')

# ------------------------
# Database Configurations.
# ------------------------
Expand Down Expand Up @@ -212,7 +160,7 @@ def __init__(self):
self.CELERY_BACKEND = get_env('CELERY_BACKEND')
self.CELERY_RESULT_BACKEND = 'db+{}'.format(self.SQLALCHEMY_DATABASE_URI) \
if self.CELERY_BACKEND == 'database' else self.CELERY_BROKER_URL
self.BROKER_USE_SSL = self.CELERY_BROKER_URL.startswith('rediss://')
self.BROKER_USE_SSL = self.CELERY_BROKER_URL.startswith('rediss://') if self.CELERY_BROKER_URL else False

# ------------------------
# Code Execution Sandbox Configurations.
Expand Down
26 changes: 26 additions & 0 deletions api/configs/app_configs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from pydantic_settings import BaseSettings, SettingsConfigDict

from configs.build_info import BuildInfo
from configs.deployment_configs import DeploymentConfigs
from configs.endpoint_configs import EndpointConfigs
from configs.http_configs import HttpConfigs
from configs.logging_configs import LoggingConfigs
from configs.security_configs import SecurityConfigs


class AppConfigs(
# pydantic-settings
BaseSettings,

# app configs
BuildInfo,
DeploymentConfigs,
EndpointConfigs,
HttpConfigs,
LoggingConfigs,
SecurityConfigs,
):
# read from dotenv format config file
model_config = SettingsConfigDict(
env_file='.env',
env_file_encoding='utf-8')
17 changes: 17 additions & 0 deletions api/configs/build_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from pydantic import BaseModel, Field


class BuildInfo(BaseModel):
"""
Build information
"""

CURRENT_VERSION: str = Field(
description='Dify version',
default='0.6.11',
)

COMMIT_SHA: str = Field(
description="SHA-1 checksum of the git commit used to build the app",
default='',
)
16 changes: 16 additions & 0 deletions api/configs/deployment_configs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from pydantic import BaseModel, Field


class DeploymentConfigs(BaseModel):
"""
Deployment configs
"""
EDITION: str = Field(
description='deployment edition',
default='SELF_HOSTED',
)

DEPLOY_ENV: str = Field(
description='deployment environment, default to PRODUCTION.',
default='PRODUCTION',
)
39 changes: 39 additions & 0 deletions api/configs/endpoint_configs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from pydantic import AliasChoices, BaseModel, Field


class EndpointConfigs(BaseModel):
"""
Module URL configs
"""
CONSOLE_API_URL: str = Field(
description='The backend URL prefix of the console API.'
'used to concatenate the login authorization callback or notion integration callback.',
default='https://cloud.dify.ai',
)

CONSOLE_WEB_URL: str = Field(
description='The front-end URL prefix of the console web.'
'used to concatenate some front-end addresses and for CORS configuration use.',
default='https://cloud.dify.ai',
)

SERVICE_API_URL: str = Field(
description='Service API Url prefix.'
'used to display Service API Base Url to the front-end.',
default='https://api.dify.ai',
)

APP_WEB_URL: str = Field(
description='WebApp Url prefix.'
'used to display WebAPP API Base Url to the front-end.',
default='https://udify.app',
)

FILES_URL: str = Field(
description='File preview or download Url prefix.'
' used to display File preview or download Url to the front-end or as Multi-model inputs;'
'Url is signed and has expiration time.',
validation_alias=AliasChoices('FILES_URL', 'CONSOLE_API_URL'),
alias_priority=1,
default='https://cloud.dify.ai',
)
11 changes: 11 additions & 0 deletions api/configs/http_configs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from pydantic import BaseModel, Field


class HttpConfigs(BaseModel):
"""
HTTP configs
"""
API_COMPRESSION_ENABLED: bool = Field(
description='whether to enable HTTP response compression of gzip',
default=False,
)
28 changes: 28 additions & 0 deletions api/configs/logging_configs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from pydantic import BaseModel, Field


class LoggingConfigs(BaseModel):
"""
Logging configs
"""

LOG_LEVEL: str = Field(
description='Log output level, default to INFO.'
'It is recommended to set it to ERROR for production.',
default='INFO',
)

LOG_FILE: str = Field(
description='logging output file path',
default='',
)

LOG_FORMAT: str = Field(
description='log format',
default='%(asctime)s.%(msecs)03d %(levelname)s [%(threadName)s] [%(filename)s:%(lineno)d] - %(message)s',
)

LOG_DATEFORMAT: str = Field(
description='log date format',
default='',
)
16 changes: 16 additions & 0 deletions api/configs/security_configs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from typing import Optional

from pydantic import BaseModel, Field


class SecurityConfigs(BaseModel):
"""
Secret Key configs
"""
SECRET_KEY: Optional[str] = Field(
description='Your App secret key will be used for securely signing the session cookie'
'Make sure you are changing this key for your deployment with a strong key.'
'You can generate a strong key using `openssl rand -base64 42`.'
'Alternatively you can set it with `SECRET_KEY` environment variable.',
default=None,
)
11 changes: 11 additions & 0 deletions api/configs/update_configs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from pydantic import BaseModel, Field


class HttpConfigs(BaseModel):
"""
HTTP configs
"""
CHECK_UPDATE_URL: str = Field(
description='url for checking updates',
default='https://updates.dify.ai',
)
2 changes: 1 addition & 1 deletion api/extensions/ext_compress.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


def init_app(app: Flask):
if app.config.get('API_COMPRESSION_ENABLED', False):
if app.config.get('API_COMPRESSION_ENABLED'):
from flask_compress import Compress

app.config['COMPRESS_MIMETYPES'] = [
Expand Down
21 changes: 20 additions & 1 deletion api/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions api/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ lxml = "5.1.0"
xlrd = "~2.0.1"
pydantic = "~2.7.4"
pydantic_extra_types = "~2.8.1"
pydantic-settings = "~2.3.3"
pgvecto-rs = "0.1.4"
firecrawl-py = "0.0.5"
oss2 = "2.18.5"
Expand Down
1 change: 1 addition & 0 deletions api/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ azure-identity==1.16.1
lxml==5.1.0
pydantic~=2.7.4
pydantic_extra_types~=2.8.1
pydantic-settings~=2.3.3
pgvecto-rs==0.1.4
tcvectordb==1.3.2
firecrawl-py==0.0.5
Expand Down
Loading

0 comments on commit e584d37

Please sign in to comment.