FlaskWebAPI is a straightforward Flask-based API connected to a PostgreSQL database, designed to be easily containerized using Docker.
To configure FlaskWebAPI, follow these steps:
- Duplicate the
example.env
file as.env
. - Populate the
.env
file with your specific environment variable values. Default values will be used if not provided.
All database configuration settings are managed within the .env
file.
You can run FlaskWebAPI in your local environment in two ways:
Running FlaskWebAPI in a Docker container is the recommended approach, aligning with production deployment standards:
- Set
DB_HOST=docker-db
in the.env
file. - Define
TEST_HOST=0.0.0.0
and chooseTEST_PORT
for the app's listening port. - Launch the Flask app in debug mode:
- Execute
docker compose up
. - For rebuilding images and removing orphaned containers:
docker compose up --build --remove-orphans
. - Docker will build two images and start containers named
docker-db
(Postgres instance) anddocker-webapi
(the app) respectively. - Access the app at
localhost:TEST_PORT
.
- Execute
- The Postgres database initializes with default data (refer to the sql folder).
- Persistent database data is stored in
postgres_data
(new data persists across app restarts). - To reset the database, delete
postgres_data
:sudo rm -rf postgres_data
and rerundocker compose up --build
. - Inspect the
docker-db
container:docker exec -it docker-db psql -d mydatabase -U user
.
For local development outside Docker:
- Ensure Python 3.8+ is installed.
- Create and activate a virtual environment.
- Use the existing
.env
file with modifiedDB_HOST
(x.x.x.x
for remote database) andTEST_HOST=localhost
. - Install dependencies:
pip install -r requirements.txt
. - Start the app:
python3 run.py
.
Explore the API documentation via Swagger UI at http://localhost:5000/swagger-ui
.
Log files are located in logs/flaskwebapi.log
.
Custom error handling ensures consistent error responses.
Contributions such as issues or pull requests are welcome.