A couple of weeks ago I came across a new python web framework called Falcon. I was quickly impressed with its ease of use and most importantly this fragment:
It doesn’t try to be everything to everyone, focusing instead on a single use case: HTTP APIs.
I decided to build a simple server that will consume data from SendGrid’s Event Webhook and use Fluentd to log and store the events. Fluentd provides a sense of dependency injection when it comes to logging and storing to multiple data outputs with a simple configuration file.
Building the Environment
First, install Fluentd. You can find several options in their quickstart, but for the purposes of this tutorial I will use the ruby gem they have provided:
Now, lets create a folder and initialize our python environment:
Notice that the last command will generate the configuration file needed to run Fluentd. Modify the fluent.conf to reflect the following settings:
This states that data will come in through port 24224 and everything that is tagged with the prefix events.log will be logged to STDOUT. We can specify multiple outputs, but for the purposes of this tutorial, we will stick to STDOUT.
Fire up the Fluend Daemon by running:
Now lets get our hands dirty with some python (specifically with Falcon). Starting a Falcon API server its as simple as:
Falcon has something called Hooks. It’s similar to middleware, but it only runs before specified functions rather than before and after. For illustration purposes, lets write a simple one that verifies that the request’s content type is application/json and it’s executed before any other function:
Now lets initialize our Fluentd client to start sending logs:
Finally, lets actually handle the Event Webhook by creating a resource. In Falcon, resources have special methods. These define which HTTP verbs will get support. To accomplish this in a semantic way, Falcon uses on_verb syntax. In our case, we only need to support HTTP POST for this example. This is how it will end up looking like:
There we have it! To run it, simply run:
Hope you enjoy this tutorial! If you have any questions, feel free to reach out!