{"id":15493856,"url":"https://github.com/seriousme/docker-voting-app-gcp","last_synced_at":"2025-11-04T12:30:36.522Z","repository":{"id":49937682,"uuid":"127443355","full_name":"seriousme/docker-voting-app-gcp","owner":"seriousme","description":"Docker Voting App done Serverless Style using GCP","archived":false,"fork":false,"pushed_at":"2021-06-08T04:34:58.000Z","size":433,"stargazers_count":0,"open_issues_count":2,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-10-19T11:29:07.805Z","etag":null,"topics":["firebase-database","firebase-functions","gcp","serverless"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/seriousme.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-03-30T15:28:09.000Z","updated_at":"2021-07-16T19:17:56.000Z","dependencies_parsed_at":"2022-07-28T22:48:55.042Z","dependency_job_id":null,"html_url":"https://github.com/seriousme/docker-voting-app-gcp","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seriousme%2Fdocker-voting-app-gcp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seriousme%2Fdocker-voting-app-gcp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seriousme%2Fdocker-voting-app-gcp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seriousme%2Fdocker-voting-app-gcp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/seriousme","download_url":"https://codeload.github.com/seriousme/docker-voting-app-gcp/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239433912,"owners_count":19637806,"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":["firebase-database","firebase-functions","gcp","serverless"],"created_at":"2024-10-02T08:09:40.869Z","updated_at":"2025-11-04T12:30:36.476Z","avatar_url":"https://github.com/seriousme.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Docker Voting App done Serverless Style using Google Firebase\n\n## Introduction\n\nI was reading [Deploy the Voting App to AWS ECS with Fargate] by Tony Pujals\nwhere he describes how to run the [Docker Voting app demo] on AWS using [AWS\nFargate]\n\n[deploy the voting app to aws ecs with fargate]: https://medium.com/@tonypujals/deploy-the-voting-app-to-aws-ecs-with-fargate-\n[docker voting app demo]: https://github.com/subfuzion/docker-voting-app-nodejs\n[aws fargate]: https://aws.amazon.com/fargate/\n\nOf course I understand that the Docker Voting app is a showcase of Docker\ntechnology and that it's not the most exciting application from a business\nperspective. I also understand that people want to show how you can take Docker\ntechnology to AWS. However in my mind I started wondering: if I would take this\nto AWS would I be following the same path ? Or would I go Serverless ?\n\nGiven the title: I went Serverless!\n\nThe first iteration was on [AWS using AWS ApiGateway and AWS DynamoDB only]. And\nthen I figured that it would be fun to try it on Google Cloud as well. Given the\nsimplicity of the problem I went for [Google Firebase].\n\n[aws using aws apigateway and aws dynamodb only]: https://github.com/seriousme/docker-voting-app-aws\n[google firebase]: https://firebase.google.com/\n\n## Figuring out the API\n\nLooking at the sources of the original voting app there are two endpoints:\n\n* POST /vote where you can post a vote by posting JSON like `{\"vote\":\"a\"}` or\n  `{\"vote\":\"b\"}`\n* GET /results which will give you results like:\n\n```json\n{\n  \"success\": true,\n  \"result\": {\n    \"a\": 0,\n    \"b\": 0\n  }\n}\n```\n\nFirebase does nothing with automatic schema validation like the AWS API\nApiGateway does. So creating a JSON schema is of no use here. Given the specific\nformats of the API a simple route to a solution is to use [Firebase Functions]\nwith data stored using [Firebase Realtime Database].\n\n[firebase functions]: https://firebase.google.com/products/functions/\n[firebase realtime database]: https://firebase.google.com/products/realtime-database/\n\nAnd then it becomes a rather simple exercise:\n\n## Import dependencies and initialize the connection to the database\n\n```js\nconst functions = require(\"firebase-functions\");\nconst admin = require(\"firebase-admin\");\nadmin.initializeApp(functions.config().firebase);\n```\n\n## Define the route for /vote\n\n```js\nexports.vote = functions.https.onRequest((request, response) =\u003e {\n  const vote = request.body.vote;\n  if (vote === \"a\" || vote === \"b\") {\n    const resultsRef = admin.database().ref(\"/result\");\n    const ref = resultsRef.child(vote);\n    ref.transaction(currentVotes =\u003e {\n      // If result.{vote} has never been set, currentVotes will be `null`.\n      return (currentVotes || 0) + 1;\n    });\n    response.send(\"You voted: \" + vote);\n  } else {\n    response.status(400).send(\"Invalid request\");\n  }\n});\n```\n\n## Define the route for /results\n\n```js\nexports.results = functions.https.onRequest((request, response) =\u003e {\n  var results = {\n    success: true,\n    result: {\n      a: 0,\n      b: 0\n    }\n  };\n  return admin\n    .database()\n    .ref(\"/result\")\n    .once(\"value\")\n    .then(snapshot =\u003e {\n      const resultsData = snapshot.val();\n      results.result.a = resultsData.a || 0;\n      results.result.b = resultsData.b || 0;\n      response.json(results);\n      return;\n    })\n    .catch(_ =\u003e {\n      response.json(results);\n    });\n});\n```\n\nThe data might not exist in the database yet when `/results` is being called\ntherefore this function takes care of that using `resultsData.a || 0` and\nthrough the `.catch` handler.\n\nThe complete code can be found in [functions/index.js]. All the rest of the\nproject is auto generated using [firebase-tools].\n\n[functions/index.js]: https://github.com/seriousme/docker-voting-app-gcp/blob/master/functions/index.js\n[firebase-tools]: https://www.npmjs.com/package/firebase-tools\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fseriousme%2Fdocker-voting-app-gcp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fseriousme%2Fdocker-voting-app-gcp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fseriousme%2Fdocker-voting-app-gcp/lists"}