Created
April 30, 2019 22:03
-
-
Save scyrizales/90eb8075075e9400b405a13c3861f00d to your computer and use it in GitHub Desktop.
Lambda@edge example
This file contains hidden or 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
| function isBotUserAgent(request) { | |
| /*VALIDATE request.headers.user-agent*/ | |
| /** | |
| Facebook: facebookexternalhit/1.1 | |
| Twitter: Twitterbot | |
| Google: Googlebot | |
| Slack: Slack | |
| LinkedIn: LinkedInBot/1.0 | |
| Pinterest: Pinterest | |
| */ | |
| } | |
| exports.handler = (event, context, callback) => { | |
| const request = event.Records[0].cf.request; | |
| if (!isBotUserAgent(request)) { | |
| callback(null, request); | |
| } | |
| const uri = request.uri; | |
| const id = uri.split('/')[2] | |
| const params = { | |
| TableName: 'my_table', | |
| Key: { | |
| "id": id | |
| } | |
| }; | |
| docClient.get(params, (err, data) => { | |
| if (err) { | |
| callback(err, null) | |
| } else if (!data.Item) { | |
| const errorObj = { | |
| errorType : "NotFound", | |
| httpStatus : 404, | |
| requestId : context.awsRequestId, | |
| message : "Public tour was not found." | |
| } | |
| callback(JSON.stringify(errorObj)); | |
| } else { | |
| const metaTags = generateMetaTags(data); | |
| const metaTagTemplate = generateMetaTagTemplate(metaTags); | |
| /* Generate HTTP response */ | |
| const response = { | |
| status: '200', | |
| statusDescription: 'HTTP OK', | |
| httpVersion: httpVersion, | |
| body: metaTagTemplate, | |
| headers: { | |
| 'cache-control': [{ | |
| key: 'Cache-Control', | |
| value: 'max-age=100' | |
| }], | |
| 'content-type': [{ | |
| key: 'Content-Type', | |
| value: 'text/html' | |
| }] | |
| } | |
| }; | |
| callback(null, response); | |
| } | |
| }); | |
| }; |
This file contains hidden or 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
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <!-- COMMON TAGS --> | |
| <meta charset="utf-8"> | |
| <title>${data.title}</title> | |
| <!-- Search Engine --> | |
| <meta name="description" content="${data.description}"> | |
| <meta name="image" content="${data.imgSrc}"> | |
| <!-- Schema.org for Google --> | |
| <meta itemprop="name" content="${data.title}"> | |
| <meta itemprop="description" content="${data.description}"> | |
| <meta itemprop="image" content="imgSrc"> | |
| <!-- Open Graph general (Facebook, Pinterest & Google+) --> | |
| <meta prefix="og: http://ogp.me/ns#" name="og:type" content="website"> | |
| <meta prefix="og: http://ogp.me/ns#" name="og:title" content="${data.title}"> | |
| <meta prefix="og: http://ogp.me/ns#" name="og:description" content="${data.description}"> | |
| <meta prefix="og: http://ogp.me/ns#" name="og:image" content="${agent.includes('LinkedIn') ? data.imgSrc+'.jpg' : data.imgSrc}"> | |
| <meta prefix="og: http://ogp.me/ns#" name="og:url" content="${data.url}"> | |
| <meta prefix="og: http://ogp.me/ns#" name="og:site_name" content="${data.title}"> | |
| </head> | |
| <body> | |
| <p>uri: ${uri}</p> | |
| <p>agent: ${agent}</p> | |
| <p>name: ${data.title}</p> | |
| <p>description: ${data.description} </p> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment