Skip to content

Commit db30679

Browse files
committed
Initial commit
0 parents  commit db30679

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+6950
-0
lines changed

Diff for: .dockerignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
node_modules
2+
Dockerfile
3+
.dockerignore
4+
.git
5+
.gitignore
6+
docker-compose-*

Diff for: .env.example

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
NODE_ENV=development
2+
JWT_SECRET=ThisIsARandomlyGeneratedLongSecret
3+
APP_PORT=3000
4+
5+
# Replace these with your DB Config
6+
DB_CLIENT=postgres
7+
DB_HOST=127.0.0.1
8+
DB_DATABASE=express_ts
9+
DB_PORT=5432
10+
DB_USERNAME=tanmay
11+
DB_PASSWORD=
12+
13+
MONGO_DSN=
14+
15+
# Make sure you have redis installed
16+
REDIS_HOST=127.0.0.1
17+
REDIS_PORT=6379
18+
REDIS_PASSWORD=null
19+
20+
MAIL_HOST=
21+
MAIL_PORT=
22+
MAIL_USERNAME=
23+
MAIL_PASSWORD=
24+
MAIL_ENCRYPTION=TLS
25+
MAIL_FROM_ADDRESS=
26+
MAIL_FROM_NAME=
27+
28+
# The email that will be used as cert owner. Required for SSL.
29+
SSL_EMAIL=
30+
# No space after comma. Required for SSL.
31+
SSL_DOMAINS="staging.example.com,*.staging.example.com"
32+
# 1 Fetches dummy certificate, 0 fetches real. Required for SSL.
33+
SSL_STAGING=1

Diff for: .env.test

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
NODE_ENV=test
2+
JWT_SECRET=ThisIsARandomlyGeneratedLongSecret
3+
APP_PORT=3000
4+
5+
# Replace these with your DB Config
6+
DB_CLIENT=postgres
7+
DB_HOST=127.0.0.1
8+
DB_DATABASE=express_ts_test
9+
DB_PORT=5432
10+
DB_USERNAME=tanmay
11+
DB_PASSWORD=
12+
13+
MONGO_DSN=
14+
15+
# Make sure you have redis installed
16+
REDIS_HOST=127.0.0.1
17+
REDIS_PORT=6379
18+
REDIS_PASSWORD=null
19+
20+
MAIL_HOST=
21+
MAIL_PORT=
22+
MAIL_USERNAME=
23+
MAIL_PASSWORD=
24+
MAIL_ENCRYPTION=TLS
25+
MAIL_FROM_ADDRESS=
26+
MAIL_FROM_NAME=

Diff for: .github/dependabot.yml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# To get started with Dependabot version updates, you'll need to specify which
2+
# package ecosystems to update and where the package manifests are located.
3+
# Please see the documentation for all configuration options:
4+
# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
5+
6+
version: 2
7+
updates:
8+
- package-ecosystem: "npm" # See documentation for possible values
9+
directory: "/" # Location of package manifests
10+
schedule:
11+
interval: "daily"

Diff for: .gitignore

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
.env
2+
/node_modules
3+
npm-debug.log
4+
yarn-error.log
5+
/dist
6+
/logs
7+
.DS_Store
8+
9+
/data/dumps/*
10+
/data/mysql/*
11+
/data/redis/*
12+
/data/certbot/conf/*
13+
/data/certbot/dhparam/*
14+
/data/certbot/logs/*
15+
/src/database/database.sqlite
16+
17+
!/data/dumps/.gitkeep
18+
!/data/mysql/.gitkeep
19+
!/data/redis/.gitkeep
20+
!/data/certbot/conf/.gitkeep
21+
!/data/certbot/dhparam/.gitkeep
22+
!/data/certbot/logs/.gitkeep

Diff for: .prettierrc

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"semi": true,
3+
"trailingComma": "none",
4+
"singleQuote": true,
5+
"printWidth": 80
6+
}

Diff for: Dockerfile

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM node:lts-alpine
2+
RUN mkdir /app && chown -R node:node /app
3+
WORKDIR /app
4+
COPY package.json .
5+
USER node
6+
RUN yarn install
7+
RUN yarn install --only=dev
8+
COPY --chown=node:node . .
9+
CMD ["yarn", "start"]

Diff for: LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Tanmay Mishu
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Diff for: README.md

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<p align="center">
2+
<img src="https://miro.medium.com/max/1400/1*xu6sBN2e6ExZS68FS83hgQ.png">
3+
<h3 align="center">Express + TypeScript</h3>
4+
<p align="center">[A Starter Template]</p>
5+
</p>
6+
7+
### 🔑 Key Features:
8+
9+
- Good ol' [Express](https://expressjs.com/).
10+
- Type-safety ([TypeScript](https://www.typescriptlang.org/)).
11+
- ORM with schema migration ([TypeORM](https://typeorm.io/)).
12+
- Session-based and Token-based Authentication ([Passport](http://www.passportjs.org/)).
13+
- Logging ([Winston](https://github.com/winstonjs/winston) + [Morgan](https://github.com/expressjs/morgan)).
14+
- Testing ([Supertest](https://github.com/visionmedia/supertest) + [Jest](https://jestjs.io/)).
15+
- Mailing ([Nodemailer](https://nodemailer.com/)).
16+
- Request validation ([Express Validator](https://express-validator.github.io/)).
17+
- Background job processing ([Bull](https://docs.bullmq.io/)).
18+
- Queue monitoring dashboard ([Bull Board](https://github.com/felixmosh/bull-board)).
19+
20+
### 🏁 Getting Started:
21+
22+
- [Generate a project](https://github.com/tanmaymishu/nodejs-starter-ts/generate) based on this template.
23+
- Create two postgres or mysql databases: `express_ts` and `express_ts_test`
24+
- Clone the newly created repo and `cd` into the directory.
25+
- Change the DB credentials accordingly in the `.env.example` and `.env.test` files. In the next step, a `.env` file will be generated based on these changes.
26+
- Run `npm run install` or, `yarn install` if you're using yarn.
27+
- Run `npm run dev` or, `yarn dev`.
28+
29+
### 🤖 Commands:
30+
31+
- `npm run build`: Compile all .ts source files and output the compiled files into /dist directory.
32+
- `npm run dev`: Run the project in development mode. Changes will be picked up and server will be restarted automatically.
33+
- `npm run test`: Run all the unit and integration tests located in /tests directory.
34+
- `npm run start`: Compile the .ts source files and run the project by invoking the compiled source files.
35+
- `npm run fmt`: Format the source code using the rules defined in .prettierrc.
36+
- `npm run entity:make`: Create a migration file. Ex: `npm run entity:make Post`. An entity file will be created inside the /src/database/sql/entities directory.
37+
- `npm run migrate:make`: Create a migration file. Ex: `npm run migrate:make CreatePostsTable`. A migration file with current timestamp will be created inside the /src/database/sql/migrations directory.
38+
- `npm run migrate`: Migrate the database schema. This will apply all newly created migrations.
39+
- `npm run migrate:rollback`: Undo the last run migration.
40+
- To run more migration or seed-specific commands, consider installing the knex cli globally. [Learn more](https://knexjs.org/#Migrations-CLI).
41+
42+
### 🐳 Docker:
43+
44+
The project ships with some docker configs and a `cli`. You can interact with the docker containers using this cli. When the `NODE_ENV` variable is set to `production`, the cli will use the `docker-compose.yml` and `docker-compose.prod.yml` file, otherwise it will use the `docker-compose.yml` and `docker-compose.dev.yml` file.
45+
46+
- `./cli start` to launch all the containers in detached mode. `./cli stop` will stop and remove all the containers.
47+
- `./cli yarn` will invoke the `yarn` command from within the `app` container. For example, to compile the source code, you could run `./cli yarn build`.
48+
- When running in production, you might want to use a free SSL from LetsEncrypt. `./cli ssl` will attempt to issue a certificate for the domains you've specified in your `.env` file. The `SSL_STAGING=1` generates a dummy certificate, and `SSL_STAGING=0` generates a real certificate and you will face an ACME challenge. Make sure your DNS record panel is open in another tab before you proceed with this operation. If the ACME challenge fails, try again with `./cli ssl`.
49+
- If the certificate has been generated successfully, you will need to run `./cli post-ssl` to fetch the dhparam key file.
50+
- Adjust the production specific nginx configs in the `nginx.prod.conf` file.
51+
52+
Do you have a significant feature in mind? [Create an issue](https://github.com/tanmaymishu/nodejs-starter-ts/issues/new) or [send a pull request](https://github.com/tanmaymishu/nodejs-starter-ts/pulls).
53+
54+
### 📝 Notes:
55+
56+
- Although this template comes with many components pre-installed, it does not impose any hard-bound rule on the project structure. Feel free to customize the directory structure and add or remove the components as you see fit for your project. Removing a module will be easier since we're using TypeScript. Any good text editor/IDE will help you track down a non-existing reference in the codebase.
57+
- Kebab-case is used for the file names since it is the most compatible across all operating systems. If you don't like this convention, feel free to change.

Diff for: cli

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/usr/bin/env bash
2+
3+
# Import the .env file
4+
if [ -f .env ]
5+
then
6+
export $(cat .env | sed 's/#.*//g' | xargs)
7+
fi
8+
9+
if [ "$NODE_ENV" = "production" ]; then
10+
COMPOSE="docker-compose -f docker-compose.yml -f docker-compose.prod.yml"
11+
else
12+
COMPOSE="docker-compose -f docker-compose.yml -f docker-compose.dev.yml"
13+
fi
14+
15+
# If we pass any arguments...
16+
if [ $# -gt 0 ];then
17+
18+
# start the app
19+
if [ "$1" == "start" ]; then
20+
$COMPOSE up -d
21+
22+
# stop the app
23+
elif [ "$1" == "stop" ]; then
24+
$COMPOSE down
25+
26+
# If "yarn" is used, pass-thru to "yarn"
27+
# inside a new container
28+
elif [ "$1" == "yarn" ]; then
29+
shift 1
30+
$COMPOSE run --rm \
31+
-w /app \
32+
app \
33+
yarn "$@"
34+
35+
# If "test" is used, run unit tests,
36+
# pass-thru any extra arguments to test
37+
elif [ "$1" == "test" ]; then
38+
shift 1
39+
$COMPOSE run --rm \
40+
-w /app \
41+
app \
42+
yarn test "$@"
43+
44+
45+
# If "db" is used, run mariadb
46+
# from our mariadb container
47+
elif [ "$1" == "db-backup" ]; then
48+
shift 1
49+
$COMPOSE exec app-mariadb mysqldump -u ${DB_USERNAME} -p${DB_PASSWORD} "$2" > ./data/dumps/dump-$(date +"%d-%m-%Y_%s").sql
50+
51+
elif [ "$1" == "ssl" ]; then
52+
shift 1
53+
/bin/bash ./init-letsencrypt.sh
54+
55+
# Add dhparam after installing ssl
56+
elif [ "$1" == "post-ssl" ]; then
57+
shift 1
58+
curl https://ssl-config.mozilla.org/ffdhe2048.txt > data/certbot/dhparam/dhparam-2048.pem
59+
$COMPOSE exec nginx nginx -s reload
60+
61+
# Else, pass-thru args to docker-compose
62+
else
63+
$COMPOSE "$@"
64+
fi
65+
66+
else
67+
$COMPOSE ps
68+
fi

Diff for: data/certbot/conf/.gitkeep

Whitespace-only changes.

Diff for: data/certbot/dhparam/.gitkeep

Whitespace-only changes.

Diff for: data/certbot/logs/.gitkeep

Whitespace-only changes.

Diff for: data/mysql/.gitkeep

Whitespace-only changes.

Diff for: data/redis/.gitkeep

Whitespace-only changes.

Diff for: docker-compose.dev.yml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
version: "3"
2+
services:
3+
nginx:
4+
image: nginx:stable-alpine
5+
container_name: nginx
6+
ports:
7+
- "${APP_PORT}:80"
8+
volumes:
9+
- ./nginx.conf:/etc/nginx/conf.d/default.conf:ro
10+
networks:
11+
- app-network
12+
depends_on:
13+
- app
14+
15+
app:
16+
volumes:
17+
- ./:/app
18+
- /app/node_modules
19+
environment:
20+
- NODE_ENV=development
21+
command: yarn dev

Diff for: docker-compose.prod.yml

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
version: "3"
2+
services:
3+
nginx:
4+
image: nginx:stable-alpine
5+
container_name: nginx
6+
ports:
7+
- "80:80"
8+
- "443:443"
9+
volumes:
10+
- ./nginx.prod.conf:/etc/nginx/conf.d/default.conf
11+
- ./data/certbot/dhparam:/etc/nginx/dhparam
12+
- ./data/certbot/conf:/etc/letsencrypt
13+
- ./data/certbot/logs:/var/log/letsencrypt
14+
networks:
15+
- app-network
16+
depends_on:
17+
- app
18+
19+
certbot:
20+
image: certbot/certbot:latest
21+
container_name: app-certbot
22+
volumes:
23+
- ./data/certbot/conf:/etc/letsencrypt
24+
- ./data/certbot/logs:/var/log/letsencrypt
25+
entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'"
26+
networks:
27+
- app-network
28+
29+
app:
30+
volumes:
31+
- /app/node_modules
32+
environment:
33+
- NODE_ENV=production
34+
command: yarn start
35+

Diff for: docker-compose.yml

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
version: "3"
2+
services:
3+
redis:
4+
image: redis:alpine
5+
container_name: app-redis
6+
networks:
7+
- app-network
8+
volumes:
9+
- ./data/redis:/data
10+
ports:
11+
- ${REDIS_PORT}:6379
12+
restart: unless-stopped
13+
14+
mariadb:
15+
image: mariadb:10.3
16+
container_name: app-mariadb
17+
volumes:
18+
- ./data/mysql:/var/lib/mysql
19+
environment:
20+
- MYSQL_ROOT_PASSWORD=${DB_PASSWORD}
21+
- MYSQL_DATABASE=${DB_DATABASE}
22+
- MYSQL_USER=${DB_USERNAME}
23+
- MYSQL_PASSWORD=${DB_PASSWORD}
24+
ports:
25+
- ${DB_PORT}:3306
26+
networks:
27+
- app-network
28+
restart: unless-stopped
29+
30+
app:
31+
build: .
32+
container_name: app
33+
depends_on:
34+
- redis
35+
- mariadb
36+
networks:
37+
- app-network
38+
command: yarn dev
39+
40+
networks:
41+
app-network:
42+
driver: bridge
43+

0 commit comments

Comments
 (0)