# /api/app_factory.pyfromdify_appimportDifyApp# ----------------------------# Application Factory Function# ----------------------------defcreate_flask_app_with_configs()->DifyApp:"""
create a raw flask app
with configs loaded from .env file
"""dify_app=DifyApp(__name__)dify_app.config.from_mapping(dify_config.model_dump())# add before request hook@dify_app.before_requestdefbefore_request():# add an unique identifier to each requestRecyclableContextVar.increment_thread_recycles()returndify_app
# api/extensions/ext_mail.pyimportloggingfromtypingimportOptionalfromflaskimportFlaskfromconfigsimportdify_configfromdify_appimportDifyAppclassMail:def__init__(self):self._client=Noneself._default_send_from=Nonedefis_inited(self)->bool:returnself._clientisnotNonedefinit_app(self,app:Flask):mail_type=dify_config.MAIL_TYPEifnotmail_type:logging.warning("MAIL_TYPE is not set")returnifdify_config.MAIL_DEFAULT_SEND_FROM:self._default_send_from=dify_config.MAIL_DEFAULT_SEND_FROMmatchmail_type:case"resend":...case"smtp":...defis_enabled()->bool:returndify_config.MAIL_TYPEisnotNoneanddify_config.MAIL_TYPE!=""definit_app(app:DifyApp):mail.init_app(app)mail=Mail()
# api/.env.example# 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.SECRET_KEY=# Console API base URLCONSOLE_API_URL=http://127.0.0.1:5001
CONSOLE_WEB_URL=http://127.0.0.1:3000
# Service API base URLSERVICE_API_URL=http://127.0.0.1:5001
# Web APP base URLAPP_WEB_URL=http://127.0.0.1:3000
# Files URLFILES_URL=http://127.0.0.1:5001
# The time in seconds after the signature is rejectedFILES_ACCESS_TIMEOUT=300# Access token expiration time in minutesACCESS_TOKEN_EXPIRE_MINUTES=60# Refresh token expiration time in daysREFRESH_TOKEN_EXPIRE_DAYS=30# ...
# api/configs/app_config.pyclassDifyConfig(# Packaging infoPackagingInfo,# Deployment configsDeploymentConfig,...):model_config=SettingsConfigDict(# read from dotenv format config fileenv_file=".env",env_file_encoding="utf-8",# ignore extra attributesextra="ignore",)
其他具体配置类(以PackagingInfo为例)代码示例如下。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# api/configs/packaging/__init__.pyfrompydanticimportFieldfrompydantic_settingsimportBaseSettingsclassPackagingInfo(BaseSettings):"""
Packaging build information
"""CURRENT_VERSION:str=Field(description="Dify version",default="1.4.2",)COMMIT_SHA:str=Field(description="SHA-1 checksum of the git commit used to build the app",default="",)
# api/controllers/console/error.pyfromlibs.exceptionimportBaseHTTPExceptionclassAlreadySetupError(BaseHTTPException):error_code="already_setup"description="Dify has been successfully installed. Please refresh the page or return to the dashboard homepage."code=403classNotSetupError(BaseHTTPException):error_code="not_setup"description=("Dify has not been initialized and installed yet. ""Please proceed with the initialization and installation process first.")code=401classNotInitValidateError(BaseHTTPException):error_code="not_init_validated"description="Init validation has not been completed yet. Please proceed with the init validation process first."code=401