Skip to content

Commit

Permalink
Removing the HTTPS hello world quickstart and adding back the time-se…
Browse files Browse the repository at this point in the history
…rver one.

Change-Id: I394919a1eb3bd1a1b11295729cf7f6aad5a895ce
  • Loading branch information
Nicolas Garnier committed Feb 3, 2017
1 parent 7534df6 commit 7623ed6
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 123 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,22 @@ To learn how to get started with the Firebase SDKL for Cloud Functions try the q

This repository contains the following samples:

### [Quickstart: Realtime Database trigger](/quickstarts/uppercase)
### [Realtime database trigger quickstart: Uppercaser](/quickstarts/uppercase)

This quickstart sample demonstrates using **Cloud Functions** with a **Firebase Realtime Database** trigger. The function will uppercase messages written to the datastore.

### [Quickstart: HTTPS trigger](/quickstarts/time)
### [HTTPS trigger quickstart: Time Server](/quickstarts/time)

This quickstart sample demonstrates using **Cloud Functions** triggered by **HTTPS requests**. The function will return the current server time and allows for date time formatting.

### [Quickstart: Firebase Storage Trigger](/quickstarts/thumbnails)
### [Cloud Storage trigger quickstart: Thumbnail generator](/quickstarts/thumbnails)

This quickstart sample demonstrates using **Cloud Functions** triggered by **Firebase Storage events**. The function will generate a thumbnail of uploaded images.

### [PubSub trigger quickstart: Hello World](/quickstarts/thumbnails)

This quickstart sample demonstrates using **Cloud Functions** triggered by **PubSub events**. The function will log the PubSub payload in a Hello world message.

### [Send FCM notifications](fcm-notifications)

This sample demonstrates how to send a Firebase Cloud Messaging (FCM) notification from a Realtime Database triggered Function when users get new followers. The sample also features a Web UI to experience the FCM notification.
Expand Down
112 changes: 0 additions & 112 deletions quickstarts/https-helloworld/functions/index.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Firebase SDK for Cloud Functions Quickstart - HTTPS trigger

This quickstart demonstrates using the **Firebase SDK for Cloud Functions** with an HTTPS trigger.
This quickstart demonstrates using the **Firebase SDK for Cloud Functions** with an HTTPS trigger through building an endpoint returning the current time.


## Introduction

We'll show a series of basic HTTPS triggered Functions.
The function `date` simply returns the current server date. You can pass it a `format` URL Query parameter to format the date.

Further reading:

Expand Down Expand Up @@ -45,7 +45,10 @@ First you need to install the `npm` dependencies of the functions:
cd functions && npm install; cd ..
```

This installs locally the Firebase SDK and the Firebase SDK for Cloud Functions.
This installs locally:
- The Firebase SDK and the Firebase Functions SDK.
- The [moment](https://www.npmjs.com/package/moment) npm package to format time.
- The [cors](https://www.npmjs.com/package/cors) npm package to allow Cross Origin AJAX requests on the endpoint.

Deploy to Firebase using the following command:

Expand All @@ -60,7 +63,15 @@ This deploys and activate the date Function.

## Try the sample

To try the samples please follow the [HTTPS trigger documentation](https://firebase.google.com/preview/functions/gcp-https)
You can first simply hit the Function URL directly. After deploying the function you can simply open the following URLs in your browser:

```
https://us-central1-<project-id>.cloudfunctions.net/date
https://us-central1-<project-id>.cloudfunctions.net/date?format=MMMM%20Do%20YYYY%2C%20h%3Amm%3Ass%20a
```

Formatted dates should be displayed.


## Contributing
Expand Down
File renamed without changes.
76 changes: 76 additions & 0 deletions quickstarts/time-server/functions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* Copyright 2016 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 functionsimport]
const functions = require('firebase-functions');
// [END functionsimport]
// [START additionalimports]
// Moments library to format dates.
const moment = require('moment');
// CORS Express middleware to enable CORS Requests.
const cors = require('cors')({origin: true});
// [END additionalimports]

// [START all]
/**
* Returns the server's date. You must provide a `format` URL query parameter or `format` vaue in
* the request body with which we'll try to format the date.
*
* Format must follow the Node moment library. See: http://momentjs.com/
*
* Example format: "MMMM Do YYYY, h:mm:ss a".
* Example request using URL query parameters:
* https://us-central1-<project-id>.cloudfunctions.net/date?format=MMMM%20Do%20YYYY%2C%20h%3Amm%3Ass%20a
* Example request using request body with cURL:
* curl -H 'Content-Type: application/json' /
* -d '{"format": "MMMM Do YYYY, h:mm:ss a"}' /
* https://us-central1-<project-id>.cloudfunctions.net/date
*
* This endpoint supports CORS.
*/
// [START trigger]
exports.date = functions.https().onRequest((req, res) => {
// [END trigger]
// [START sendError]
// Forbidding PUT requests.
if (req.method === 'PUT') {
res.status(403).send('Forbidden!');
}
// [END sendError]

// [START usingMiddleware]
// Enable CORS using the `cors` express middleware.
cors(req, res, () => {
// [END usingMiddleware]
// Reading date format from URL query parameter.
// [START readQueryParam]
let format = req.query.format;
// [END readQueryParam]
// Reading date format from request body query parameter
if (!format) {
// [START readBodyParam]
format = req.body.format;
// [END readBodyParam]
}
// [START sendResponse]
const formattedDate = moment().format(format);
console.log('Sending Formatted date:', formattedDate);
res.status(200).send(formattedDate);
// [END sendResponse]
});
});
// [END all]
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "https-quickstart-functions",
"description": "HTTPS Firebase Functions Quickstart sample",
"name": "time-server-functions",
"description": "A simple time server using HTTPS Cloud Function",
"dependencies": {
"cors": "^2.8.1",
"dateformat": "^1.0.12",
"firebase": "^3.6",
"firebase-functions": "https://storage.googleapis.com/firebase-preview-drop/node/firebase-functions/firebase-functions-preview.latest.tar.gz"
"firebase-functions": "https://storage.googleapis.com/firebase-preview-drop/node/firebase-functions/firebase-functions-preview.latest.tar.gz",
"moment": "^2.17.1"
}
}

0 comments on commit 7623ed6

Please sign in to comment.