Last active
May 25, 2020 18:34
-
-
Save syntax-punk/9e6c0953dc6e90fbccca2b8497a3adc5 to your computer and use it in GitHub Desktop.
AWS Lambda functions
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
| const AWS = require("aws-sdk"); | |
| const { randomize } = require("./randomize.js"); | |
| const documentClient = new AWS.DynamoDB.DocumentClient(); | |
| exports.handler = async event => { | |
| const { url } = JSON.parse(event.body); | |
| const id = randomize(6); | |
| const params = { | |
| TableName: // dynamoDB, | |
| Item: { | |
| id, | |
| url, | |
| timestamp: Date.now() | |
| } | |
| }; | |
| try { | |
| const data = await documentClient.put(params).promise(); | |
| const response = { | |
| statusCode: 200, | |
| headers: { | |
| "Access-Control-Allow-Origin": "*" | |
| }, | |
| body: JSON.stringify({id}) | |
| }; | |
| return response; | |
| } catch (e) { | |
| return { | |
| statusCode: 500, | |
| headers: { | |
| "Access-Control-Allow-Origin": "*" | |
| }, | |
| body: JSON.stringify(e) | |
| }; | |
| } | |
| }; | |
| // {"body": "{\"url\": \"https://twitter.com\"}"} |
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
| const AWS = require("aws-sdk"); | |
| const documentClient = new AWS.DynamoDB.DocumentClient(); | |
| exports.handler = async (event) => { | |
| const id = event.pathParameters.id; | |
| if(id) { | |
| try { | |
| const params = { | |
| TableName: // dynamoDB, | |
| Key: { | |
| id: id | |
| } | |
| } | |
| const data = await documentClient.get(params).promise(); | |
| if (data.Item) { | |
| return({ | |
| statusCode: 302, | |
| headers: { | |
| Location: data.Item.url | |
| } | |
| }) | |
| } | |
| } catch(e) { | |
| return({ | |
| statusCode: 302, | |
| headers: { | |
| Location: "https://slit.link/error" | |
| } | |
| }) | |
| } | |
| } | |
| return({ | |
| statusCode: 302, | |
| headers: { | |
| Location: "https://slit.link/lnf" | |
| } | |
| }) | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment