Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ If you want to just run the application in development mode, use the following c

```sh
$ yarn dev

```

To run the application in debug mode in a way that the execution will stop when a debugger statement is called, use:
Expand Down Expand Up @@ -50,6 +51,68 @@ The boilerplate is prepared to run tests using Jest. We usually group the tests
$ yarn test
```

## Docker wrapper

In case you want to use Docker to run it, this project includes a [docker wrapper](https://github.com/brunohcastro/node-base) for development. Any command can be executed by calling the scripts under the `dbin/` folder.

```sh
$ dbin/yarn dev

$ dbin/yarn debug

$ dbin/yarn cli

$ dbin/yarn remote [server address] [REPL port]

$ dbin/yarn test
```

The container runs using host networking, so there's no need to map ports. Keep in mind that environment variables should be added to the docker-compose.yml.

### Wrapper commands

```sh
# Runs the command inside an ephemeral container using the app service described in the docker-compose file as a base (use the --root flag if the command should be run as root)
$ dbin/run [--root] <command>

# Rebuild the image after any changes to the dockerfile
$ dbin/build

# Remove all containers and their volumes (WARNING any cache or files not stored in the filesystem will be deleted)
$ dbin/dispose

# Appends a RUN command to the base image, useful to install new packages
$ dbin/chimg <command>
```

### Wrapper Aliases

```sh
# Creates a new <name> file in dbin to alias the <command> inside the docker container (use the --root flag if the command should be run as root)
$ dbin/mkalias [--root] <name> <command>

# Opens a new terminal in the project folder (use the --root flag if the shell should assume the root user)
$ dbin/shell [--root]

# Runs npm in the project folder
$ dbin/npm

# Runs npx in the project folder
$ dbin/npx

# Runs yarn in the project folder
$ dbin/yarn
```

### Wrapper Helpers

```bash
# Adds dbin folder to the PATH only for the current terminal session.
$ source dbin/local-env

# After using this command you can use the any script inside the dbin folder without the dbin/ prefix
```

## Dependency injection

We use [Awilix](https://www.npmjs.com/package/awilix) to implement dependency injection and decouple the parts of your application. The boilerplate was developed in a way that each [module](#modules) is able to consume and augment the registered dependencies separately. Click here to know more about [inversion of control and dependency injection](https://www.martinfowler.com/articles/injection.html). The creator of Awilix also has a very good series of posts about the design decisions of the library that you can read [clicking here](https://medium.com/@Jeffijoe/dependency-injection-in-node-js-2016-edition-f2a88efdd427).
Expand Down
3 changes: 3 additions & 0 deletions dbin/build
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

USER_ID=$(id -u) GROUP_ID=$(id -g) docker-compose build dev
13 changes: 13 additions & 0 deletions dbin/chimg
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

PARENT_DIR="$(cd "$(dirname "$0")" && cd .. && pwd)"

cp "$PARENT_DIR/docker/Dockerfile.dev" "$PARENT_DIR/docker/Dockerfile.dev.bkp"

echo -e "\nRUN $*" >> "$PARENT_DIR/docker/Dockerfile.dev"

if USER_ID=$(id -u) GROUP_ID=$(id -g) docker-compose build dev; then
rm -f "$PARENT_DIR/docker/Dockerfile.dev.bkp"
else
mv "$PARENT_DIR/docker/Dockerfile.dev.bkp" "$PARENT_DIR/docker/Dockerfile.dev"
fi
3 changes: 3 additions & 0 deletions dbin/dispose
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

docker-compose down -v
5 changes: 5 additions & 0 deletions dbin/local-env
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

DIR="$(cd "$(dirname "$0")" && pwd)"

export PATH="$PATH:$DIR"
23 changes: 23 additions & 0 deletions dbin/mkalias
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash

DIR="$(cd "$(dirname "$0")" && pwd)"

if [[ $1 == --root ]] ; then
runner="\$DIR/run --root"
name=$2
shift 2
else
runner="\$DIR/run"
name=$1
shift 1
fi

cat >"$DIR/$name" <<EOL
#!/bin/bash

DIR="\$(cd "\$(dirname "\$0")" && pwd)"

. "$runner" $@ "\$@"
EOL

chmod +x "$DIR/$name"
11 changes: 11 additions & 0 deletions dbin/mvroot
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

PARENT_DIR="$(cd "$(dirname "$0")" && cd .. && pwd)"

TARGET_DIR="$1"

mv "$PARENT_DIR/README.md" "$PARENT_DIR/INSTRUCTIONS.md" 2>/dev/null

(shopt -s dotglob; mv "$PARENT_DIR/$TARGET_DIR"/* "$PARENT_DIR")

rm -r "${PARENT_DIR:?}/$TARGET_DIR"
5 changes: 5 additions & 0 deletions dbin/npm
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

DIR="$(cd "$(dirname "$0")" && pwd)"

. "$DIR/run" npm "$@"
5 changes: 5 additions & 0 deletions dbin/npx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

DIR="$(cd "$(dirname "$0")" && pwd)"

. "$DIR/run" npx "$@"
14 changes: 14 additions & 0 deletions dbin/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

user=""

if [[ $1 == --root ]] ; then
user="--user root"
shift 1
fi

if docker-compose ps -a | grep -E -i -q 'app(\s*)running'; then
USER_ID=$(id -u) GROUP_ID=$(id -g) docker-compose --profile dev exec $user dev "$@"
else
USER_ID=$(id -u) GROUP_ID=$(id -g) docker-compose --profile dev run --rm $user --service-ports dev "$@"
fi
10 changes: 10 additions & 0 deletions dbin/shell
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

DIR="$(cd "$(dirname "$0")" && pwd)"

if [[ $1 == --root ]] ; then
shift
. "$DIR/run" --root /bin/ash "$@"
else
. "$DIR/run" /bin/ash "$@"
fi
5 changes: 5 additions & 0 deletions dbin/yarn
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

DIR="$(cd "$(dirname "$0")" && pwd)"

. "$DIR/run" yarn "$@"
36 changes: 0 additions & 36 deletions docker-compose.yaml

This file was deleted.

47 changes: 47 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
version: '3.9'

services:
mongodb:
container_name: blog-mongodb
image: mongo:4.2
ports:
- 27017:27017
volumes:
- mongo_data:/data/db
environment:
MONGO_INITDB_ROOT_USERNAME: blog
MONGO_INITDB_ROOT_PASSWORD: blog

mongo-express:
container_name: blog-mongo-express
image: mongo-express
depends_on:
- mongodb
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_URL: mongodb://blog:blog@blog-mongodb:27017

dev:
container_name: blog-dev
build:
context: .
dockerfile: ./docker/Dockerfile.dev
args:
USER_ID: ${USER_ID:-1000}
GROUP_ID: ${GROUP_ID:-1000}
depends_on:
- mongodb
network_mode: host
environment:
- NODE_ENV=${NODE_ENV:-development}
volumes:
- .:/opt/node-app
- npm_cache:/home/node/.npm-packages
tty: true
profiles:
- dev

volumes:
npm_cache:
mongo_data:
30 changes: 30 additions & 0 deletions docker/Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
FROM node:16-alpine

ARG USER_ID=1000
ARG GROUP_ID=1000

RUN apk update \
&& apk add --no-cache \
ca-certificates \
curl \
tzdata \
git \
build-base \
linux-headers \
coreutils \
shadow \
sudo \
&& usermod -u ${USER_ID} node \
&& groupmod -g ${GROUP_ID} node \
&& mkdir /opt/node-app \
&& chown -R node:node /opt/node-app \
&& mkdir /home/node/.npm-packages \
&& chown -R node:node /home/node/.npm-packages \
&& npm config set -g prefix "/home/node/.npm-packages" \
&& echo "node ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers

ENV PATH="$PATH:/home/node/.npm-packages/bin"

USER node

WORKDIR /opt/node-app
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
},
"dependencies": {
"awilix": "^4.3.4",
"cors": "^2.8.5",
"dotenv": "^10.0.0",
"express": "^4.17.1",
"helmet": "^5.1.1",
"joi": "^17.4.1",
"lodash.template": "^4.5.0",
"mongodb": "^4.0.0",
Expand All @@ -29,6 +31,7 @@
"uuid-mongodb": "^2.4.4"
},
"devDependencies": {
"@types/cors": "^2.8.12",
"@types/express": "^4.17.13",
"@types/jest": "^26.0.24",
"@types/lodash.template": "^4.5.0",
Expand Down
6 changes: 2 additions & 4 deletions src/_boot/appModules.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { articleModule, ArticleRegistry } from '@/article';
import { ArticleMessages } from '@/article/messages';
import { commentModule, CommentRegistry } from '@/comment';

type AppModulesMessages = ArticleMessages;

// eslint-disable-next-line @typescript-eslint/ban-types
type AppModulesConfig = {};

const appModules = [articleModule, commentModule];

type AppModulesRegistry = ArticleRegistry & CommentRegistry;

export { appModules };
export type { AppModulesMessages, AppModulesConfig, AppModulesRegistry };
export type { AppModulesConfig, AppModulesRegistry };
Loading