Skip to content

Commit a5b8d46

Browse files
committed
init commit
1 parent fbabc6c commit a5b8d46

29 files changed

+3042
-34
lines changed

.editorconfig

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Editor configuration, see http://editorconfig.org
2+
root = true
3+
4+
[*]
5+
charset = utf-8
6+
indent_style = space
7+
indent_size = 4
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true
10+
11+
[*.md]
12+
max_line_length = off
13+
trim_trailing_whitespace = false
14+
15+
[*.json]
16+
indent_style = space
17+
indent_size = 2

.gitignore

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,6 @@
1-
# Logs
2-
logs
3-
*.log
4-
npm-debug.log*
5-
6-
# Runtime data
7-
pids
8-
*.pid
9-
*.seed
10-
11-
# Directory for instrumented libs generated by jscoverage/JSCover
12-
lib-cov
13-
14-
# Coverage directory used by tools like istanbul
15-
coverage
16-
17-
# nyc test coverage
18-
.nyc_output
19-
20-
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
21-
.grunt
22-
23-
# node-waf configuration
24-
.lock-wscript
25-
26-
# Compiled binary addons (http://nodejs.org/api/addons.html)
27-
build/Release
28-
29-
# Dependency directories
1+
# package directories
302
node_modules
313
jspm_packages
324

33-
# Optional npm cache directory
34-
.npm
35-
36-
# Optional REPL history
37-
.node_repl_history
5+
# Serverless directories
6+
.serverless

.jscsrc

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"disallowKeywords": ["with"],
3+
"disallowKeywordsOnNewLine": ["else"],
4+
"disallowMixedSpacesAndTabs": true,
5+
"disallowMultipleVarDecl": {
6+
"allExcept": ["require", "undefined"]
7+
},
8+
"disallowNewlineBeforeBlockStatements": true,
9+
"disallowQuotedKeysInObjects": true,
10+
"disallowSpaceAfterObjectKeys": true,
11+
"disallowSpaceAfterPrefixUnaryOperators": true,
12+
"disallowSpacesInFunction": {
13+
"beforeOpeningRoundBrace": true
14+
},
15+
"disallowTrailingComma": true,
16+
"disallowSpacesInsideParentheses": true,
17+
"disallowTrailingWhitespace": false,
18+
"maximumLineLength": 120,
19+
"requireCamelCaseOrUpperCaseIdentifiers": true,
20+
"requireCapitalizedComments": false,
21+
"requireCapitalizedConstructors": true,
22+
"requireCurlyBraces": true,
23+
"requireSpaceAfterKeywords": [
24+
"if",
25+
"else",
26+
"for",
27+
"while",
28+
"do",
29+
"switch",
30+
"case",
31+
"return",
32+
"try",
33+
"catch",
34+
"typeof"
35+
],
36+
"requireSpaceAfterLineComment": true,
37+
"requireSpaceAfterBinaryOperators": true,
38+
"requireSpaceBeforeBinaryOperators": true,
39+
"requireSpaceBeforeBlockStatements": true,
40+
"requireSpaceBeforeObjectValues": true,
41+
"requireSpacesInFunction": {
42+
"beforeOpeningCurlyBrace": true
43+
},
44+
"requireEarlyReturn": true,
45+
"validateIndentation": 4,
46+
"validateLineBreaks": false,
47+
"validateQuoteMarks": "'"
48+
}

.jshintrc

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
{
2+
"bitwise": true,
3+
"camelcase": false,
4+
"curly": true,
5+
"eqeqeq": true,
6+
"es3": false,
7+
"forin": true,
8+
"freeze": true,
9+
"immed": true,
10+
"indent": 4,
11+
"latedef": "nofunc",
12+
"newcap": true,
13+
"noarg": true,
14+
"noempty": true,
15+
"nonbsp": true,
16+
"nonew": true,
17+
"plusplus": false,
18+
"quotmark": "single",
19+
"undef": true,
20+
"unused": true,
21+
"strict": false,
22+
"maxparams": 10,
23+
"maxdepth": 2,
24+
"maxstatements": 40,
25+
"maxcomplexity": 8,
26+
"maxlen": 0,
27+
28+
"asi": false,
29+
"boss": false,
30+
"debug": false,
31+
"eqnull": true,
32+
"esnext": false,
33+
"evil": false,
34+
"expr": false,
35+
"funcscope": false,
36+
"globalstrict": false,
37+
"iterator": false,
38+
"lastsemic": false,
39+
"laxbreak": true,
40+
"laxcomma": false,
41+
"loopfunc": true,
42+
"maxerr": false,
43+
"moz": false,
44+
"multistr": false,
45+
"notypeof": false,
46+
"proto": false,
47+
"scripturl": false,
48+
"shadow": false,
49+
"sub": true,
50+
"supernew": false,
51+
"validthis": false,
52+
"noyield": false,
53+
"esversion": 6,
54+
55+
"browser": true,
56+
"node": true,
57+
"jasmine": true,
58+
59+
"globals": {
60+
"_": true,
61+
"moment": true
62+
}
63+
64+
}

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Hapi.js Lambda Demo
2+
3+
A demo of running Hapi.js on AWS Lambda.
4+
5+
This application uses Hapi as the foundation, and is deployed to AWS Lambda and API gateway. We use the [Serverless](https://serverless.com) framework to provision and deploy the stack to AWS, as well as run it locally for debugging purposes.
6+
7+
## Getting Started
8+
9+
Run `yarn install` from a shell or bash based window, then execute any of the following commands. To execute deployments, you must have configured your default AWS profile with an account that has access to Lambda, API Gateway, and Cloudformation.
10+
11+
To edit any of the parameters involved in deploying to AWS, check out the `serverless.yml` file.
12+
13+
## Development server
14+
15+
Run `npm start for a dev server. View the swagger file at `http://localhost:3000/swagger.json`.
16+
17+
## Debugging server
18+
19+
Run `npm run debug` for a debuggable instance. Setting the `NODE_ENV` variable to whatever environment you are running in will load the correct config. `dev, qa, stage, prod`
20+
21+
## Running unit tests
22+
23+
Run `npm test` to execute the unit tests via [Jasmine](https://jasmine.github.io/). To get detailed debugging output, run the tests with `DEBUG=true npm test`.
24+
25+
## API Documentation
26+
27+
API documentation is provided by Swagger and can be accessed at http://localhost:3000/swagger.json.
28+

debug-sls.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
3+
node_modules=`npm root -g`
4+
node --debug "$node_modules/serverless/bin/serverless" "$@"

index.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
3+
const server = require('./src');
4+
5+
/**
6+
* This module maps the Lambda proxy requests to the Hapijs router
7+
*/
8+
exports.handler = (event, context, callback) => {
9+
10+
server.makeReady((err) => {
11+
12+
if(err) throw err;
13+
14+
// map lambda event to hapi request
15+
const options = {
16+
method: event.httpMethod,
17+
url: event.path,
18+
payload: event.body,
19+
headers: event.headers,
20+
validate: false
21+
};
22+
23+
server.inject(options, function(res){
24+
const response = {
25+
statusCode: res.statusCode,
26+
body: res.result
27+
};
28+
callback(null, response);
29+
});
30+
31+
});
32+
33+
};

jasmine.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"spec_dir": "./",
3+
"spec_files": [
4+
"src/**/*.spec.js"
5+
],
6+
"helpers": [
7+
"src/**/*.helper.spec.js"
8+
]
9+
}

package.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "hapi-lambda-demo",
3+
"version": "1.0.0",
4+
"description": "An example of running Hapi.js on AWS Lambda",
5+
"main": "index.js",
6+
"scripts": {
7+
"start": "sls offline",
8+
"debug": "bash -c './debug-sls.sh offline'",
9+
"deploy": "sls deploy -v",
10+
"test": "jasmine JASMINE_CONFIG_PATH=jasmine.json",
11+
"lint": "jshint src || true"
12+
},
13+
"author": "",
14+
"license": "MIT",
15+
"devDependencies": {
16+
"hapi-swagger": "^7.7.0",
17+
"jasmine": "^2.5.3",
18+
"jshint": "^2.9.4",
19+
"jshint-stylish": "^2.2.1",
20+
"nock": "^9.0.9",
21+
"nodemon": "^1.11.0",
22+
"proxyquire": "^1.7.11",
23+
"serverless-offline": "^3.13.1",
24+
"sinon": "^1.17.7"
25+
},
26+
"dependencies": {
27+
"boom": "^4.2.0",
28+
"crumb": "^6.0.3",
29+
"extend": "^3.0.0",
30+
"glob": "^7.1.1",
31+
"good": "^7.1.0",
32+
"good-console": "^6.4.0",
33+
"good-squeeze": "^5.0.1",
34+
"hapi": "^16.1.0",
35+
"joi": "^10.2.2"
36+
}
37+
}

serverless.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
service: hapi-lambda-demo
2+
provider:
3+
name: aws
4+
runtime: nodejs6.10
5+
6+
stage: dev
7+
region: us-east-1
8+
9+
functions:
10+
api:
11+
handler: index.handler
12+
events:
13+
- http:
14+
path: "{proxy+}"
15+
method: any
16+
cors: true
17+
18+
plugins:
19+
- serverless-offline

src/api/health/health.controller.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
3+
const Boom = require('boom');
4+
5+
/**
6+
* Health Check API methods
7+
*/
8+
module.exports = {
9+
10+
/**
11+
* Returns health check information by replying with the input request
12+
*/
13+
getCheck: function (request, reply) {
14+
return reply({status: 'OK'});
15+
}
16+
17+
};

src/api/health/health.routes.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
const controller = require('./health.controller');
4+
5+
/**
6+
* Route configuration
7+
*/
8+
module.exports = [
9+
{
10+
method: 'GET',
11+
path: '/health/check',
12+
handler: controller.getCheck,
13+
config: {
14+
description: 'Returns the input http request.',
15+
notes: 'Used for testing the health of the application.',
16+
tags: ['api', 'health'],
17+
auth: false
18+
}
19+
}
20+
];

0 commit comments

Comments
 (0)