Skip to content

Commit

Permalink
code: chapter 9 (PacktPublishing#7)
Browse files Browse the repository at this point in the history
* chore: add application scaffolding from chapter 6

* fix: merge updates
  • Loading branch information
Eomm committed Apr 28, 2022
1 parent 48c8be7 commit 207f62b
Show file tree
Hide file tree
Showing 30 changed files with 16,344 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ typings/
.coveralls.yml

package-lock.json
!Chapter 9/package-lock.json

demo/*

Expand Down
3 changes: 3 additions & 0 deletions Chapter 9/.env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
NODE_ENV=development
PORT=3001
MONGO_URL=mongodb://localhost:27017/todo
1 change: 1 addition & 0 deletions Chapter 9/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"extends": "standard"}
27 changes: 27 additions & 0 deletions Chapter 9/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
FROM node:16-alpine

# set envs
ENV HOME=/home/app
ENV APP_HOME=$HOME/node/
ENV NODE_ENV=production

# add app dependencies
COPY package.json $APP_HOME
COPY package-lock.json $APP_HOME

# change workgin dir and install deps
WORKDIR $APP_HOME
RUN npm ci --only=production

# copy all app files
COPY --chown=node:node . $APP_HOME

# dumb-init registers signal handlers for every signal that can be caught
RUN apk update && apk add --no-cache dumb-init

# run app with low permissions level user
USER node

EXPOSE 3000
ENTRYPOINT ["dumb-init"]
CMD ["npm", "start"]
21 changes: 21 additions & 0 deletions Chapter 9/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Backend Cafe

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
29 changes: 29 additions & 0 deletions Chapter 9/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Chapter 9 - Application testing

This chapter will implements the tests for our project by running the application and mocking when necessary.

## How to use this folder?

In this folder you will find the source code of the chapter.
The filenames are the same as the chapter's sections.

Install the application first by running:

```
npm ci
```

You can run the chapter's code by starting the application:

```bash
npm test
```

⚠️ You need a running `docker` application to run the tests.

Before running the code examples, you need to install the dependencies running `npm install`.

## Trouble?

If you encounter any problems while reading the chapter or while running the code examples,
you can ask for help on the [official Discord chat](TODO) TODO LINK
32 changes: 32 additions & 0 deletions Chapter 9/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict'

const path = require('path')
const AutoLoad = require('fastify-autoload')

module.exports = async function (fastify, opts) {
fastify.register(AutoLoad, {
dir: path.join(__dirname, 'schemas'),
indexPattern: /^loader.js$/i
})

fastify.register(AutoLoad, {
dir: path.join(__dirname, 'plugins'),
dirNameRoutePrefix: false,
ignorePattern: /.*.no-load\.js/,
// ignorePattern: /^((?!load\.js).)*$/,
indexPattern: /^no$/i,
options: Object.assign({}, opts)
})

fastify.register(AutoLoad, {
dir: path.join(__dirname, 'routes'),
indexPattern: /.*routes(\.js|\.cjs)$/i,
ignorePattern: /.*\.js/,
autoHooksPattern: /.*hooks(\.js|\.cjs)$/i,
autoHooks: true,
cascadeHooks: true,
options: Object.assign({}, opts)
})
}

module.exports.options = require('./configs/server-options')
9 changes: 9 additions & 0 deletions Chapter 9/configs/server-options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

module.exports = {
ajv: {
customOptions: {
coerceTypes: 'array',
removeAdditional: 'all'
}
}
}
Loading

0 comments on commit 207f62b

Please sign in to comment.