Skip to content

Integration to queue messages in RabbitMQ backend prior to forward them to BigPanda's Alert API

License

Notifications You must be signed in to change notification settings

cjcrist/Panda-Flask-API

Repository files navigation

Panda Flask API

This is the 2nd part of the Panda-Weather-Alerts integration to forward AccuWeather alerts to BigPanda's Alert API via the use of a RabbitMQ message broker. This Flask API runs on a uWSGI web server and has endpoints to communicate with a RabbitMQ message broker.

Flowchart

graph TD;
    A(alerts2panda) --> |request current conditions| B[[AccuWeather]];
    B --> |Response with current conditions| A;
    A --> |Send data| C((Panda Flask API));
    A --> |Get Queue Status| C;
    A --> |HealthCheck| C;
    C --> |Queue message| D[(RabbitMQ)];
    D --> |Consume Queue| C;
    C --> |Send Alert| E[[BigPanda Alert API]];
    C --> |Send to DLQ on failure| D;
    
%% Set the classes.
classDef app fill:#8FD27F,font-size:16px,color:#000000;
classDef api fill:#A2C3FB,font-size:16px,color:#000000;
classDef db fill:#FABE78,font-size:16px,color:#000000;
%% Assign the classes.
class A app;
class B,C,E api;
class D db;
Loading

Deployments

Running the server deployed in docker containers (Recommended)

docker-compose is used to orchestrate proper configuration of all containers. An NGINX reverse proxy is deployed to send requests between both the RabbitMQ management console and Panda Flask API.

Requirements

There are a few steps that need happen to deploy the containers in docker. You can read through docker-compose.yml to get the full picture, but to make it easy, all the required files are in the etc directory. You still need to add a few things, which I'll go over.

  • Make the required directories in /etc
    • sudo mkdir -p /etc/nginx /etc/panda-flask /etc/rabbitmq
    • If you decide to create these somewhere else, make sure to update the paths in docker-compose.yml
  • Copy the contents of the etc directory to /etc
    • sudo cp etc/nginx/* /etc/nginx/
    • sudo cp etc/panda-flask/* /etc/panda-flask/
    • sudo cp etc/rabbitmq/* /etc/rabbitmq/
  • Docker secrets are used to obfuscate the BigPanda app key and bearer token. Add them to the files in /etc/panda-flask directory.
  • The Flask config file is included in the etc/panda-flask directory. If anything is changed, /etc/panda-flask should be updated to reflect the changes.
  • uwsgi uses 2 types of connections, either by socket or http. The appropriate uwsgi.ini file is added to the etc/panda-flask directory.
    • For docker, uwsgi should use a unix socket connection.
  • docker-compose gives us access to route requests via the service name. Make sure the pandaflaskapi.yml is pointing to rabbimq-server, and not localhost.
    • The same should be configured for the respective nginx.conf files.
  • docker-compose services rely on 2 docker networks, panda_flask (Which is a frontend network), and panda_flask_backend. Create these networks as follows:
    • docker network create panda_flask
    • docker network create panda_flask_backend

The /etc/ setup should look as follows:

/etc
├── nginx
│   ├── panda.conf
│   ├── proxy.conf
│   └── rabbit.conf
├── panda-flask
│   ├── panda_app_key
│   ├── panda_bearer_token
│   ├── pandaflaskapi.yml
│   ├── rabbit_pass
│   ├── rabbit_user
│   └── uwsgi.ini
└── rabbitmq
    ├── definitions.json
    ├── enabled_plugins
    └── rabbitmq.conf

Testing on localhost

NGINX is configured to use test domains to forward requests to RabbitMQ management console and Flask OpenAPI docs respectively. To set this up on linux, edit the /etc/hosts file with something similar to the following:

  • 127.0.0.1 localhost rabbitmq.local panda-server.local

Running the deployment

Now that everything is set up, all that's left to do is run docker-compose. The Flask API waits for RabbitMQ to report a healthy condition before starting up. This could take about 10-15 seconds. From the root directory of the project, run the following command:

  • docker-compose up -d
    • 3 containers should start: nginx-proxy, rabbitmq, panda-flask.
    • To tear these down, you can run docker-compose down. All containers will be stopped and deleted.

A few test steps you can take to verify everything is up and running correctly is to view the logs.

  • docker logs nginx-proxy
  • docker logs rabbitmq
  • docker logs panda-flask

Docs and management site access

With everything up and running, you should be able to access the RabbitMQ management console. In a browser, navigate to:

  • http://rabbitmq.local

To view the OpenAPI docs for the Panda Flask API, navigate to:

  • http://panda-server.local/api/v1/ui

Running the server on the host directly

Requirements

RabbitMQ is required to run as a message broker between flask and BigPanda. RabbitMQ can be installed via Docker Run, or by using docker-compose. The rabbitmq directory in the root of the project holds definitions.json and rabbitmq.conf.
Set up RabbitMQ as follows:

  • sudo mkdir /etc/rabbitmq
  • sudo cp rabbitmq/* /etc/rabbitmq
  • docker pull rabbitmq:latest
  • docker run -d -v /etc/rabbitmq:/etc/rabbitmq/ -p 15672:15672 -p 5673:5673 -p 5672:5672 --name rabbit-server rabbitmq

Now that the rabbitmq container is running and detached, you can check to see if it is fully loaded with:

  • docker logs rabbit-server

If you want to install the management console plugins, run this once the container is set up:

  • docker container exec -it rabbit-server rabbitmq-plugins enable rabbitmq_management

Usage

The Flask API runs on a uWSGI server. The requirements to run the Flask server are:

  1. It needs the config file listed in the root directory, pandaflaskapi.yml
  2. It needs the uwsgi.ini file listed in the root directory
  3. It needs creds either in the config file, or loaded into ENV Vars
    1. A list of ENV Vars are:
      1. RABBIT_USER
        1. Currently set to panda
      2. RABBIT_PASS
        1. Currently set to panda
      3. PANDA_BEARER_TOKEN
      4. PANDA_APP_TOKEN

The Flask API uses a uWSGI server. To run it, run the following command from the root directory:

  • uwsgi uwsgi.ini

API

Once you launch the web server, documentation can be found at:

  • localhost:5000/api/v1/ui

About

Integration to queue messages in RabbitMQ backend prior to forward them to BigPanda's Alert API

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published