Skip to content

Commit

Permalink
Merge pull request #68 from ryanmurakami/v3
Browse files Browse the repository at this point in the history
3 is the new 2
  • Loading branch information
ryanmurakami committed Sep 19, 2021
2 parents aaafade + 23f0b31 commit 415ba93
Show file tree
Hide file tree
Showing 9 changed files with 6,509 additions and 1,791 deletions.
1,291 changes: 521 additions & 770 deletions cloudformation/pizza.template

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion handlers/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ module.exports = async (req, h) => {
return h.redirect('/')
}

let user

if (req.method === 'post') {
const user = await users.authenticate(req.payload.username.toLowerCase(), req.payload.password)
try {
user = await users.authenticate(req.payload.username.toLowerCase(), req.payload.password)
} catch (err) {
console.error(err.message)
}
if (!user) return Boom.unauthorized()
const sid = String(Math.random())
await req.server.app.cache.set(sid, user, 0)
Expand Down
11 changes: 7 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
const Hapi = require('@hapi/hapi')
const { logger } = require('./util/logger')

const plugins = require('./plugins')
const plugins = require('./util/plugins')
const routes = require('./routes')

async function startServer () {
const server = Hapi.Server({
port: 3000
})

await plugins.register(server)
await plugins.register(server, logger)
routes.register(server)

try {
await server.start()
console.log(`Server running at: ${server.info.uri}`)
} catch (err) {
console.error(`Server could not start. Error: ${err}`)
logger.error(`Server could not start. Error: ${err}`)
}
}

process.on('unhandledRejection', err => {
console.log(err)
process.on('unhandledRejection', (reason, promise) => {
console.error(`Reason: ${reason.message}, Stack: ${reason?.stack}`)
logger.error(`Reason: ${reason.message}, Stack: ${reason?.stack}`)
process.exit()
})

Expand Down
4 changes: 0 additions & 4 deletions nodemon.json

This file was deleted.

6,899 changes: 5,925 additions & 974 deletions package-lock.json

Large diffs are not rendered by default.

19 changes: 10 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
{
"name": "pizza-luvrs",
"version": "1.1.0",
"version": "2.0.0",
"description": "A social oasis for lovers of pizza.",
"repository": "https://github.com/ryanmurakami/pizza-luvrs",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"author": "Ryan Lewis <[email protected]>",
"author": "Ryan Lewis <[email protected]>",
"license": "MIT",
"dependencies": {
"@hapi/boom": "9.1.3",
"@aws-sdk/client-dynamodb": "3.29.0",
"@aws-sdk/client-s3": "3.29.0",
"@aws-sdk/lib-dynamodb": "3.29.0",
"@hapi/boom": "9.1.4",
"@hapi/catbox": "11.1.1",
"@hapi/catbox-redis": "6.0.2",
"@hapi/cookie": "11.0.2",
"@hapi/good": "9.0.1",
"@hapi/good-squeeze": "6.0.0",
"@hapi/hapi": "20.1.5",
"@hapi/inert": "6.0.3",
"@hapi/inert": "6.0.4",
"@hapi/vision": "6.1.0",
"aws-sdk": "2.958.0",
"bcryptjs": "2.4.3",
"bootflat": "2.0.4",
"fs-extra": "10.0.0",
"handlebars": "4.7.7",
"joi": "17.4.1",
"joi": "17.4.2",
"lodash": "4.17.21",
"pg": "8.7.1",
"sequelize": "6.6.5"
"sequelize": "6.6.5",
"winston": "3.3.3"
}
}
2 changes: 1 addition & 1 deletion templates/layout.hbs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://code.jquery.com/jquery-3.0.0.min.js" integrity="sha256-JmvOoLtYsmqlsWxa7mDSLMwa6dZ9rrIdtrrVYRnDRH0=" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js" crossorigin="anonymous"></script>
<link rel="stylesheet" href="/assets/css/stylesheet.css" />
<link rel="shortcut icon" href="/assets/favicon.ico" type="image/x-icon">
<title>pizza luvrs</title>
Expand Down
13 changes: 13 additions & 0 deletions util/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const winston = require('winston')

// set up logger
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
transports: [
new winston.transports.File({ filename: './log/error.log', level: 'error' }),
new winston.transports.File({ filename: './log/combined.log' }),
]
})

module.exports = { logger }
53 changes: 25 additions & 28 deletions plugins.js → util/plugins.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,16 @@
const Cookie = require('@hapi/cookie')
const Good = require('@hapi/good')
const Handlebars = require('handlebars')
const Inert = require('@hapi/inert')
const Vision = require('@hapi/vision')

const GoodFile = require('./lib/good-file')
const mockData = require('./data/mock')
const mockData = require('../data/mock')

const goodOptions = {
reporters: {
file: [{
module: '@hapi/good-squeeze',
name: 'Squeeze',
args: [{ response: '*', request: '*', error: '*' }]
}, {
module: '@hapi/good-squeeze',
name: 'SafeJson'
}, {
module: GoodFile,
args: ['./log/hapi_log']
}]
}
}

module.exports.register = async server => {
module.exports.register = async (server, logger) => {
// register plugins
await server.register([
Inert,
Vision,
Cookie,
{
plugin: Good,
options: goodOptions
}
Cookie
])

// setup template rendering
Expand All @@ -42,9 +20,9 @@ module.exports.register = async server => {
},
layout: true,
relativeTo: __dirname,
path: './templates',
partialsPath: './templates/partials',
helpersPath: './templates/helpers'
path: '../templates',
partialsPath: '../templates/partials',
helpersPath: '../templates/helpers'
})

// setup cache
Expand Down Expand Up @@ -79,6 +57,25 @@ module.exports.register = async server => {

server.auth.default('session')

// 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}`)
}
})

// setup data
mockData.hydrate()
}

0 comments on commit 415ba93

Please sign in to comment.