/qaws-alchemy

A web service application (with Flask+SQLAlchemy)

Primary LanguagePythonMIT LicenseMIT

Q-A-web-service (qaws)

Python requirement

About

A simple web service application:

  • takes POST requests in form {"questions_num": integer};
  • sends a request to a public API to get specified early number of questions (integer);
  • saves part information of received questions to own DB (in case the question is found in the DB, the application sends new requests to the public API to get unique questions);
  • responds to the client request by returning previously saved question or, if there is none, an empty object (in my case empty dictionary{}).

Tech stack:
Flask, PostgreSQL, Flask-SQLAlchemy, Docker

Docker instructions

Clone this git repository.
Before starting, install Docker Compose if you do not have it. Below it is assumed that Docker's repositories are set up. By default, the Docker daemon always runs as the root user. If you do not want to preface the docker command with sudo see this. Start Docker daemon with command:

$ sudo service docker start

Build image

Dockerfile describes modifications of Python 3.9 parent image needed to build 'qaws-app' image.
To build Docker's 'qaws-app' image, run the following from the project root directory:

$ sudo docker build --tag qaws-app .

Run containers

docker-compose.yml describes two services: 'db' ans 'web'. 'db' is service with PostgreSQL DBMS. The 'postgres' image is used to start 'db'. 'db' uses volumes at path ./data/db for containing DB data. See the reference for more information about structure docker-compose.yml. 'web' is service that runs 'qaws-app' image and also has dependency on 'db'. Make sure you create the 'qaws-app' image before starting the services.

To create and run only Docker container with PostgreSQL, run from the project root directory:

$ sudo docker compose up db

or use flag -d to start the service in the background

$ sudo docker compose up db -d

To create and run Docker container with 'qaws-app' application, run from the project root directory:

$ sudo docker compose up

To shut down running services and clean up containers, use either of these methods:

  • stop the application by typing Ctrl-C in the same shell (if the service is running in the foreground) in where you started it, then use sudo docker rm <CONTAINER ID | NAME> to remove containers (to see containers list sudo docker ps -a)
  • or switch to a different shell and run from the project root directory
$ sudo docker compose down

Application

To connect to running 'db' service with PostgreSQL, run:

$ psql -U postgres -W -h 127.0.0.1 -p 5432 postgres

Input password: 'postgres'. It is assumed you have psql - PostgreSQL interactive terminal.

To send POST request to the running 'qaws-app', use:

$ curl -X POST http://127.0.0.1:8000/ \
        -H 'Content-Type: application/json' \
        -d '{"questions_num":<your integer number>}'

for example:

$ curl -X POST http://127.0.0.1:8000/ \
        -H 'Content-Type: application/json' \
        -d '{"questions_num":1}'

or run from the project root directory:

$ ./request.sh <not negative integer number>