This repository contains the prototype of a stats app.
Architecture and features:
- two services:
backend
anddatabase
- two RestAPI endpoints:
POST /messages
- adds a message to the databaseGET /stats
- retrieves the required stats
- the app can be started with an empty database,
or it can populate the database at start time with pre-defined or random data
(for more see:
backend/main.py
andbackend/fill_db.py
)
Tech stack:
docker
- containerize the servicesdocker compose
- orchestrate the servicesPostgreSQL
- databasePython
- languageFastAPI
- web frameworkSQLAlchemy
- library for database interactionPydantic
- library for model definition and validation
Step 1:
- inside your terminal run
docker compose up
- starts the backend and database services
- populates the database with pre-defined messages (see:
backend/main.py
andbackend/fill_db.py
)
Step 2:
- inside your browser access http://localhost/docs
- interact with the API using the documentation generated by Swagger UI
Note:
If you want to start the application with an empty database or to populate the database with random data,
please follow steps:
- reset the database using the instructions below
- follow the instructions inside
backend/main.py
andbackend/fill_db.py
to run the app with the desired configurations
docker compose up
- start the database and the backend services
docker compose down
- stop the services, removes the associated containers and the docker network
docker volume ls # list the docker volumes
docker compose down # stop the services and removes the docker network
docker volume rm interview_task_stats_app_storage-vol # remove the storage and the database
The application has been manually tested with a bunch of various inputs. Besides this, it has been also tested with 1.000.000 database entries.
While Pydantic
does various validations ensuring the type checks for the endpoints (see main.py
),
the database operations (see crud.py
) would have benefited from a suite of unit tests.
This section highlights a few of the further improvements that can be added:
- hide the credentials from
.env
file - make use of the async functionality of FastAPI
- write unit tests and integration tests
- run tests automatically for every pull-request with tools like GitHub actions or Travis CI
- setup deployment pipelines