Google cloud functions
Google Maps example
What are they ?
Node.js apps that run on a cloud VM and are invoked by a trigger
Where are they ?
In cloud storage
How are they created ?
Like any Node application, with package management & request handling.
How are they deployed ?
Get the source code from my repository
git pull https://github.com/brucemcpherson/effex-pd-cloudfunctions.git
Deploy it to my storage bucket as an http triggered function
gcloud beta functions deploy pdordergenerator --stage-bucket gcf_hell --trigger-http
How are they executed (http trigger) ?
// post a message to the cloud function
var packet = {
contents: {
item:keys.item,
message: {
updater:keys.updater,
mapsApiKey:keys.mapsApiKey
}
}
};
var response =
UrlFetchApp.fetch (url , {
payload:JSON.stringify(packet),
contentType:"application/json",
method:"post",
muteHttpExceptions:true
});
Google Maps in a cloud function
Node has no DOM, so you can't use regular Maps API.
But a limited version exists for Node
https://github.com/googlemaps/google-maps-services-js
npm install @google/maps
ns.maps = require('@google/maps');
ns.data.googleMapsClient = ns.maps.createClient({
key: keys.mapsApiKey,
Promise: Promise
});
How to test
There is a simulator that deploys and runs the function locally
npm install -g @google-cloud/functions-emulator
functions deploy pdordergenerator --trigger-http
functions call pdordergenerator
--data='{"contents":{"message":{"updater":"uxk-dela1w-b2os2bfjucqj","mapsApiKey":"AIxxxxxxxxxxxxx5YA9U"},"item":"item-pd-demo"}}'
functions logs read
Use as part of a workflow
A giant pub crawl generator
Data from cache to plot map using API
More info, videos and source code