An open API service indexing awesome lists of open source software.

https://github.com/akaliutau/python-microservices

Microservices in Python
https://github.com/akaliutau/python-microservices

flask python3 sqlalchemy

Last synced: about 2 months ago
JSON representation

Microservices in Python

Awesome Lists containing this project

README

          

RESTful Python microservice
=============================

This code is an example of RESTful service web app.

It is a Flask application (https://www.djangoproject.com/) that creates a RESTful backend to store user's posts. In other
words it's a nano-twitter application

Overview
=========
The format of a Tweet POJO is as follows:

```
{
id integer
username string
text string
timestamp string($date-time)
}
```

API has the following contract:

public api:

| Method | Endpoint |Secure| Description |
|--------|----------------------------|------|------------------------------------------------|
|GET | /api/me/tweets/ | Yes | List of tweets of the user |
|POST | /api/me/tweets/ | Yes | The newly created tweet |
|GET | /api/tweets/ | No | List of all tweets |
|GET | /api/tweets/{id}/ | No | The tweet with ID=id |
|GET | /api/tweets/?search=token | No | Searches all the tweets that contain token |

private api:

| Method | Endpoint |Secure| Description |
|--------|----------------------------|------|------------------------------------------------|
|DELETE | /admin/tweets/{id}/ | No | Deletes tweet with ID=id |

Database has the following schema (created through SQLAlchemy https://www.sqlalchemy.org/):

```
id INTEGER NOT NULL Primary key
username VARCHAR(50)
text VARCHAR(250)
timestamp DATETIME Creation time
```

Build and run local service - nanotwitter
==========================================

Create a virtual environment and install the requirements

```
python3 -m venv ./venv
source ./venv/bin/activate OR .\venv\Scripts\activate
pip install -r requirements.txt
```

Get the local database ready

```
python -m nanotwitter.init_db
```

Generate the API token:
```
python -m nanotwitter.gen_token
Bearer eyJ0eXAiOiJK... ...YVCnAAo4kLXO8Fg
```

Start the development server

```
set FLASK_APP=.\nanotwitter\wsgi.py
set FLASK_ENV=development

flask run
```

Check the service at http://127.0.0.1:5000/

Build and run dockerized microservice - nanotwitter_pg
=======================================================

Note, there are tiny differences between nanotwitter_pg and nanotwitter_pg - in a way of working with DB layer and in
dependencies

Use this command to build and spin up containers:

```
docker-compose -f docker-compose-pg.yaml up --build
```

and this one to destroy them:

```
docker-compose -f docker-compose-pg.yaml down
```
Check the service at http://127.0.0.1:5000/

Tests
======

Run the unit tests with

```
pytest
```

Dependencies
=============

This application uses:

* Flask as a web framework ()

* Flask RESTplus for creating the web interface, similar to Swagger (https://flask-restplus.readthedocs.io/en/stable)

* SQLAlchemy to handle the database models (https://www.sqlalchemy.org/)

* SQLlite database for local development and U-tests ()