Skip to content

Commit

Permalink
Merge pull request #44 from ryanmurakami/v3
Browse files Browse the repository at this point in the history
11.2021 Update
  • Loading branch information
ryanmurakami authored Nov 19, 2021
2 parents 86fd0a6 + be90ce8 commit a7bde03
Show file tree
Hide file tree
Showing 47 changed files with 29,872 additions and 8,493 deletions.
7 changes: 5 additions & 2 deletions deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ archive.directory('plugins/', 'plugins')
archive.directory('public/', 'public')
archive.directory('routes/', 'routes')
archive.directory('util/', 'util')
archive.append(fs.createReadStream(path.join(__dirname, '.env')), { name: '.env' })
archive.append(fs.createReadStream(path.join(__dirname, 'index.js')), { name: 'index.js' })
archive.append(fs.createReadStream(path.join(__dirname, 'package.json')), { name: 'package.json' })
archive.append(fs.createReadStream(path.join(__dirname, 'package-lock.json')), { name: 'package-lock.json' })
archive.append(fs.createReadStream(path.join(__dirname, 'webpack.config.js')), { name: 'webpack.config.js' })

archive.finalize()
Expand Down Expand Up @@ -116,8 +118,9 @@ function unpack (ip) {
const command = 'cd /home/bitnami && ' +
'unzip -o -q ./archive.zip -d hbfl && ' +
'cd hbfl && ' +
'npm install && ' +
'sudo npm start'
'npm ci && ' + // installs dependencies
'kill -9 $(pgrep -f node | grep -v ^$$\\$) && ' + // this kills any existing node processes except self
'npm start' // starts app

exec(command, {
user: 'bitnami',
Expand Down
20 changes: 20 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ require('dotenv').config()

const plugins = require('./plugins')
const routes = require('./routes')
const { logger } = require('./util/logger')
const { init: usersInit } = require('./lib/data/users')
const { init: queueInit } = require('./lib/data/lib/sqs.listener')

Expand Down Expand Up @@ -62,6 +63,24 @@ const init = async () => {
// register routes
server.route(routes)

// logging
server.events.on('log', (_, event) => {
if (event.error) {
logger.error(`Server error: ${event.error.message || 'unknown'}`);
} else {
logger.info(`Server event: ${event}`)
}
})

server.events.on('request', (_, event) => {
if (event.tags.includes('unauthenticated')) return
if (event.tags.includes('error')) {
logger.error(`Request error: ${event.data || event.error.message || 'unknown'}`);
} else {
logger.info(`Request event: ${event}`)
}
})

// initialize database and start server
usersInit()
// Commented out until SQS is configured
Expand All @@ -72,6 +91,7 @@ const init = async () => {
console.log(`Server started at http://localhost:${server.info.port}`)
} catch (err) {
console.error(`Server could not start. Error: ${err}`)
logger.error(`Server could not start. Error: ${err}`)
}
})
}
Expand Down
46 changes: 22 additions & 24 deletions lib/data/lib/dynamo.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,34 @@
const AWS = require('aws-sdk')

AWS.config.update({ region: '/*TODO: Add your region */' })

const client = new AWS.DynamoDB.DocumentClient()

function getAll (tableName) {
// TODO: Declare params for scan

return new Promise((resolve, reject) => {
// TODO: Scan table and return
})
const {
DynamoDBClient
} = require('@aws-sdk/client-dynamodb')
const {
DynamoDBDocumentClient,
PutCommand,
QueryCommand,
ScanCommand
} = require('@aws-sdk/lib-dynamodb')

async function sendCommand (command) {
const client = new DynamoDBClient({ region: process.env.AWS_REGION })
const docClient = DynamoDBDocumentClient.from(client)
return docClient.send(command)
}

function get (tableName, id) {
// TODO: Declare params for query
async function getAll (tableName) {
// TODO: Scan table and return Items
}

return new Promise((resolve, reject) => {
// TODO: Query table and return
})
async function get (tableName, id) {
// TODO: Query table and return first item
}

function put (tableName, item) {
async function put (tableName, item) {
const params = {
TableName: tableName,
Item: item
}
return new Promise((resolve, reject) => {
client.put(params, (err, data) => {
if (err) reject(err)
else resolve(data)
})
})
const command = new PutCommand(params)
return sendCommand(command)
}

module.exports = {
Expand Down
4 changes: 3 additions & 1 deletion lib/data/lib/kinesis.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ const {
const client = new KinesisClient({ region: process.env.AWS_REGION })

async function send (streamName, partition, msg) {
// TODO: Create params const object
const params = {
// TODO: Add properties to put record
}

try {
const command = new PutRecordCommand(params)
Expand Down
6 changes: 3 additions & 3 deletions lib/data/lib/mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,19 @@ const raceData = [
id: 1,
venue: 'Petco 2000',
city: 'Seattle, WA',
date: '04/29/17',
date: '04/29/22',
results: []
}, {
id: 2,
venue: 'Triscuit Circuit 4700',
city: 'Daytona Beach, FL',
date: '09/21/17',
date: '09/21/22',
results: []
}, {
id: 3,
venue: 'Kraft 35',
city: 'Tokyo, Japan',
date: '07/14/17',
date: '07/14/22',
results: []
}
]
Expand Down
13 changes: 6 additions & 7 deletions lib/data/lib/sns.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
const { SNSClient } = require('@aws-sdk/client-sns')
const {
SNSClient,
PublishCommand
} = require('@aws-sdk/client-sns')

const client = new SNSClient({ region: process.env.AWS_REGION })
const TOPIC_ARN = '/* TODO: Add your topic arn */'
const topicArn = '/* TODO: Add your topic arn */'

function publish (msg) {
// TODO: Create params const object

return new Promise((resolve, reject) => {
// TODO: Publish message
})
// TODO: Publish message to SNS topic
}

module.exports = { publish }
16 changes: 8 additions & 8 deletions lib/data/lib/sqs.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const { SQSClient } = require('@aws-sdk/client-sqs')
const {
SQSClient,
GetQueueUrlCommand,
SendMessageCommand
} = require('@aws-sdk/client-sqs')

const client = new SQSClient({ region: process.env.AWS_REGION })

function push (queueName, msg) {
// TODO: Create params const to get queue URL

return new Promise((resolve, reject) => {
// TODO: Get sqs queue URL
// Then send message to queue url
})
async function push (queueName, msg) {
// TODO: Get sqs queue URL
// Then send message to queue url
}

module.exports = { push }
22 changes: 14 additions & 8 deletions lib/data/lib/sqs.listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,41 @@ function init () {
}

async function poll () {
// TODO: Create params const for getting queue URL
const queueParams = {
// TODO: Add properties for getting queue URL
}

const getUrlCommand = new GetQueueUrlCommand()
const getUrlCommand = new GetQueueUrlCommand(queueParams)
const queueData = await client.send(getUrlCommand)

// TODO: Create params const for receiving message
const receiveCommand = new ReceiveMessageCommand()
const receiveParams = {
// TODO: Add properties for receiving message
}
const receiveCommand = new ReceiveMessageCommand(receiveParams)
const msgData = await client.send(receiveCommand)

if (!msgData.Messages || !msgData.Messages.length) {
return `No messages in queue ${RACE_QUEUE}`
} else {
const resultsMap = groupMessagesByHamster(messages)
const resultsMap = groupMessagesByHamster(msgData.Messages)

for (const key in resultsMap) {
await putResults(key, resultsMap[key])
}

await deleteMsgs(msgData.Messages, queueData.QueueUrl)

console.log(`Processed ${msgData.Messages.length} messages from SQS`)
return `Processed ${msgData.Messages.length} messages from SQS`
}
}

async function deleteMsgs (results, queueUrl) {
for (const result of results) {
// TODO: Create params const for deleting message
const params = {
// TODO: Add properties for deleting message
}

const command = new DeleteMessageCommand()
const command = new DeleteMessageCommand(params)
await client.send(command)
}
}
Expand Down
Loading

0 comments on commit a7bde03

Please sign in to comment.