This repository contains a Dockerized Flask project that includes an API to retrieve users from the database with pagination, filtration and simple caching support.
- Python3.8
- Docker version 20.10.12, build 20.10.12-0ubuntu2~20.04.1
- Docker Compose version v2.6.1
- GNU Make 4.2.1
- heroku/7.60.2 linux-x64 node-v14.19.0
- psql (PostgreSQL) 12.11 (Ubuntu 12.11-0ubuntu0.20.04.1)
- gunicorn (version 20.1.0)
- Create
powertofly-be-challenge/.env
directory and add the following files:- .env
FLASK_APP=api/__init__.py FLASK_ENV=development POSTGRES_USER=powertofly POSTGRES_PASSWORD=powertofly POSTGRES_DB=powertofly_dev POSTGRES_HOST=postgres POSTGRES_PORT=5432
- .env.db
POSTGRES_USER=powertofly POSTGRES_PASSWORD=powertofly POSTGRES_DB=powertofly_dev
- .env
Note: These environment variables should be set according to the production so change them while production deployment.
- Create DB:
powertofly_dev
- Create User:
powertofly
- Grant privileges on DB to User.
make build_and_run yml=docker-compose.yml
make logs yml=docker-compose.yml
curl http://127.0.0.1:5000/
This should print OK
if ran successfully.
./deploy.sh <docker-username> <docker-repository> <environment>
- development
- production
This API is to check the health of the Flask application.
This API fetches the users from the database using the filtration, pagination and caches them accordingly.
It implements:
- Pagination
- Filtration
- Caching
- Template rendering for showing users.
-
page: Page number. By default it is
1
. (integer) -
per_page: Rows per page. By default it is
50
. (integer) - filter: Search string from User table. Only checks in email & country columns and filters records. Default value is empty string. (string)
-
format: Output format e.g. json/html. By default it is
json
. (integer)
{
"curr_page":1,
"prev_page":null,
"next_page":2,
"total_pages":500000,
"data": [
{
"id":1,
"name":"User 1",
"email":"user_1@powertofly.com",
"country":"Isle of Man",
"created_at":"Thu, 14 Jul 2022 21:10:27 GMT"
},
{
"id":2,
"name":"User 2",
"email":"user_2@powertofly.com",
"country":"American Samoa",
"created_at":"Thu, 14 Jul 2022 21:10:27 GMT"
}
]
}