Skip to content

Instantly share code, notes, and snippets.

@syntax-punk
Last active May 25, 2020 18:34
Show Gist options
  • Select an option

  • Save syntax-punk/9e6c0953dc6e90fbccca2b8497a3adc5 to your computer and use it in GitHub Desktop.

Select an option

Save syntax-punk/9e6c0953dc6e90fbccca2b8497a3adc5 to your computer and use it in GitHub Desktop.
AWS Lambda functions
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\"}"}
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