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.
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;
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.
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
/etcsudo 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
etcdirectory to/etcsudo 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 keyandbearer token. Add them to the files in/etc/panda-flaskdirectory. - The Flask config file is included in the
etc/panda-flaskdirectory. If anything is changed,/etc/panda-flaskshould be updated to reflect the changes. uwsgiuses 2 types of connections, either bysocketorhttp. The appropriateuwsgi.inifile is added to theetc/panda-flaskdirectory.- For docker, uwsgi should use a unix socket connection.
docker-composegives us access to route requests via the service name. Make sure thepandaflaskapi.ymlis pointing torabbimq-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), andpanda_flask_backend. Create these networks as follows:docker network create panda_flaskdocker 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
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
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.
- 3 containers should start:
A few test steps you can take to verify everything is up and running correctly is to view the logs.
docker logs nginx-proxydocker logs rabbitmqdocker logs panda-flask
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
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/rabbitmqsudo cp rabbitmq/* /etc/rabbitmqdocker pull rabbitmq:latestdocker 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
The Flask API runs on a uWSGI server. The requirements to run the Flask server are:
- It needs the config file listed in the root directory,
pandaflaskapi.yml - It needs the
uwsgi.inifile listed in the root directory - It needs creds either in the config file, or loaded into ENV Vars
- A list of ENV Vars are:
- RABBIT_USER
- Currently set to
panda
- Currently set to
- RABBIT_PASS
- Currently set to
panda
- Currently set to
- PANDA_BEARER_TOKEN
- PANDA_APP_TOKEN
- RABBIT_USER
- A list of ENV Vars are:
The Flask API uses a uWSGI server. To run it, run the following command from the root directory:
uwsgi uwsgi.ini
Once you launch the web server, documentation can be found at:
localhost:5000/api/v1/ui