Skip to content

Shamilv05/store

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Store(REST API)

alt text

Configuration

First you need to install pip

If you have macOS, then you can use

sudo easy_install pip

If you have Ubuntu, then you need to use the following commands

sudo apt-get update
sudo apt-get -y install python3-pip

Now you can proceed to install the necessary libraries to run our application Create a folder with our project and a venv folder inside:

mkdir myproject
cd myproject
python3 -m venv venv

Activate the virtual environment

. venv/bin/activate

To install the libraries, you have to use requirements.txt (the virtual environment must be activated)

pip3 install -r requirements.txt

Database Migration

Before running application, you have to configure Postgres and environment variables:

export SECRET_KEY='your_secret_key'
export DATABASE_URI='your db uri'

,the next step is

python manage.py db init
python manage.py db migrate
python manage.py db upgrade

Usage

To start the service, use the following command:

python3 run.py

Running Tests

python3 -m unittest tests/test.py

Libraries Usage

Numpy

Used to calculate percentile, filling an array with zeros using numpy.zeros

Flask

Framework for writing current API

SQLAlchemy, Flask-SQLAlchemy

Used to interact with the database

MarshMallow, Flask-Marshmallow

Used to convert complex data types to Python data types

fastjsonschema

Used to validate input data. Using regular jsonschema is also possible, but then validation will be slower.

datetime

It was used to validate dates, that is, the DD.MM.YYYY format, to obtain utcnow (), and also to inadmissibly enter birthdays whose date is greater than the current one.

unittest

Used for writting tests

Deploy (Nginx + gunicorn + Supervisor)

alt text

To deploy the application we need a host

ssh username@host

The next step is to install Postgres and create a user. All the necessary steps to install Postgres on Ubuntu 18.04 are described here

Now you can start downloading files using git clone

git clone "repo"

After that, install the necessary tools and libraries

sudo apt install python3-pip
sudo apt install python3-venv
python3 -m venv store/venv

Move to the project folder

cd store
source venv/bin/activate
pip install -r requirements.txt

After that, you need to slightly modify the config.py file, since there is a more convenient way than using env variables

sudo touch /etc/config.json
sudo nano /etc/config.json

Inside we add:

{
    "SECRET_KEY": "your_secret_key",
    "DATABASE_URI": "your_db_uri"
}
sudo nano config.py

Make the following changes to config.py

import json

with open('/etc/config.json') as config_file:
    config = json.load(config_file)
    
class Config:
    SECRET_KEY = config.get("SECRET_KEY")
    SQLALCHEMY_DATABASE_URI = config.get("DATABASE_URI")
    SQLALCHEMY_TRACK_MODIFICATIONS = False

Follow the migration steps described above Install nginx & gunicorn

cd
sudo apt install nginx
pip install gunicorn

Let's dive into nginx & gunicorn configuration

sudo rm /etc/nginx/sites-enabled/default
sudo nano /etc/nginx/sites-enabled/store

And add the following to this file:

server {
    listen 8080;
    server_name <host_ip>;
    client_max_body_size 20M;
    
    location / {
        proxy_pass http://localhost:8000;
        include /etc/nginx/proxy_params;
        proxy_redirect off;
    }
}
sudo systemctl restart nginx

Now you need to configure supervisor:

sudo apt install supervisor
sudo nano /etc/supervisor/conf.d/store.conf

It is also necessary to add the following inside:

[program:store]
directory=/home/<user>/store
command=/home/<user>/store/venv/bin/gunicorn -w <num> run:app
user=<user>
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true

, where num = (2 x num_cores) + 1. You can get num_cores using the command

nproc --all

Launch:

sudo supervisorctl reload

Extra

Postman

Used to make API requests and validation.

About

REST API using FLASK

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages