Skip to content

Instantly share code, notes, and snippets.

@EliaECoyote
Last active February 25, 2018 17:38
Show Gist options
  • Select an option

  • Save EliaECoyote/9e86e46d16c366b9f9c72009c8165087 to your computer and use it in GitHub Desktop.

Select an option

Save EliaECoyote/9e86e46d16c366b9f9c72009c8165087 to your computer and use it in GitHub Desktop.

Revisions

  1. EliaECoyote revised this gist Feb 25, 2018. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions firebaseFunction2.ts
    Original file line number Diff line number Diff line change
    @@ -35,6 +35,7 @@ const nodeFetchConfig = {
    app.get('/events/:id', (request, response) => {
    const userAgent = request.get('user-agent');
    if (userAgent.indexOf("facebookexternalhit") === 0) {
    const endpoint = `${apiEndpoint}/ev/${request.params.id}`;
    fetch(apiEndpoint, nodeFetchConfig)
    .then(res => res.json())
    .then(json => json.data)
  2. EliaECoyote created this gist Feb 25, 2018.
    59 changes: 59 additions & 0 deletions firebaseFunction2.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    import * as functions from 'firebase-functions';
    import * as express from 'express';

    const app = express();

    function buildOgTemplate(event: any) {
    return `
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
    <meta property="og:title" content="${event.title}" />
    <meta property="og:description" content="${event.description}" />
    <meta property="og:url" content="https://socialevents.com" />
    <meta property="og:image" content="${event.image}" />
    <meta property="og:type" content="website" />
    <meta property="og:image:width" content="600" />
    <meta property="og:image:height" content="600" />
    <base href="/">
    </head>
    </html>
    `
    }

    const publicEndpoint = "https://socialevents.com"
    const apiEndpoint = "https://api.socialevents.com/api/v1"

    const nodeFetchConfig = {
    headers: {
    "Accept": "application/json",
    "Cache-Control": "no-cache"
    }
    };

    app.get('/events/:id', (request, response) => {
    const userAgent = request.get('user-agent');
    if (userAgent.indexOf("facebookexternalhit") === 0) {
    fetch(apiEndpoint, nodeFetchConfig)
    .then(res => res.json())
    .then(json => json.data)
    .then(event => {
    const template = buildTemplateWithMetas(event)
    response.status(200).send(template);
    })
    .catch(error =>
    fetch(`${publicEndpoint}/index.html`)
    .then(res => res.text())
    .then(html => response.status(200).send(html))
    .catch(err => response.send(err));
    );
    } else {
    fetch(`${config.publicEndpoint}/index.html`)
    .then(res => res.text())
    .then(html => response.send(html))
    .catch(error => response.send(error));
    }
    });

    exports.eventsReqHandler = functions.https.onRequest(app);