A simple real-time chat application built with Flask and PostgreSQL, designed for Clever Cloud deployment.
In this demo is designed for two kind of deployment:
- A native chat based on PostgreSQL backend without authentication.
- Adding a Keycloak based authentication and user management layer.
This is why there is two files main.py and main_with_keycloak.py, and two indexes index.html and secure_index.html.
- Python 3.12+
- PostgreSQL Clever Cloud add-on
- (optionnal) Keycloak Clever Cloud add-on
- Add your Organisation ID in
terraform/clevercloud.tffile (line 13) - Initialize Terraform:
terraform init
terraform applyto remove the resources created by terraform, run
terraform destroy
For uv projects on Clever Cloud, the application must listens on port 9000 with 0.0.0.0 as host.
- Run Command: Set the environment variable:
CC_RUN_COMMAND="uv run main.py"
- Clever Cloud account
- Clever Cloud CLI installed (optional)
- Login (if not already logged in):
clever login- Create a PostgreSQL add-on on Clever Cloud
clever addon create postgresql-addon --org [ORGANISATION ID] minichat_postgresql- Create a Python application:
clever create minichat --type python --region par --org [ORGANISATION ID]- Link the add-on to your application:
clever service link-addon minichat_postgresql- Set up environment variable:
clever env set CC_PYTHON_VERSION "3.13"
clever env set CC_RUN_COMMAND "uv run main.py"- Deploy:
clever deploy- Create a Keycloak add-on:
clever addon create keycloak --org [ORGANISATION ID] minichat_keycloak- Link the add-on to your application:
clever service link-addon minichat_keycloak- Set up environment variable:
clever env set CC_PYTHON_VERSION "3.13"
clever env set CC_RUN_COMMAND "uv run main_with_keycloak.py"-
Set up Keycloak:
-
8.1 Set up the Keycloak administration
-
Go to the Keycloak admin console and create a new client.
- Admin URL:
clever env | grep CC_KEYCLOAK_ADMIN_URL - Initial password:
clever env | grep CC_KEYCLOAK_ADMIN_DEFAULT_PASSWORD - You will have to change it at the first login
- Admin URL:
-
Manage Realm menu:
- Top corner left, default realm name is
master, you can create a new one or not.
- Top corner left, default realm name is
-
Set up a client:
You will need to provide the full URL of your application. (Available in Domain names from the Clever Cloud console or with the CLI
clever domain)- Left panel ->
Clients->Create a client- Client ID:minichat- Client authentication:On- Fill Valid Redirect URIs and Web Origins with the URL of your application. Mind thehttp://orhttps://prefix - examplehttp://APPID.cleverapps.io/
- Left panel ->
-
Set up the Realm
- Left panel ->
Realm settings->Logintab- Turn on
User registrationoption
- Turn on
- ->
User profiletab- Manage fields required during the registration
- Left panel ->
-
-
8.2 Update your application environment variable
-
Get the following information from the Keycloak admin: Keakcloak realm name (default
master), Keycloak client ID (we set;minichat), Keycloak client secret (admin Keycloak, Left panel ->Clients->minichattab ->Credentialstab) -
Update your application environment variable
clever env set KEYCLOAK_REALM "master"clever env set KEYCLOAK_CLIENT_ID "minichat"clever env set KEYCLOAK_CLIENT_SECRET "[THE SECRET YOU GET]"
-
-
-
Deploy or restart your apllication to appply chages:
clever deploy
clever restart