Skip to content

Instantly share code, notes, and snippets.

Avatar

Grant Timmerman grant

View GitHub Profile
@grant
grant / hello_world_http.cc
Created Feb 13, 2021
Hello World, C++ Functions Framework
View hello_world_http.cc
#include <google/cloud/functions/http_request.h>
#include <google/cloud/functions/http_response.h>
#include <nlohmann/json.hpp>
namespace gcf = ::google::cloud::functions;
gcf::HttpResponse hello_world_http(gcf::HttpRequest request) {
auto greeting = [r = std::move(request)] {
auto request_json = nlohmann::json::parse(r.payload(), /*cb=*/nullptr,
/*allow_exceptions=*/false);
@grant
grant / deploy.sh
Created Jan 27, 2021
run-workflow-function deploy.sh
View deploy.sh
# Configuration
export PROJECT_ID=$(gcloud config get-value core/project)
# Deploy Workflow
gcloud workflows deploy myFirstWorkflow \
--source=myFirstWorkflow.yaml
# Deploy (private) Function
gcloud functions deploy runWorkflowFunction \
--runtime nodejs12 \
@grant
grant / index.js
Last active Jan 27, 2021
run-workflow-function index.js
View index.js
const projectId = process.env.PROJECT_ID;
/**
* Run Workflow Cloud Function
*/
exports.runWorkflow = async (req, res) => {
if (req.method !== 'POST') {
return res.status(405).send('Only POST method is allowed');
}
const workflowsAPI = await callWorkflowsAPI();
@grant
grant / package.json
Last active Jan 28, 2021
run-workflow-function package.json
View package.json
{
"name": "run-workflow-function",
"main": "index.js",
"scripts": {
"start": "functions-framework --target=runWorkflow"
},
"dependencies": {
"@google-cloud/functions-framework": "^1.7.1",
"@google-cloud/workflows": "^1.1.0"
}
@grant
grant / index.js
Created Nov 12, 2020
Critterwatch Vision
View index.js
const vision = require('@google-cloud/vision');
const visionClient = new vision.ImageAnnotatorClient();
const {Firestore} = require('@google-cloud/firestore');
const firestore = new Firestore();
const trackedAnimals = ['raccoon', 'alpaca', 'peacock', 'dog', 'gecko', 'squirrel', 'red panda'];
/**
* Triggered from a change to a Cloud Storage bucket.
@grant
grant / index.js
Last active Aug 1, 2020
Sheets as a Service – Basic Script
View index.js
const path = require('path');
const {google} = require('googleapis');
const sheets = google.sheets('v4');
// TODO: Add your Sheet ID
// https://docs.google.com/spreadsheets/d/2L2LpYPyxJQTf14OLM6mVuiou88MsEi4Q-IUUxswNlfe/edit
const SPREADSHEET_ID = '2L2LpYPyxJQTf14OLM6mVuiou88MsEi4Q-IUUxswNlfe';
/**
* Appends row data to the Google Sheet
@grant
grant / package.json
Last active Aug 5, 2020
Sheets as a Service – Basic Package
View package.json
{
"name": "sheets-on-run",
"main": "index.js",
"scripts": {
"start": "functions-framework --target=addRow"
},
"dependencies": {
"@google-cloud/functions-framework": "^1.6.0",
"googleapis": "^58.0.0"
}
@grant
grant / install_locally.sh
Created Jul 26, 2020
R Functions Framework Local Install
View install_locally.sh
# Install devtools, specifying a CRAN mirror
install.packages("devtools", repos='http://cran.us.r-project.org')
# Import devtools
library(devtools)
# Install GitHub project
install_github("averikitsch/functions-framework-r/src/functionsframework")
@grant
grant / deploy.sh
Created Jul 26, 2020
R Functions Framework Deploy
View deploy.sh
# Set env var "GCP_PROJECT" to our project name
GCP_PROJECT=$(gcloud config list --format 'value(core.project)' 2>/dev/null)
# Build and upload your image in Google Container Registry
gcloud builds submit \
--tag gcr.io/$GCP_PROJECT/hellor \
--timeout="20m"
# Deploy your container to Cloud Run
gcloud run deploy hellor \
@grant
grant / Dockerfile
Last active Jul 27, 2020
R Functions Framework Dockerfile
View Dockerfile
# Use an R image.
# https://hub.docker.com/r/rocker/r-ver/
FROM rocker/r-ver:3.6.1
# Copy R package binary
COPY functionsframework_*.tgz /
# Install functions framework dependencies.
RUN R -e "install.packages('functionsframework_0.0.0.9000.tgz', repos = NULL, type = 'source')"
RUN R -e "install.packages(c('optparse', 'plumber'))"