{"id":13638072,"url":"https://github.com/mchmarny/buttons","last_synced_at":"2025-06-30T23:36:49.527Z","repository":{"id":77051253,"uuid":"189588282","full_name":"mchmarny/buttons","owner":"mchmarny","description":"How to use Flic buttons with Clud Run on GCP","archived":false,"fork":false,"pushed_at":"2023-02-25T02:28:41.000Z","size":5272,"stargazers_count":4,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-15T22:44:27.562Z","etag":null,"topics":["cloud","cloudrun","flic","iot","pubsub"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mchmarny.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2019-05-31T12:12:23.000Z","updated_at":"2022-04-01T20:05:47.000Z","dependencies_parsed_at":"2024-01-14T09:18:19.671Z","dependency_job_id":"f0bc0901-e2c5-47cb-8a75-b206158341e0","html_url":"https://github.com/mchmarny/buttons","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/mchmarny/buttons","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mchmarny%2Fbuttons","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mchmarny%2Fbuttons/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mchmarny%2Fbuttons/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mchmarny%2Fbuttons/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mchmarny","download_url":"https://codeload.github.com/mchmarny/buttons/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mchmarny%2Fbuttons/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262869359,"owners_count":23377277,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cloud","cloudrun","flic","iot","pubsub"],"created_at":"2024-08-02T01:00:39.675Z","updated_at":"2025-06-30T23:36:49.481Z","avatar_url":"https://github.com/mchmarny.png","language":"Go","funding_links":[],"categories":["Integrations"],"sub_categories":["Container guides"],"readme":"# How to use Flic buttons with Cloud Run on GCP\n\n\u003cimg align=\"right\" src=\"image/flic.png\" alt=\"Flic button\"\u003e\n\nA co-worker recently told me about [flic.io](https://flic.io/) buttons. These button caught my attention because they can include triggers for single, double, or hold click and can be easily wired up to all kinds of actions.\n\nI instantly thought of of a few really interesting applications. To start with though, I wanted to create a simple service that would allow me to push the custom data defined on each button over HTTP to Cloud PubSub. That in turn would then connect me to the many more actuation options through GCP APIs and services that connect to Cloud PugSub.\n\nI went ahead and ordered [4-pack](https://flic.io/shop/flic-4pack) of Flic buttons and chose [Cloud Run](https://cloud.google.com/run/) to implement my `buttons` service.\n\nIn this demo I will illustrate how to:\n\n* Deploy Cloud Run service that will persist sent data to Cloud PubSub\n* and, how to configure Flic buttons to send data to Cloud Run service\n\n## Easy Deploy\n\nYou can deploy `buttons` service in Cloud Run with a single... wait for it... click of a button. Just click on the \"Run on Google Cloud\" bellow and follow the prompts in Cloud Shell. When done, go to the [Configuring Flic Button](#configuring-flic-button) section to finish configuring your Flic buttons.\n\n[![Click to run on Cloud Run](https://storage.googleapis.com/cloudrun/button.svg)](https://console.cloud.google.com/cloudshell/editor?shellonly=true\u0026cloudshell_image=gcr.io/cloudrun/button\u0026cloudshell_git_repo=https://github.com/mchmarny/buttons.git)\n\n\n## Manual Deploy (the harder way)\n\n### Prerequisites\n\n* Configured gcloud CLI (Cloud SDK) for your operating system ([how-to](https://cloud.google.com/sdk/gcloud/)) with default application credentials:\n  * `gcloud auth application-default login`\n* Enabled GCP APIs for Cloud PubSub and Cloud Run\n  * `gcloud services enable pubsub.googleapis.com`\n  * `gcloud services enable run.googleapis.com`\n\n### Cloud PubSub Topic\n\nTo store the data sent from each button action, first, we need to create a Cloud PubSub topic named `clicks`\n\n```shell\ngcloud pubsub topics create clicks\n```\n\nThat should result with\n\n```shell\nCreated topic [projects/YOUR_PROJECT_ID/topics/clicks].\n```\n\n### Cloud Run Service\n\nNext deploy the generic Cloud Run service called `buttons`. The code for that service is in this repository for you to review. There is already a public image available (see below), but if you want to, you can build your own copy with this command:\n\n```shell\ngcloud builds submit --tag gcr.io/YOUR_PROJECT_ID/buttons:0.1.2\n```\n\n\u003e For more information on how to build images using Cloud Build see [here](https://cloud.google.com/run/docs/quickstarts/build-and-deploy). You can skip the build step and use the already pre-built public image located at `gcr.io/knative-samples/buttons:0.1.2`\n\nBefore we deploy the Cloud Run service we have to create a `secret` which will be used to ensure that only data from your button will be accepted. To do that, replace the `your-long-and-super-secret-string` string below with something more secure and define it as an environment variable using this command:\n\n```shell\nexport SECRET=\"your-long-and-super-secret-string\"\n```\n\n\u003e For more secure way to defining secrets in Cloud Run environment variables you can use the [berglas](https://github.com/GoogleCloudPlatform/berglas) library.\n\nNow that we have the `SECRET` defined, you can deploy the Cloud Run service. A couple of flags worth highlighting in the bellow command:\n\n* `concurrency` - the button service is thread safe and doesn't store any internal state so we can turn the concurrency to maximum. More on concurrency [here](https://cloud.google.com/run/docs/about-concurrency)\n* `allow-unauthenticated` - By default Cloud Run creates private services which can't be accessed by anonymous users. Since our buttons don't support more complex authentication scheme, we will expose the Cloud Run service to the public and validate each request using `token` string in request header. More on allowing public access [here](https://cloud.google.com/run/docs/authenticating/public)\n\n\n```shell\ngcloud beta run deploy buttons \\\n    --region=us-central1 \\\n    --concurrency=80 \\\n    --allow-unauthenticated \\\n    --image=gcr.io/knative-samples/buttons:0.1.2 \\\n    --update-env-vars=\"SECRET=${SECRET}\"\n```\n\nThe response from the above command should look something like this\n\n```shell\nDeploying container to Cloud Run service [buttons] in project [YOUR_PROJECT_ID] region [us-central1]\n✓ Deploying... Done.\n  ✓ Creating Revision...\n  ✓ Routing traffic...\n  ✓ Setting IAM Policy...\nDone.\nService [buttons] revision [buttons-00001] has been deployed and is serving traffic at https://buttons-*******-uc.a.run.app\n```\n\nYou should be able to see that service in Cloud Run service list\n\n```shell\n$: gcloud beta run services list\n   SERVICE   REGION       LATEST REVISION  SERVING REVISION  LAST DEPLOYED BY     LAST DEPLOYED AT\n✔  buttons   us-central1  buttons-00001    buttons-00001     mchmarny@*****.com   2019-06-01T00:10:06.059Z\n```\n\nYou can also test the deployed service using `curl`. Just make sure you replace the `***` part of the URL with the actual `URL` returned by the above command.\n\n```shell\ncurl -H \"content-type: application/json\" -H \"token: ${SECRET}\" \\\n    -d '{ \"version\": \"v0.1.0\", \"type\": \"button\", \"color\": \"white\", \"click\": 1 }' \\\n    -X POST https://buttons-*******-uc.a.run.app\n```\n\nYou can always find the URL of your `buttons` service by executing the following command\n\n```shell\ngcloud beta run services describe buttons --region us-central1 --format 'value(status.domain)'\n```\n\n## Configuring Flic Button\n\nTo setup Flic buttons on your device follow the [quick start instructions](https://start.flic.io/). Each one of the Flic buttons come with many [pre-programmed actions](https://flic.io/all-functions). To execute above configured Cloud Run service though we will use the `Internet Request` action.\n\n\u003cimg src=\"image/flic-req.png\" alt=\"Flic Internet Request Action\"\u003e\n\nTo do that you will need to select one of your buttons on your device, expand the `Advanced` category, and configure an action for either `single`, `double`, or `hold` click.\n\n\u003cimg src=\"image/flic-nav.png\" alt=\"Flic Internet Request Action\"\u003e\n\nThat will get you to the `HTTP Internet Request` action configuration screen\n\n\u003cimg src=\"image/flic-conf.png\" alt=\"Flic Internet Request Action Configuration\" width=\"400\"\u003e\n\nFew parameters to configure here:\n\n| Parameter        | Description                                                                                                                               |\n| ---------------- | ----------------------------------------------------------------------------------------------------------------------------------------- |\n| **URL**          | Enter the full URL of the Cloud Run Service                                                                                               |\n| **Method**       | Select `POST`                                                                                                                             |\n| **Content Type** | Enter `application/json`                                                                                                                  |\n| **Body**         | Enter your JSON message to send to the service (e.g. `{ \"type\": \"button\", \"color\": \"white\", \"click\": 1 }`)                                |\n| **HTTP Header**  | Kye: `token`, Value: the value of the previously defined `SECRET`. Make sure you click the `Insert` button to \"save\" the header parameter |\n\nWhen done entering the above parameters, click `Done` in upper right corner to save the entire action. Now your Flic button is configured for use with Cloud Run.\n\n## Demo\n\nAssuming all the above deployment steps were completed successfully, you should be able to click the button and see the following in GCP service:\n\n1. **Cloud Run UI in service log**\n\n\u003cimg src=\"image/cr-log.png\" alt=\"Cloud Run Log\"\u003e\n\n1. **Cloud Run UI metrics charts**\n\n\u003cimg src=\"image/cr-metric.png\" alt=\"Cloud Run Metric\"\u003e\n\n3. **Stackdriver PubSub topic (`clikc`) metric chart**\n\n\u003cimg src=\"image/sd-metric.png\" alt=\"Stackdriver Metric\"\u003e\n\n## Summary\n\nHopefully this demo gave you an idea on how to connect your Flic buttons and many other IoT devices to Cloud Run service. With the basic implementation in place we can start working on more creative solutions next.\n\n\n## Disclaimer\n\nThis is my personal project and it does not represent my employer. I take no responsibility for issues caused by this code. I do my best to ensure that everything works, but if something goes wrong, my apologies is all you will get.\n\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmchmarny%2Fbuttons","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmchmarny%2Fbuttons","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmchmarny%2Fbuttons/lists"}