Deploy kwai backend¶
This page will describe how you can deploy the backend of kwai.
Step 1: Build¶
First you need to get the latest version. The repository is a monorepo. This means the backend code and the frontend code are in the same repository. We only need the backend. Set up the development environment for kwai and build the wheel file.
cd backend
uv build --all-packages
This command creates a dist directory where you find all files: a tar.gz with the source code and a .whl file for all applications, bounded contexts and packages.
Create a requirements.txt file that contains a list of all the wheel files. For example:
kwai_core-0.3.0-py3-none-any.whl
sql_smith-1.1.2-py3-none-any.whl
kwai-0.3.0-py3-none-any.whl
kwai_bc_club-0.3.0-py3-none-any.whl
kwai_bc_identity-0.3.0-py3-none-any.whl
kwai_bc_portal-0.3.0-py3-none-any.whl
kwai_bc_teams-0.3.0-py3-none-any.whl
kwai_bc_training-0.3.0-py3-none-any.whl
kwai_api-0.3.0-py3-none-any.whl
kwai_cli-0.3.0-py3-none-any.whl
kwai_events-0.3.0-py3-none-any.whl
Step 2: Python Environment¶
Kwai needs a Python environment. It is recommended to create a separate environment for kwai. Run the following command on your host:
mkdir backend
python3 -m venv .venv
source .venv/bin/activate
Copy all wheel files and the requirements.txt file to the backend directory and install them:
pip install -r requirements.txt
Step 3: Configuration¶
Kwai needs a configuration file. When this is the first time, we need to create it.
cd backend
cp kwai.dist.toml .kwai.toml
Use an editor to change the configuration. Set the environment
variable KWAI_SETTINGS_FILE with the full path of this configuration file.
Step 4: Migration¶
When there are database changes required, a migration must run before starting the backend. Copy the migrations directory to your server and use dbmate to upgrade.
dbmate is used for database migrations. Make sure it is available.
cd <your_directory_on_the_server>/migrations
dbmate -d . -u "<database_uri>" up
Step 5: Frontend¶
The frontend is also served by the FastAPI backend.
To build the frontend you need Node.
cd frontend
npm install
npm run build
Note
You can also use Task to build the frontend. Run the following
command from the kwai root directory:
task frontend:build
This will result in a directory dist in all the frontend applications.
FastAPI will be used to render the frontend. Add all applications to the .kwai.toml configuration file:
[frontend]
path = "/home/kwai/frontend"
root_app = "portal"
[frontend.apps]
[frontend.apps.portal]
base="/apps/portal"
entries="/src/index.ts"
In this example there should be a /home/kwai/frontend/apps/portal/dist directory that contains the result of the
build command.
Step 6: Run¶
To start the backend application use the following command:
cd backend
source .venv/bin/activate
python -m kwai_api
This python script will start an uvicorn server. The host and port arguments can be used to configure this server.