Skip to content

Server

Server is the entry point for the application. Implemented using Flask and SocketIO for the API and WebSocket.

Server Config

By default, the server is configured to run in development mode. To run in production mode, set the SERVER_ENV environment variable to production.

director.entrypoint.api.server.LocalAppConfig

Bases: BaseAppConfig

Local configuration for the app. All the default values can be change using environment variables. e.g. SERVER_PORT=8001

DEBUG class-attribute instance-attribute

DEBUG = 1

Debug mode for the app.

SECRET_KEY class-attribute instance-attribute

SECRET_KEY = 'secret'

Secret key for the app.

LOGGING_CONFIG class-attribute instance-attribute

LOGGING_CONFIG = LOGGING_CONFIG

Logging configuration for the app.

DB_TYPE class-attribute instance-attribute

DB_TYPE = 'sqlite'

Database type for the app.

HOST class-attribute instance-attribute

HOST = '0.0.0.0'

Host for the app.

PORT class-attribute instance-attribute

PORT = 8000

Port for the app.

TESTING class-attribute instance-attribute

TESTING = 0

Testing mode for the app.

director.entrypoint.api.server.ProductionAppConfig

Bases: BaseAppConfig

Production configuration for the app. All the default values can be change using environment variables. e.g. SERVER_PORT=8001

LOGGING_CONFIG class-attribute instance-attribute

LOGGING_CONFIG = LOGGING_CONFIG

Logging configuration for the app.

DB_TYPE class-attribute instance-attribute

DB_TYPE = 'sqlite'

Database type for the app.

HOST class-attribute instance-attribute

HOST = '0.0.0.0'

Host for the app.

PORT class-attribute instance-attribute

PORT = 8000

Port for the app.

DEBUG class-attribute instance-attribute

DEBUG = 0

Debug mode for the app.

TESTING class-attribute instance-attribute

TESTING = 0

Testing mode for the app.

SECRET_KEY class-attribute instance-attribute

SECRET_KEY = 'production'

Secret key for the app.

Server Initialization

director.entrypoint.api.create_app

create_app(app_config)

Create a Flask app using the app factory pattern.

Parameters:

Name Type Description Default
app_config object

The configuration object to use.

required

Returns:

Type Description

A Flask app.

Source code in backend/director/entrypoint/api/__init__.py
def create_app(app_config: object):
    """
    Create a Flask app using the app factory pattern.

    :param app_config: The configuration object to use.
    :return: A Flask app.
    """
    app = Flask(__name__)

    # Set the app config
    app.config.from_object(app_config)
    app.config.from_prefixed_env(app_config.ENV_PREFIX)
    CORS(app)

    # Init the socketio and attach it to the app
    socketio.init_app(
        app,
        cors_allowed_origins="*",
        logger=True,
        engineio_logger=True,
        reconnection=False if app.config["DEBUG"] else True,
    )
    app.socketio = socketio

    # Set the logging config
    dictConfig(app.config["LOGGING_CONFIG"])

    with app.app_context():
        from director.entrypoint.api import errors

    # register blueprints
    app.register_blueprint(agent_bp)
    app.register_blueprint(session_bp)
    app.register_blueprint(videodb_bp)
    app.register_blueprint(config_bp)

    # register socket namespaces
    socketio.on_namespace(ChatNamespace("/chat"))

    return app