Skip to content

Instantly share code, notes, and snippets.

View rts-rob's full-sized avatar
👋
Howdy

Rob Sutter rts-rob

👋
Howdy
View GitHub Profile
@rts-rob
rts-rob / handler.js
Last active September 9, 2017 08:41
Default Serverless handler.js
'use strict';
module.exports.hello = (event, context, callback) => {
const response = {
statusCode: 200,
body: JSON.stringify({
message: 'Go Serverless v1.0! Your function executed successfully!',
input: event,
}),
};
@rts-rob
rts-rob / serverless.yml
Created September 9, 2017 08:40
Default Serverless serverless.yml
service: aws-nodejs # NOTE: update this with your service name
provider:
name: aws
runtime: nodejs6.10
functions:
hello:
handler: handler.hello
@rts-rob
rts-rob / handler.js
Last active September 9, 2017 08:59
Final handler.js
'use strict';
let fs = require('fs');
let mysql = require('mysql');
let querystring = require('querystring');
module.exports.reset = (event, context, callback) => {
// Extract the Slack username
const requestingUser = querystring.parse(event.body).user_name;
@rts-rob
rts-rob / serverless.yml
Last active September 9, 2017 09:09
Final serverless.yml
service: dbreset
provider:
name: aws
runtime: nodejs6.10
region: eu-central-1
functions:
reset:
handler: handler.reset
@rts-rob
rts-rob / querystring.js
Last active September 9, 2017 10:03
Querystring example
let querystring = require('querystring');
...
const requestingUser = querystring.parse(event.body).user_name;
@rts-rob
rts-rob / mysql-connection.js
Created September 9, 2017 10:02
Setup for MySQL Connection
let mysql = require('mysql');
...
const connection = mysql.createConnection({
host: "somedatabasehost.com",
user: "someuser",
password: "somepassword",
multipleStatements: true
});
...
connection.connect();
@rts-rob
rts-rob / mysql-query.js
Created September 9, 2017 10:07
Sample query execution
let fs = require('fs');
...
const resetScript = fs.readFileSync("resetdata.sql");
const query = `USE ${requestingUser}; ${resetScript}`;
...
connection.query(query, function (error, results, fields) {
connection.end();
@rts-rob
rts-rob / lambda-response.js
Created September 9, 2017 10:09
Send a MySQL query response back through the callback
const response = {
statusCode: 200,
body: JSON.stringify({
text: `Hi, ${requestingUser}.\nYour database has been reset!`
})
};
callback(null, response);
@rts-rob
rts-rob / authentication.js
Created September 11, 2017 06:21
Basic psuedocode example of a signup/login flow with salt and hash
function signup (email, password) {
const salt = uuid();
const hashedPassword = hash(`${salt}${password}`);
// this stores everything in the DB
createUser(email, salt, hashedPassword);
}
function login (request, response) {
// get the salt - SELECT salt FROM users
// WHERE email = ?, [email]
@rts-rob
rts-rob / archetype.js
Created September 11, 2017 06:22
Basic archetype of every express function ever
function archetype (request, response) {
// Get Data out of the request
// Process that data
// Build and send the response
response.setHeaders();
response.write();
response.end();
}