Welcome to the SAP BTP Flask Starter App! This repository is your one-stop solution for building and deploying production-grade Flask applications on SAP Business Technology Platform (BTP). With built-in support for XSUAA authentication & authorization, HANA database integration, and MTA deployment, this project is designed to help developersβboth beginners and expertsβhit the ground running. π
-
π§βπ» Friendly Developer-Experience (DX) Developer Experience was the top goal of this project. Use Basic Auth for local development while using XSUAA Auth for production. Similarly, use SQLite for local development while using SAP HANA for production.
-
π XSUAA Authentication & Authorization
Secure your app with SAP's XSUAA service for seamless authentication and role-based access control. -
π οΈ HANA Integration
Leverage SAP HANA for robust database management with SQLAlchemy and@sap/cds-dk
for HDI artifact generation. -
π¦ MTA Deployment
Effortlessly deploy your app using the Multi-Target Application (MTA) framework. -
β‘ Local & Production Configurations
Automatically adapt to local or production environments with minimal setup by settingFLASK_ENV
environment variable -
π CF Logging
Integrated Cloud Foundry logging for better observability usingcf-python-logging-support
-
π Gunicorn for Production
Run your app with the high-performance Gunicorn WSGI server in production. -
π©Ί Health Check Endpoint
Monitor app and database liveliness via the/health
endpoint.
- Time-Saving: Pre-configured for SAP BTP, so you can focus on building features.
- Best Practices: Implements SAP standards for BTP apps.
- Scalable: Designed to grow with your application needs.
- Beginner-Friendly: Easy-to-follow setup and documentation.
This repository is a template repository. To start your own project based on this starter app:
- Click the green "Use this template" button at the top of the GitHub repository page.
- Choose "Create a new repository" and fill in your desired repository name and settings.
- Follow the Getting Started instructions below to set up your environment.
Tip
Using the template ensures you start with a clean commit history and can customize your project independently.
-
Clone the Repository
git clone https://github.com/anselm94/sapbtp-flask-bookstore.git cd sapbtp-flask-bookstore
-
Create and Activate a Virtual Environment
python -m venv .venv source .venv/bin/activate
-
npm install
-
Deploy DB artefacts
Deploy DB artefacts to local SQLite DB file.
# in root project dir npm run deploy:sqlite
[!NOTE]
You must repeat the above command everytime for every CDS file change in
db/
folder -
Install Poetry Dependencies
cd srv && poetry install
-
Start the Local Server
The local server starts with SQLite as DB and Basic User (username:
me
& password:me
)# in `srv` folder python -m flask run
-
Test the API
- Health Check
GET http://127.0.0.1:5000/health
- Get
books
API
GET http://127.0.0.1:5000/api/v1/books Authorization: Basic me:me
-
Run Tests
# in `srv` folder python -m pytest
-
See Coverage Reports
- Open generated HTML coverage reports in
srv/htmlcov/
folder
- Open generated HTML coverage reports in
-
Build the MTA Project
# in project root dir mbt build
-
Push the Artefact
# in project root dir cf deploy mta_archives/sapbtp-flask-bookstore_1.0.0.mtar
-
Test the API
Check the BTP Cockpit for the URL.
- Health Check
GET https://<BTP_APP_DEPLOYMENT_URL>/health
- Get
books
API
Get an
oauth2_token
before running the following checkGET http://<BTP_APP_DEPLOYMENT_URL>/api/v1/books Authorization: Bearer <oauth2_token>
Tip
In order to replicate the PRODUCTION
in the local by connecting to HANA DB and use oAuth2 authentication flow, you can replicate the VCAP_SERVICES
in the local and set FLASK_ENV=PRODUCTION
. See config.py
for more info
- Copy
.env.example
to.env
- Run the setup script to populate
VCAP_SERVICES
sh setup-env.sh
- Run the app locally
cd srv FLASK_ENV=PRODUCTION python -m flask run
- Test the API with oAuth2 credentials
sapbtp-flask-bookstore/
βββ db/ # CDS schema and data files
β βββ schema.cds # Core Data Services (CDS) schema definition
β βββ data/ # Sample data for the database
βββ gen/ # Generated HANA artifacts
βββ srv/ # Flask application source code
β βββ app.py # Entry point for the Flask application
β βββ config.py # Configuration management for different environments
β βββ app/ # Application modules
β β βββ database.py # Database connection and setup logic
β β βββ models.py # ORM models for database tables
β β βββ routes/ # API route definitions
β β β βββ __init__.py # Route initialization
β β βββ services/ # Business logic and service layer
β β β βββ books_service.py # Service logic for book-related operations
β β βββ utils/ # Utility functions for authentication and error handling
β β β βββ auth_utils.py # Authentication helper functions
| β β βββ heathcheck_utils.py # Healthcheck helper functions
β βββ tests/ # Unit Tests
βββ mta.yaml # MTA deployment descriptor
βββ setup-env.sh # Script to configure local environment
βββ README.md # Project documentation
-
Authentication & Authorization
- Use
@login_required
for any route, which needs authentication - Use
@roles_required(["role-1", "role-2"])
for any route, which needs necessary scope authorization
Local Development
- Uses Basic Auth for quick and easy development in the local - See
srv/config.py
,srv/app/routes/__init__.py
&srv/app/utils/auth_utils.py
- Validate Role-based access control as defined for mock users in
srv/config.py
- Configure default mock users and roles in
srv/config.py
Production
- Uses SAP XSUAA for OAuth2-based authentication & authorization - See
srv/config.py
,srv/app/routes/__init__.py
&srv/app/utils/auth_utils.py
- Role-based access control is defined in
xs-security.json
.
- Use
-
Database Integration
- All DB artefacts are modelled in SAP CDS - Conceptual Definition Language format - See
db/schema.cds
- Uses
@sap/cds-dk
CLI for compiling CDS CDL to target DB DDL format and deploy them at ease. - SQLAlchemy is used for ORM mapping in the Flask app (
srv/app/models.py
) using 'SQLite dialect' or 'SAP HANA dialect'. - Easily load default CSV data configurations - see
db/data/
- CDS models (
db/schema.cds
) are converted to HANA HDI artifacts using@sap/cds-dk
.
Local Development
- Run
npm run deploy:sqlite
in the root project dir to deploy the latest CDS changes indb/
folder to the local SQLite DB files.
Production
- Use SAP HANA Deployer -
@sap/hdi-deploy
for automated HANA artefact migration and deployment
- All DB artefacts are modelled in SAP CDS - Conceptual Definition Language format - See
-
Health Checks
- Checks for dependent services' availability for health check endpoint
/health
- seesrv/app/utils/heathcheck_utils.py
- Checks for dependent services' availability for health check endpoint
-
Automated Tests
- Uses
pytest
framework withpytest-flask
for automated tests. - Uses
pytest-cov
for coverage reporting
- Uses
-
Deployment
- MTA framework bundles the app, database, and services for deployment.
cf deploy
handles the deployment to SAP BTP.
- Debugging: Use VS Code's built-in debugger with the provided
launch.json
. - Extending:
- Add new routes in
srv/app/routes/
- Add new services in
srv/app/services/
. - Add new models in
db/schema.cds
andsrv/app/models.py
- Add new routes in
This project is licensed under the MIT License.
Special thanks to the SAP BTP community and contributors for their support and inspiration!