Last active
February 25, 2018 17:38
-
-
Save EliaECoyote/9e86e46d16c366b9f9c72009c8165087 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | |
const endpoint = `${apiEndpoint}/ev/${request.params.id}`; | |
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment