commit | author | age
|
4829e4
|
1 |
# Gossip Service |
J |
2 |
|
|
3 |
The Gossip Service is a small application written in [Python](https://www.python.org/) + [Flask](https://flask.palletsprojects.com) |
|
4 |
which exposes a variable endpoint in order to retrieve news from a specific topic. It Comes with CORS enabled so it can |
|
5 |
be used from browsers. |
|
6 |
|
|
7 |
## How it works |
|
8 |
|
|
9 |
The application exposes a `GET` endpoint `/news/<topic>` on port `5000` by default. Once a GET request hits the service, |
|
10 |
the application uses the `<topic>` part of the endpoint to find a json file with that name. |
|
11 |
|
|
12 |
In the case of not being able to locate a file with the `<topic>` name, the application throws a 404 error. In any other |
|
13 |
case, the application loads the list of news from the json file, picks 3 randomly and returns the list sorted by their |
|
14 |
timestamp. |
|
15 |
|
|
16 |
Template of the json used to store the news: |
|
17 |
```json |
|
18 |
{ |
|
19 |
"data": [ |
|
20 |
{ |
|
21 |
"anyFieldKey": "anyFieldValue", |
|
22 |
"timestamp": 1578454302 |
|
23 |
} |
|
24 |
] |
|
25 |
} |
|
26 |
``` |
|
27 |
|
|
28 |
The only requirements for the json files are: |
|
29 |
- Be a well formed JSON file |
|
30 |
- The `data` field should exists and stores the list of news objects |
|
31 |
- Each news element should have a `timestamp` field |
|
32 |
|
|
33 |
## Installation |
|
34 |
|
|
35 |
The Gossip Service is a Python application with Flask so, in order to run in your local machine you need Python + some |
|
36 |
libraries. |
|
37 |
|
|
38 |
A simple approach is to have different Python virtual environments per project. Execute the following command in the |
|
39 |
root of this project ot install a virtual environment: |
|
40 |
|
|
41 |
``` |
|
42 |
$ python3 -m virtualenv venv |
|
43 |
``` |
|
44 |
|
|
45 |
Once the virtual environment is installed in the project, you need to activate this new virtual environment. Execute the |
|
46 |
following command to activate it: |
|
47 |
|
|
48 |
``` |
|
49 |
. venv/bin/activate |
|
50 |
``` |
|
51 |
|
|
52 |
After the activation of the virtual environment you need to install all the requirements for the service (unless you did |
|
53 |
this step before). Execute the following command to install all the required libraries: |
|
54 |
|
|
55 |
``` |
|
56 |
pip install -r requirements.txt |
|
57 |
``` |
|
58 |
|
|
59 |
## Running Gossip in local |
|
60 |
|
|
61 |
To run the Gossip Service in local, execute the following commands: |
|
62 |
|
|
63 |
``` |
|
64 |
$ cd src |
|
65 |
$ export FLASK_APP=gossip.py |
|
66 |
$ flask run |
|
67 |
``` |
|
68 |
|
|
69 |
## Running Gossip in a containers environment |
|
70 |
|
|
71 |
In order to run the Gossip Service in a containers environment you need a container image. A prebuilt image is |
|
72 |
available in [quay.io](https://quay.io/repository/psolarvi/python-flask-gossip) |
|
73 |
|