kdubba14 / python-flask-rest-microservice

Python Flask REST API Micro service with SQL Alchemy

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Python Flask REST Microservice

REST API written in Python Flask using SQLAlchemy as the ORM with auto migration capabilities

Pre-requisites

 python -m pip install -U pip 
  • Download & Install MySQL Server locally or use an external database (OPTIONAL)

For Developers

  • Download & install NodeJS
  • Install nodemon (use sudo if you in linux)
npm i -g nodemon

Installation

# Clone the repository 
git clone https://github.com/dipayan/python-flask-rest-microservice.git
# Change into the directory
cd python-flask-rest-microservice
# Install all required dependencies with
// skip // pipenv install --deploy --skip-lock
// skip // ('pip' or 'pip3') install psycopg2
pipenv install pylint --dev
# Activate the project virtual environment
pipenv shell
# Create an local .env file and replace with the relevant values
cp .env.sample .env

You can also set the enviroment variables explicity (OPTIONAL)

PORT=9400
BUILD_DEV=development
DATABASE_URL=postgres://name:password@host:port/dbname

Running the application

Start the app

python run.py

Start the app for developers

nodemon run.py

Usage

API Specifications

  • GET: /api/v1/todos/
  • GET: /api/v1/todos/?title=
  • GET: /api/v1/todos/{todo_id}
  • POST: /api/v1/todos/
  • PUT: /api/v1/todos/{todo_id}
  • DELETE: /api/v1/todos/{todo_id}

Example Get all todos curl http://localhost:{PORT}/api/v1/todos/

Running the application as a Docker container

cd python-flask-rest-microservice
# Build the docker image 
docker build -t python-flask-app .
# Run the docker container and put the port as specified in the .env file
docker run -d -p 9000:9000 --name python-flask-app -e PORT=9000 -e BUILD_ENV='development' -e DATABASE_URL='mysql+pymysql://user:pass@host:port/dbname' python-flask-app
# Check the logs
docker logs -f python-flask-app
# Cleaup the container
docker stop python-flask-app && docker rm python-flask-app

Database Auto migration & creation

You can create the tables using the following set of commands

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

Author

Dipayan Biswas

About

Python Flask REST API Micro service with SQL Alchemy


Languages

Language:Python 99.5%Language:Dockerfile 0.5%