Skip to content

Commit

Permalink
Adjustments and improvements
Browse files Browse the repository at this point in the history
- Updates the base node image to 12.18.0-alpine
- Updates versions of the project dependencies
- Adds routine to load mongodb and rabbitmq client certificates
- Updates the CustomLogger class
- Updates the Query class to return the created_at classification as a default in toJSON
- Change the .mocharc.yml file to the project root
- Adds support for ES2020
- Adds settings for the travis service
- Updates README
- Adds new unit and integration tests
  • Loading branch information
douglasrafael committed Jun 30, 2020
1 parent b5d23ab commit cc70ff0
Show file tree
Hide file tree
Showing 29 changed files with 2,608 additions and 2,121 deletions.
87 changes: 85 additions & 2 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,85 @@
node_modules
npm-debug.log
# ignore .git and .cache folders
.git
.cache

# Docker files
.dockerignore
Dockerfile
Dockerfile.archive

# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# next.js build output
.next

# IDE
.idea

# Docs
docs

# Bash
bash.exe.stackdump

# certificates and keys directories and files
.certs
*.pem
*.crt
*.key
*.csr
*.cert
certs
64 changes: 46 additions & 18 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -58,29 +58,57 @@ SMTP_USER=YOUR_SMTP_USER
SMTP_PASS=YOUR_SMTP_PASS

#################################################################################################
##################################### MESSAGE CHANNEL SETUP #####################################
##################################### MONGO DATABASE SETUP #####################################
#################################################################################################

# RABBITMQ_URI: URI containing the parameters for connection to the message channel (RabbitMQ).
# format value: amqp://user:pass@host:port/vhost
# default value: amqp://guest:guest@127.0.0.1:5672/haniot
RABBITMQ_URI=amqp://guest:guest@127.0.0.1:5672
# MONGODB_URI: Database connection URI used by the ACCOUNT service for connecting to
# a MongoDB instance if the application is running in development or
# production environment (NODE_ENV=development or NODE_ENV=production).
# default value: mongodb://localhost:27017/account-service
MONGODB_URI=mongodb://localhost:27017/account-service

# MONGODB_URI_TEST: Database connection URI used by the ACCOUNT service for connecting to
# a MongoDB instance if the application is running test environment
# (NODE_ENV=test).
# default value: mongodb://localhost:27017/account-service-test
MONGODB_URI_TEST=mongodb://localhost:27017/account-service-test

# MONGODB_ENABLE_TLS: Enables/Disables connection to TLS
# When TLS is used for connection, client certificates
# are required (MONGODB_KEY_PATH, MONGODB_CA_PATH).
#
# default value: false
MONGODB_ENABLE_TLS=false

# MONGODB_KEY_PATH: Client certificate and key in .pem format to connect to MongoDB
# default value: .certs/mongodb/client.pem
MONGODB_KEY_PATH=.certs/mongodb/client.pem

# RABBITMQ_CA_PATH: Certificate CA
# default value: .certs/ca.crt
# NOTE: Only required if protocol is amqps
RABBITMQ_CA_PATH=.certs/ca.crt
# MONGODB_CA_PATH: MongoDB Certificate of the Authentication entity (CA)
# default value: .certs/mongodb/client.pem
MONGODB_CA_PATH=.certs/mongodb/ca.pem

#################################################################################################
##################################### MONGO DATABASE SETUP #####################################
##################################### MESSAGE CHANNEL SETUP #####################################
#################################################################################################

# MONGODB_URI: Database connection URI used if the application is running in development
# or production environment (NODE_ENV=development or NODE_ENV=production)
# default value: mongodb://127.0.0.1:27017/notification-service
MONGODB_URI=mongodb://127.0.0.1:27017/notification-service
# RABBITMQ_URI: URI for connection to RabbitMQ.
# When TLS is used for conection the protocol is amqps
# and client certificates are required (RABBITMQ_CERT_PATH,
# RABBITMQ_KEY_PATH, RABBITMQ_CA_PATH)
#
# format value: amqp://user:pass@host:port/vhost
# default value: amqp://guest:guest@localhost:5672
RABBITMQ_URI=amqp://guest:guest@localhost:5672

# RABBITMQ_CERT_PATH: RabbitMQ Certificate
# default value: .certs/rabbitmq/cert.pem
RABBITMQ_CERT_PATH=.certs/rabbitmq/cert.pem

# RABBITMQ_KEY_PATH: RabbitMQ Key
# default value: .certs/rabbitmq/key.pem
RABBITMQ_KEY_PATH=.certs/rabbitmq/key.pem

# MONGODB_URI_TEST: Database connection URI used if the application is running in test
# environment (NODE_ENV=test)
# default value: mongodb://127.0.0.1:27017/notification-service-test
MONGODB_URI_TEST=mongodb://127.0.0.1:27017/notification-service-test
# RABBITMQ_CA_PATH: RabbitMQ Certificate of the Authentication entity (CA)
# default value: .certs/rabbitmq/ca.pem
RABBITMQ_CA_PATH=.certs/rabbitmq/ca.pem
11 changes: 11 additions & 0 deletions .mocharc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
extension:
- ts
exit: true # could be expressed as "no-exit: true"
recursive: true
reporter: spec
require:
- 'test/helpers.ts'
- 'ts-node/register'
- 'reflect-metadata/Reflect'
slow: 8000
timeout: 30000 # same as "no-timeout: true" or "timeout: 0"
26 changes: 26 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
language: node_js
node_js:
- "12"
- "13"
env:
- NODE_ENV=test RABBITMQ_URI=amqp://guest:guest@localhost:5672/haniot MONGODB_ENABLE_TLS=false MONGODB_URI_TEST=mongodb://localhost:27017/notification-test
addons:
apt:
packages:
- rabbitmq-server
services:
- rabbitmq
- mongodb
sudo: false
before_script:
- sudo rabbitmqctl add_vhost haniot
- sudo rabbitmqctl set_permissions -p haniot guest ".*" ".*" ".*"
- npm install && npm install coveralls mocha-lcov-reporter --save-dev
script:
- npm test
after_success:
- nyc report --reporter=text-lcov | coveralls
notifications:
email:
on_success: never
on_failure: always
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
FROM node:12.13.1
FROM node:12-alpine
RUN apk --no-cache add bash curl grep

# Create app directory
RUN mkdir -p /usr/src/ns
Expand Down
46 changes: 26 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ Service for sending messages: **email**, **sms** or **push**. In this version on
See the [documentation](https://github.com/haniot/notification/wiki) for more information.

## Prerequisites
- [Node 8.0.0+](https://nodejs.org/en/download/)
- [MongoDB Server 3.0.0+](https://www.mongodb.com/download-center/community)
- [Node 12.0.0+](https://nodejs.org/en/download/)
- [MongoDB Server 4.0.0+](https://www.mongodb.com/download-center/community)
- [RabbitMQ 3.7.0+](https://www.rabbitmq.com/download.html)

---

## Set the environment variables
Application settings are defined by environment variables.. To define the settings, make a copy of the `.env.example` file, naming for `.env`. After that, open and edit the settings as needed. The following environments variables are available:
Application settings are defined by environment variables. To define the settings, make a copy of the `.env.example` file, naming for `.env`. After that, open and edit the settings as needed. The following environments variables are available:

| VARIABLE | DESCRIPTION | DEFAULT |
|-----|-----|-----|
Expand All @@ -27,14 +27,19 @@ Application settings are defined by environment variables.. To define the settin
| `PORT_HTTPS` | Port used to listen for HTTPS requests. Do not forget to provide the private key and the SSL/TLS certificate. See the topic [generate certificates](#generate-certificates). | `7001` |
| `SSL_KEY_PATH` | SSL/TLS certificate private key. | `.certs/server.key` |
| `SSL_CERT_PATH` | SSL/TLS certificate. | `.certs/server.crt` |
| `RABBITMQ_URI` | URI containing the parameters for connection to the message channel RabbitMQ. The [URI specifications ](https://www.rabbitmq.com/uri-spec.html) defined by RabbitMQ are accepted. For example: `amqp://user:pass@host:port/vhost`. | `amqp://guest:guest`<br/>`@127.0.0.1:5672` |
| `RABBITMQ_CA_PATH` | RabbitMQ SSL certificate path. | `.certs/ca.crt` |
| `MONGODB_URI` | Database connection URI used if the application is running in development or production environment. The [URI specifications ](https://docs.mongodb.com/manual/reference/connection-string) defined by MongoDB are accepted. For example: `mongodb://user:pass@host:port/database?options`. | `mongodb://127.0.0.1:27017`<br/>`/notification-service` |
| `MONGODB_URI_TEST` | Database connection URI used if the application is running in test environment. The [URI specifications ](https://docs.mongodb.com/manual/reference/connection-string) defined by MongoDB are accepted. For example: `mongodb://user:pass@host:port/database?options`. | `mongodb://127.0.0.1:27017`<br/>`/notification-service-test` |
| `SMTP_HOST` | SMTP protocol host for sending emails. | `YOUR_SMTP_HOST` |
| `SMTP_PORT` | SMTP port for sending emails. | `587` |
| `SMTP_USER` | User/email who will authenticate to the SMTP host. | `YOUR_SMTP_USER` |
| `SMTP_PASS` | Password who will authenticate to the SMTP host. | `YOUR_SMTP_PASS` |
| `MONGODB_URI` | Database connection URI used if the application is running in development or production environment. The [URI specifications ](https://docs.mongodb.com/manual/reference/connection-string) defined by MongoDB are accepted. For example: `mongodb://user:pass@host:port/database?options`. | `mongodb://127.0.0.1:27017`<br/>`/mhealth-service` |
| `MONGODB_URI_TEST` | Database connection URI used if the application is running in test environment. The [URI specifications ](https://docs.mongodb.com/manual/reference/connection-string) defined by MongoDB are accepted. For example: `mongodb://user:pass@host:port/database?options`. | `mongodb://127.0.0.1:27017`<br/>`/mhealth-service-test` |
| `MONGODB_ENABLE_TLS` | Enables/Disables connection to TLS. When TLS is used for connection, client certificates are required (`MONGODB_KEY_PATH`, `MONGODB_CA_PATH`). | `false` |
| `MONGODB_KEY_PATH` | Client certificate and key in .pem format to connect to MongoDB | `.certs/mongodb/client.pem` |
| `MONGODB_CA_PATH` | MongoDB Certificate of the Authentication entity (CA) | `.certs/mongodb/ca.pem` |
| `RABBITMQ_URI` | URI for connection to RabbitMQ. The [URI specifications ](https://www.rabbitmq.com/uri-spec.html). For example: `amqp://user:pass@host:port/vhost`. When TLS is used for conection the protocol is amqps and client certificates are required (`RABBITMQ_CERT_PATH`, `RABBITMQ_KEY_PATH`, `RABBITMQ_CA_PATH`) | `amqp://guest:guest`<br/>`@127.0.0.1:5672` |
| `RABBITMQ_CERT_PATH` | RabbitMQ Certificate | `.certs/rabbitmq/cert.pem` |
| `RABBITMQ_KEY_PATH` | RabbitMQ Key | `.certs/rabbitmq/key.pem` |
| `RABBITMQ_CA_PATH` | RabbitMQ Certificate of the Authentication entity (CA). | `.certs/rabbitmq/ca.pem` |

## Generate Certificates
For development and testing environments the easiest and fastest way is to generate your own self-signed certificates. These certificates can be used to encrypt data as well as certificates signed by a CA, but users will receive a warning that the certificate is not trusted for their computer or browser. Therefore, self-signed certificates should only be used in non-production environments, that is, development and testing environments. To do this, run the `create-self-signed-certs.sh` script in the root of the repository.
Expand Down Expand Up @@ -111,25 +116,26 @@ This command will download the latest image and create a container with the defa
You can also create the container by passing the settings that are desired by the environment variables. The supported settings are the same as those defined in ["Set the environment variables"](#set-the-environment-variables). See the following example:
```sh
docker run --rm \
-e PORT_HTTP=8080 \
-e PORT_HTTPS=8081 \
-e SSL_KEY_PATH=.certs/server.key \
-e SSL_CERT_PATH=.certs/server.crt \
-e RABBITMQ_URI="amqp://guest:guest@192.168.0.1:5672" \
-e MONGODB_URI="mongodb://192.168.0.2:27017/notification-service" \
-e PORT_HTTP=7080 \
-e PORT_HTTPS=7081 \
-v $(pwd)/.certs:/etc \
-e SSL_KEY_PATH=/etc/server.key \
-e SSL_CERT_PATH=/etc/server.crt \
-e MONGODB_ENABLE_TLS=false \
-e MONGODB_URI="mongodb://HOSTNAME:27017/haniot-ds-agent" \
-e RABBITMQ_URI="amqp://guest:guest@HOSTNAME:5672" \
-e SMTP_HOST="SMTP.office365.com" \
-e SMTP_PORT=587 \
-e SMTP_USER="YOUR_SMTP_USER@outlook.com" \
-e SMTP_PASS="YOUR_SMTP_PASS" \
-e SMTP_PASS="YOUR_SMTP_PASS" \
--name haniot-notification \
haniot/notification-service
```
If the MongoDB or RabbitMQ instance is in the host local, add the `--net=host` statement when creating the container, this will cause the docker container to communicate with its local host.
```sh
docker run --rm \
--net=host \
-e RABBITMQ_URI="amqp://guest:guest@localhost:5672" \
-e MONGODB_URI="mongodb://localhost:27017/notification-service" \
haniot/notification-service
...
```
To generate your own docker image, run the following command:
```sh
Expand All @@ -139,10 +145,10 @@ docker build -t image_name:tag .
[//]: # (These are reference links used in the body of this note.)
[license-image]: https://img.shields.io/badge/license-Apache%202-blue.svg
[license-url]: https://github.com/haniot/notification/blob/master/LICENSE
[node-image]: https://img.shields.io/badge/node-%3E%3D%208.0.0-brightgreen.svg
[node-image]: https://img.shields.io/badge/node-%3E%3D%2012.0.0-brightgreen.svg
[node-url]: https://nodejs.org
[travis-image]: https://travis-ci.org/haniot/notification.svg?branch=master
[travis-url]: https://travis-ci.org/haniot/notification
[travis-image]: https://travis-ci.com/haniot/notification.svg?branch=master
[travis-url]: https://travis-ci.com/haniot/notification
[coverage-image]: https://coveralls.io/repos/github/haniot/notification/badge.svg
[coverage-url]: https://coveralls.io/github/haniot/notification?branch=master
[known-vulnerabilities-image]: https://snyk.io/test/github/haniot/notification/badge.svg
Expand Down
Loading

0 comments on commit cc70ff0

Please sign in to comment.