Given tasks of integrating our current system with multiple third-party services for customer support purposes at RealStake, I often make use of available webhooks to distribute data to multiple places. But there are some trouble like bringing up separate Express app for such goals or how we centralize logs. Therefore I decide to explore how to utilize Google cloud functions in current tasks.
Under the hood, Google cloud function is already using Express library, so it is really easy to write functions as mini Express apps then deploy right away from local machines or set up Github actions for it. So connecting the dots, I come up with below flow.
The basic idea is the separate of concerns from updating data in our internal system and third party results via cloud functions.
For example, the requirements are after user signing up successfully via the tracking_id stored in browser cookies, such data needs to be sent to AccessTrade (a marketing affiliate provider). Then after our customer support team confirm such users are legitimate, we trigger an update for user status on AccessTrade for reporting.
First, I make an index.js file to export our Express app as a single cloud function.
Next is creating our Express app for necessary routes interacting with AccessTrade service.
I will not go into details of business route implementation because it is exactly the same approach when we normally built REST api with Express app.
Then I think of how we centralize error log format in case we put in more third party service cloud functions in the future. It is convenient that Google already has provided a plugin for the famous Winston logging library.
I really love this library because we can define and format log to a universal JSON format and stream it to JSONpayload of Google cloud logging system.
Note: to test cloud functions locally, we can use https://cloud.google.com/functions/docs/functions-framework.
So far so good, I can make use of lightweight cloud functions and have a base structure for integrating different third party services later.
However, to push my learning further, I am considering using Google Cloud PubSub to apply event-based trigger patterns. The main idea is using a cloud function A as publiser to publish data to a defined topic, then another cloud function B will subscribe to it and update data back to our internal system. But that will be for my later post.