Skip to content

Commit

Permalink
Changes webserver to Fastify (#3)
Browse files Browse the repository at this point in the history
* adds logging capabilities

* replaces restify with fastify

* removes main restify server

* renames var

* removes deprecated dependencies

* removes deprecated next middleware function

* removes restify errors

* config eslint

* decouple server from app

* refactors integration tests

* fixes static file serving

* removes TODO
  • Loading branch information
rasouza authored Jan 10, 2021
1 parent 80a964a commit 1df567a
Show file tree
Hide file tree
Showing 18 changed files with 1,461 additions and 1,695 deletions.
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ DB_HOST=localhost
DB_PORT=27017
DB_DATABASE=users
DB_USER=myuser
DB_PASS=mypass
DB_PASS=mypass

LOG_LEVEL=info
24 changes: 24 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"env": {
"browser": true,
"commonjs": true,
"es2021": true,
"mocha": true
},
"extends": [
"standard"
],
"parserOptions": {
"ecmaVersion": 12
},
"rules": {
},
"overrides": [
{
"files": "*.test.js",
"rules": {
"no-unused-expressions": "off"
}
}
]
}
87 changes: 36 additions & 51 deletions coverage/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,30 @@ <h1>All files</h1>
<div class='clearfix'>

<div class='fl pad1y space-right2'>
<span class="strong">94.17% </span>
<span class="strong">96.12% </span>
<span class="quiet">Statements</span>
<span class='fraction'>113/120</span>
<span class='fraction'>124/129</span>
</div>


<div class='fl pad1y space-right2'>
<span class="strong">54.55% </span>
<span class="strong">61.54% </span>
<span class="quiet">Branches</span>
<span class='fraction'>6/11</span>
<span class='fraction'>8/13</span>
</div>


<div class='fl pad1y space-right2'>
<span class="strong">93.94% </span>
<span class="strong">97.06% </span>
<span class="quiet">Functions</span>
<span class='fraction'>31/33</span>
<span class='fraction'>33/34</span>
</div>


<div class='fl pad1y space-right2'>
<span class="strong">95.19% </span>
<span class="strong">96.49% </span>
<span class="quiet">Lines</span>
<span class='fraction'>99/104</span>
<span class='fraction'>110/114</span>
</div>


Expand All @@ -73,22 +73,7 @@ <h1>All files</h1>
</tr>
</thead>
<tbody><tr>
<td class="file high" data-value="node-clean-architecture"><a href="node-clean-architecture/index.html">node-clean-architecture</a></td>
<td data-value="100" class="pic high">
<div class="chart"><div class="cover-fill cover-full" style="width: 100%"></div><div class="cover-empty" style="width: 0%"></div></div>
</td>
<td data-value="100" class="pct high">100%</td>
<td data-value="4" class="abs high">4/4</td>
<td data-value="100" class="pct high">100%</td>
<td data-value="0" class="abs high">0/0</td>
<td data-value="100" class="pct high">100%</td>
<td data-value="0" class="abs high">0/0</td>
<td data-value="100" class="pct high">100%</td>
<td data-value="4" class="abs high">4/4</td>
</tr>

<tr>
<td class="file high" data-value="node-clean-architecture/application"><a href="node-clean-architecture/application/index.html">node-clean-architecture/application</a></td>
<td class="file high" data-value="application"><a href="application/index.html">application</a></td>
<td data-value="100" class="pic high">
<div class="chart"><div class="cover-fill cover-full" style="width: 100%"></div><div class="cover-empty" style="width: 0%"></div></div>
</td>
Expand All @@ -103,7 +88,7 @@ <h1>All files</h1>
</tr>

<tr>
<td class="file high" data-value="node-clean-architecture/domain"><a href="node-clean-architecture/domain/index.html">node-clean-architecture/domain</a></td>
<td class="file high" data-value="domain"><a href="domain/index.html">domain</a></td>
<td data-value="100" class="pic high">
<div class="chart"><div class="cover-fill cover-full" style="width: 100%"></div><div class="cover-empty" style="width: 0%"></div></div>
</td>
Expand All @@ -118,22 +103,22 @@ <h1>All files</h1>
</tr>

<tr>
<td class="file medium" data-value="node-clean-architecture/infrastructure/config"><a href="node-clean-architecture/infrastructure/config/index.html">node-clean-architecture/infrastructure/config</a></td>
<td data-value="75" class="pic medium">
<div class="chart"><div class="cover-fill" style="width: 75%"></div><div class="cover-empty" style="width: 25%"></div></div>
<td class="file high" data-value="infrastructure/config"><a href="infrastructure/config/index.html">infrastructure/config</a></td>
<td data-value="84.85" class="pic high">
<div class="chart"><div class="cover-fill" style="width: 84%"></div><div class="cover-empty" style="width: 16%"></div></div>
</td>
<td data-value="75" class="pct medium">75%</td>
<td data-value="20" class="abs medium">15/20</td>
<td data-value="33.33" class="pct low">33.33%</td>
<td data-value="6" class="abs low">2/6</td>
<td data-value="75" class="pct medium">75%</td>
<td data-value="4" class="abs medium">3/4</td>
<td data-value="76.47" class="pct medium">76.47%</td>
<td data-value="17" class="abs medium">13/17</td>
<td data-value="84.85" class="pct high">84.85%</td>
<td data-value="33" class="abs high">28/33</td>
<td data-value="50" class="pct medium">50%</td>
<td data-value="8" class="abs medium">4/8</td>
<td data-value="80" class="pct high">80%</td>
<td data-value="5" class="abs high">4/5</td>
<td data-value="86.67" class="pct high">86.67%</td>
<td data-value="30" class="abs high">26/30</td>
</tr>

<tr>
<td class="file high" data-value="node-clean-architecture/infrastructure/repositories"><a href="node-clean-architecture/infrastructure/repositories/index.html">node-clean-architecture/infrastructure/repositories</a></td>
<td class="file high" data-value="infrastructure/repositories"><a href="infrastructure/repositories/index.html">infrastructure/repositories</a></td>
<td data-value="100" class="pic high">
<div class="chart"><div class="cover-fill cover-full" style="width: 100%"></div><div class="cover-empty" style="width: 0%"></div></div>
</td>
Expand All @@ -148,33 +133,33 @@ <h1>All files</h1>
</tr>

<tr>
<td class="file high" data-value="node-clean-architecture/infrastructure/webserver"><a href="node-clean-architecture/infrastructure/webserver/index.html">node-clean-architecture/infrastructure/webserver</a></td>
<td class="file high" data-value="infrastructure/webserver"><a href="infrastructure/webserver/index.html">infrastructure/webserver</a></td>
<td data-value="100" class="pic high">
<div class="chart"><div class="cover-fill cover-full" style="width: 100%"></div><div class="cover-empty" style="width: 0%"></div></div>
</td>
<td data-value="100" class="pct high">100%</td>
<td data-value="10" class="abs high">10/10</td>
<td data-value="24" class="abs high">24/24</td>
<td data-value="100" class="pct high">100%</td>
<td data-value="0" class="abs high">0/0</td>
<td data-value="100" class="pct high">100%</td>
<td data-value="3" class="abs high">3/3</td>
<td data-value="4" class="abs high">4/4</td>
<td data-value="100" class="pct high">100%</td>
<td data-value="10" class="abs high">10/10</td>
<td data-value="24" class="abs high">24/24</td>
</tr>

<tr>
<td class="file high" data-value="node-clean-architecture/ports/http"><a href="node-clean-architecture/ports/http/index.html">node-clean-architecture/ports/http</a></td>
<td data-value="94.59" class="pic high">
<div class="chart"><div class="cover-fill" style="width: 94%"></div><div class="cover-empty" style="width: 6%"></div></div>
<td class="file high" data-value="ports/http"><a href="ports/http/index.html">ports/http</a></td>
<td data-value="100" class="pic high">
<div class="chart"><div class="cover-fill cover-full" style="width: 100%"></div><div class="cover-empty" style="width: 0%"></div></div>
</td>
<td data-value="94.59" class="pct high">94.59%</td>
<td data-value="37" class="abs high">35/37</td>
<td data-value="100" class="pct high">100%</td>
<td data-value="23" class="abs high">23/23</td>
<td data-value="100" class="pct high">100%</td>
<td data-value="0" class="abs high">0/0</td>
<td data-value="87.5" class="pct high">87.5%</td>
<td data-value="8" class="abs high">7/8</td>
<td data-value="97.06" class="pct high">97.06%</td>
<td data-value="34" class="abs high">33/34</td>
<td data-value="100" class="pct high">100%</td>
<td data-value="7" class="abs high">7/7</td>
<td data-value="100" class="pct high">100%</td>
<td data-value="21" class="abs high">21/21</td>
</tr>

</tbody>
Expand All @@ -185,7 +170,7 @@ <h1>All files</h1>
<div class='footer quiet pad2 space-top1 center small'>
Code coverage generated by
<a href="https://istanbul.js.org/" target="_blank">istanbul</a>
at Mon Sep 07 2020 00:37:03 GMT-0300 (Brasilia Standard Time)
at Sat Jan 09 2021 21:19:08 GMT-0300 (Brasilia Standard Time)
</div>
</div>
<script src="prettify.js"></script>
Expand Down
10 changes: 8 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
require('dotenv').config()
require('make-promises-safe')

const container = require('./infrastructure/config/container')()
const server = require('./infrastructure/webserver/server')
const server = require('./infrastructure/webserver/fastify')(container.cradle)

module.exports = server(container.cradle)
server.listen(3000, (err, address) => {
if (err) {
server.log.error(err)
process.exit(1)
}
})
35 changes: 5 additions & 30 deletions infrastructure/config/container.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,18 @@
const { createContainer, asFunction } = require('awilix')
const { createContainer } = require('awilix')
const resolveLogger = require('./logger')
const resolveDB = require('./database')

const container = createContainer()

const resolveDB = ({ DB_DRIVER, NODE_ENV }) => {
if (NODE_ENV === 'test') DB_DRIVER = 'in-memory'

if (DB_DRIVER === 'in-memory') inMemoryDB()
else if (DB_DRIVER === 'mongo') mongoDB()
}

const inMemoryDB = () => {
const UserRepositoryInMemory = require('../repositories/UserRepositoryInMemory')

container.register({
UserRepository: asFunction(UserRepositoryInMemory).singleton()
})
}

const mongoDB = () => {
const UserRepositoryMongo = require('../repositories/UserRepositoryMongo')

// Load Database and Schemas
container.loadModules([
'infrastructure/database/**/*.js'
])

container.register({
UserRepository: asFunction(UserRepositoryMongo)
})
}

module.exports = () => {
container.loadModules([
'ports/**/*.js',
'application/**/*.js',
'domain/**/*.js'
])

resolveDB(process.env)
resolveDB(container)
resolveLogger(container)

return container
}
31 changes: 31 additions & 0 deletions infrastructure/config/database.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const { asFunction } = require('awilix')

const inMemoryDB = container => {
const UserRepositoryInMemory = require('../repositories/UserRepositoryInMemory')

container.register({
UserRepository: asFunction(UserRepositoryInMemory).singleton()
})
}

const mongoDB = container => {
const UserRepositoryMongo = require('../repositories/UserRepositoryMongo')

// Load Database and Schemas
container.loadModules([
'infrastructure/database/**/*.js'
])

container.register({
UserRepository: asFunction(UserRepositoryMongo)
})
}

const resolveDB = container => {
if (process.env.NODE_ENV === 'test') process.env.DB_DRIVER = 'in-memory'

if (process.env.DB_DRIVER === 'in-memory') inMemoryDB(container)
else if (process.env.DB_DRIVER === 'mongo') mongoDB(container)
}

module.exports = resolveDB
22 changes: 22 additions & 0 deletions infrastructure/config/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const { asValue } = require('awilix')

const resolveLogger = container => {
const isDev = process.env.NODE_ENV === 'development'
const config = {
name: 'app',
prettyPrint: isDev,
level: process.env.LOG_LEVEL || 'info'
}

const Logger = require('pino')(config)

container.register({
Logger: asValue(Logger)
})

container.register({
LoggerConfig: asValue(config)
})
}

module.exports = resolveLogger
4 changes: 2 additions & 2 deletions infrastructure/repositories/UserRepositoryInMemory.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const first = require('lodash/fp/first')
const remove = require('lodash/fp/remove')
const merge = require('lodash/fp/merge')

const { NotFoundError, ForbiddenError } = require('restify-errors')
const { NotFoundError, AlreadyExistsError } = require('../webserver/errors')

module.exports = () => {
return {
Expand All @@ -15,7 +15,7 @@ module.exports = () => {

persist (user) {
const duplicated = filter({ cpf: user.cpf }, this.db)
if (duplicated.length > 0) throw new ForbiddenError('This CPF already exists')
if (duplicated.length > 0) throw new AlreadyExistsError('This CPF already exists')

user.id = `${++this.lastId}`
this.db.push(user)
Expand Down
34 changes: 13 additions & 21 deletions infrastructure/repositories/UserRepositoryMongo.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { NotFoundError, ForbiddenError } = require('restify-errors')
const { NotFoundError, AlreadyExistsError } = require('../webserver/errors')

const MONGO_ALREADY_EXISTS = 11000

Expand Down Expand Up @@ -27,31 +27,23 @@ module.exports = ({ User, UserSchema }) => ({
)
} catch (err) {
if (err.code === MONGO_ALREADY_EXISTS) {
throw new ForbiddenError('This CPF already exists')
throw new AlreadyExistsError('This CPF already exists')
}
}
},

get: async id => {
try {
const mongooseUser = await UserSchema.findById(id)
if (!mongooseUser) throw new NotFoundError('User not found')
const mongooseUser = await UserSchema.findById(id)
if (!mongooseUser) throw new NotFoundError('User not found')

return new User(
mongooseUser.id,
mongooseUser.name,
mongooseUser.cpf,
mongooseUser.birthdate,
mongooseUser.subscription,
mongooseUser.dependents
)
} catch (err) {
if (err.name === 'CastError') {
throw new NotFoundError('User not found')
} else {
throw err
}
}
return new User(
mongooseUser.id,
mongooseUser.name,
mongooseUser.cpf,
mongooseUser.birthdate,
mongooseUser.subscription,
mongooseUser.dependents
)
},

merge: async (id, data) => {
Expand All @@ -71,7 +63,7 @@ module.exports = ({ User, UserSchema }) => ({
if (err.name === 'CastError') {
throw new NotFoundError('User not found')
} else if (err.code === MONGO_ALREADY_EXISTS) {
throw new ForbiddenError('This CPF already exists')
throw new AlreadyExistsError('This CPF already exists')
} else {
throw err
}
Expand Down
Loading

0 comments on commit 1df567a

Please sign in to comment.