From cf6d7b9632c02d3bae1747d158c2cd233851ddb8 Mon Sep 17 00:00:00 2001 From: Nicolas Garnier Date: Fri, 12 May 2017 13:23:47 -0700 Subject: [PATCH] Add big-ben quickstart Change-Id: I6eac374f642c66ddb86ca874b1660624b2fd3b43 --- README.md | 4 ++ quickstarts/big-ben/README.md | 79 ++++++++++++++++++++++ quickstarts/big-ben/firebase.json | 12 ++++ quickstarts/big-ben/functions/index.js | 40 +++++++++++ quickstarts/big-ben/functions/package.json | 8 +++ quickstarts/big-ben/public/404.html | 33 +++++++++ quickstarts/big-ben/public/index.html | 65 ++++++++++++++++++ 7 files changed, 241 insertions(+) create mode 100644 quickstarts/big-ben/README.md create mode 100644 quickstarts/big-ben/firebase.json create mode 100644 quickstarts/big-ben/functions/index.js create mode 100644 quickstarts/big-ben/functions/package.json create mode 100644 quickstarts/big-ben/public/404.html create mode 100644 quickstarts/big-ben/public/index.html diff --git a/README.md b/README.md index 82a203d..3e026e7 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,10 @@ This quickstart sample demonstrates using **Cloud Functions** triggered by **Fir This quickstart sample demonstrates using **Cloud Functions** triggered by **HTTPS requests**. The function returns the current server time and allows for date time formatting. +### [Hosting triggered HTTPS fucntion quickstart: Big Ben](/quickstarts/big-ben) + +This quickstart demonstrates using **Cloud Functions** with an HTTPS trigger that's triggered through a Firebase Hosting URL. The function will display a repeated number of "BONG" depending on the hour of the day. + ### [Cloud Storage trigger quickstart: Thumbnail generator](/quickstarts/thumbnails) This quickstart sample demonstrates using **Cloud Functions** triggered by **Firebase Storage events**. The function generates a thumbnail of uploaded images. diff --git a/quickstarts/big-ben/README.md b/quickstarts/big-ben/README.md new file mode 100644 index 0000000..c1a8323 --- /dev/null +++ b/quickstarts/big-ben/README.md @@ -0,0 +1,79 @@ +# Firebase SDK for Cloud Functions Quickstart - HTTPS function with Firebase Hosting URL trigger + +This quickstart demonstrates using the **Firebase SDK for Cloud Functions** with an HTTPS trigger that's triggered through a Firebase Hosting URL. The function will display a repeated number of "BONG" depending on the hour of the day. + + +## Introduction + +The function `bigben` returns an HTML page that display a repeated number of "BONG" depending on the hour of the day. + +Further reading: + + - [Read more about the Firebase SDK for Cloud Functions](https://firebase.google.com/docs/functions) + + +## Initial setup, build tools and dependencies + +### 1. Clone this repo + +Clone or download this repo and open the `quickstarts/big-ben` directory. + + +### 2. Create a Firebase project and configure the quickstart + +Create a Firebase Project on the [Firebase Console](https://console.firebase.google.com). + +Set up your Firebase project by running `firebase use --add`, select your Project ID and follow the instructions. + + +### 3. Install the Firebase CLI and enable Functions on your Firebase CLI + +You need to have installed the Firebase CLI. If you haven't run: + +```bash +npm install -g firebase-tools +``` + +> Doesn't work? You may need to [change npm permissions](https://docs.npmjs.com/getting-started/fixing-npm-permissions). + + +## Deploy the app to prod + +First you need to install the `npm` dependencies of the functions: + +```bash +cd functions && npm install; cd .. +``` + +This installs locally the Firebase SDK and the Firebase Functions SDK. + +Deploy to Firebase using the following command: + +```bash +firebase deploy +``` + +This deploys and activates the `reverseString` Function. + +> The first time you call `firebase deploy` on a new project with Functions will take longer than usual. + + +## Try the sample + +After deploying the function you can open the following URL in your browser: + +``` +https://.firebaseapp.com/bigben +``` + +A page containing a repeated number of "BONG" will be displayed. + + +## Contributing + +We'd love that you contribute to the project. Before doing so please read our [Contributor guide](../../CONTRIBUTING.md). + + +## License + +© Google, 2016. Licensed under an [Apache-2](../../LICENSE) license. diff --git a/quickstarts/big-ben/firebase.json b/quickstarts/big-ben/firebase.json new file mode 100644 index 0000000..490bbc4 --- /dev/null +++ b/quickstarts/big-ben/firebase.json @@ -0,0 +1,12 @@ +// [START rewriterules] +{ + "hosting": { + "public": "public", + + // Add the following rewrites section *within* "hosting". + "rewrites": [ { + "source": "/bigben", "function": "bigben" + } ] + } +} +// [END rewriterules] diff --git a/quickstarts/big-ben/functions/index.js b/quickstarts/big-ben/functions/index.js new file mode 100644 index 0000000..8f82af0 --- /dev/null +++ b/quickstarts/big-ben/functions/index.js @@ -0,0 +1,40 @@ +/** + * Copyright 2017 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +'use strict'; + +// [START all] +const functions = require('firebase-functions'); + +exports.bigben = functions.https.onRequest((req, res) => { + const hours = (new Date().getHours() % 12) + 1; // london is UTC + 1hr; + // [START_EXCLUDE silent] + // [START cachecontrol] + res.set('Cache-Control', 'public, max-age=300, s-maxage=600'); + // [END cachecontrol] + // [START vary] + res.set('Vary', 'Accept-Encoding, X-My-Custom-Header'); + // [END vary] + // [END_EXCLUDE] + res.status(200).send(` + + Time + + + ${'BONG '.repeat(hours)} + + `); +}); +// [END all] diff --git a/quickstarts/big-ben/functions/package.json b/quickstarts/big-ben/functions/package.json new file mode 100644 index 0000000..1b843b7 --- /dev/null +++ b/quickstarts/big-ben/functions/package.json @@ -0,0 +1,8 @@ +{ + "name": "big-ben-functions", + "description": "A simple endpoint that returns a number of 'BONG' based on the time of day", + "dependencies": { + "firebase-admin": "^4.2.1", + "firebase-functions": "^0.5.5" + } +} diff --git a/quickstarts/big-ben/public/404.html b/quickstarts/big-ben/public/404.html new file mode 100644 index 0000000..829eda8 --- /dev/null +++ b/quickstarts/big-ben/public/404.html @@ -0,0 +1,33 @@ + + + + + + Page Not Found + + + + +
+

404

+

Page Not Found

+

The specified file was not found on this website. Please check the URL for mistakes and try again.

+

Why am I seeing this?

+

This page was generated by the Firebase Command-Line Interface. To modify it, edit the 404.html file in your project's configured public directory.

+
+ + diff --git a/quickstarts/big-ben/public/index.html b/quickstarts/big-ben/public/index.html new file mode 100644 index 0000000..4828490 --- /dev/null +++ b/quickstarts/big-ben/public/index.html @@ -0,0 +1,65 @@ + + + + + + Welcome to Firebase Hosting + + + + + + + + + + + + + + +
+

Welcome

+

Firebase Hosting Setup Complete

+

You're seeing this because you've successfully setup Firebase Hosting. Now it's time to go build something extraordinary!

+ Open Hosting Documentation +
+

Firebase SDK Loading…

+ + + +