Skip to content

Commit

Permalink
Merge pull request #3 from aquariophilie/feature/initial-release-of-t…
Browse files Browse the repository at this point in the history
…he-boilerplate#1

Feature/initial release of the boilerplate#1
  • Loading branch information
gmacario committed Feb 22, 2022
2 parents b222aad + 718e314 commit 2c74f94
Show file tree
Hide file tree
Showing 42 changed files with 10,295 additions and 1,529 deletions.
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
npm-debug.log
.git
.gitignore
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*.ts]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
25 changes: 25 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Node environment
NODE_ENV=development

# JSON web token (JWT) secret: this keeps our apps user authentication secure
# JWT configurations, no secret used
JWT_ALGO=ES256
JWT_EXP=5m
JWT_PRIVATE=./private.pem
JWT_PUBLIC=./public.pem
JWT_REFRESH_EXP=30d

# Mongo DB
# Local development
MONGODB_NAME=
MONGODB_PASS=
MONGODB_URI=
MONGODB_USER=

# Port
PORT=4000

# Debug
LOG_LEVEL=debug

# EOF
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
95 changes: 95 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
{
"ignorePatterns": [
"**/*.json"
],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"airbnb"
],
"parser": "@typescript-eslint/parser",
"plugins": [
"@typescript-eslint"
],
"settings": {
"react": {
"version": "latest"
}
},
"rules": {
"indent": [
"error",
4
],
"import/no-unresolved": "off",
"@typescript-eslint/indent": [
"error",
4
],
"quotes": [
2,
"single",
{
"avoidEscape": true
}
],
"@typescript-eslint/no-unused-vars": "error",
"@typescript-eslint/no-explicit-any": "off",
"max-len": [
2,
160,
4,
{
"ignoreUrls": true
}
],
"lines-between-class-members": "off",
"prefer-object-spread": "off",
"padded-blocks": "off",
"import/extensions": "off",
"import/prefer-default-export": "off",
"object-curly-newline": [
"error",
{
"ObjectExpression": {
"multiline": true,
"minProperties": 2
},
"ObjectPattern": {
"multiline": true
},
"ImportDeclaration": "never",
"ExportDeclaration": {
"multiline": true,
"minProperties": 3
}
}
],
"no-useless-constructor": "off",
"no-empty-function": [
"error",
{
"allow": [
"functions",
"constructors"
]
}
],
"class-methods-use-this": "off",
"no-underscore-dangle": [
"error",
{
"allow": [
"_id"
]
}
],
"no-param-reassign": [
"error",
{
"props": false
}
],
"@typescript-eslint/no-var-requires": "off"
}
}
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,14 @@ dist

# TernJS port file
.tern-port

# Environment varibales
.env*
!.env*.example

# Keys
public.pem
private.pem

# This file is automatically built
swagger.json
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
engine-strict=true
19 changes: 19 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM node:16

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./

RUN npm install
# If you are building your code for production
# RUN npm ci --only=production

# Bundle app source
COPY . .

EXPOSE 4000
CMD [ "npm" , "run", "start:prod" ]
61 changes: 61 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,66 @@
# node-koa-boilerplate

A boilerplate for a simple API in [Node.js](https://nodejs.org/) using the [Koa.js framework](https://koajs.com/) with [TypeScript](https://www.typescriptlang.org/) and dependency injection using [InversifyJS](https://inversify.io/).

## Prerequisites

* [Node.js](https://nodejs.org/) v14.x or later
* Connection details (URI, user, password and db name) to a running instance of [MongoDB](https://www.mongodb.com/)

## Installation

Use the package manager [npm](https://www.npmjs.com/) to install the dependencies.

```bash
npm install
```

For the authentication in this example we are using a [JWT](https://jwt.io/) token with "ES256" algorithm and async keys.
For generating private and public keys for authentication use [OpenSSL](https://www.openssl.org/).
We are also using a short-lived token and a long-lived refresh token.

```bash
openssl ecparam -genkey -name prime256v1 -noout -out private.pem
openssl ec -in private.pem -pubout -out public.pem
```

## Usage

### Runtime customization

Before running the code you need to customize its runtime enviroment.
Create an `.env` file starting from the provided `.env.example`, then customize it as described in the comments
(for instance you need the supply the database configuration).

### Initialize (or migrate) database

```bash
# View database migrations status
npm run migrate:status
# Run database migrations
npm run migrate:up
```

### Start in development mode

```bash
# Start in development mode with nodemon
npm start
```

### Build for production

Make sure your `.env` file has the line `NODE_ENV=development` commented out, then run

```bash
# Build
npm run build
```

### API documentation

Documentation built with [Swagger](https://swagger.io/) is available under "api/swagger-html" (only in development environment).

## Copyright and license

Copyright (C) 2022 by the [Aquariophilie team](https://github.com/aquariophilie), all rights reserved.
Expand Down
42 changes: 42 additions & 0 deletions migrate-mongo-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// In this file you can configure migrate-mongo
require('dotenv').config();

// Set the NODE_ENV to 'development' by default
process.env.NODE_ENV = process.env.NODE_ENV || 'development';

const config = {
mongodb: {
// TODO Change (or review) the url to your MongoDB:
url: process.env.MONGODB_URI || 'mongodb://localhost:27017',

// TODO Change this to your database name:
databaseName: process.env.MONGODB_NAME || '',

options: {
useNewUrlParser: true, // removes a deprecation warning when connecting
useUnifiedTopology: true, // removes a deprecating warning when connecting
// connectTimeoutMS: 3600000, // increase connection timeout to 1 hour
// socketTimeoutMS: 3600000, // increase socket timeout to 1 hour
auth: {
username: process.env.MONGODB_USER || '',
password: process.env.MONGODB_PASS || '',
},
},
},

// The migrations dir, can be an relative or absolute path. Only edit this when really necessary.
migrationsDir: 'migrations',

// The mongodb collection where the applied changes are stored. Only edit this when really necessary.
changelogCollectionName: 'changelog',

// The file extension to create migrations and search for in migration dir
migrationFileExtension: '.js',

// Enable the algorithm to create a checksum of the file contents and use that in the comparison to determin
// if the file should be run. Requires that scripts are coded to be run multiple times.
useFileHash: false,
};

// Return the config as a promise
module.exports = config;
18 changes: 18 additions & 0 deletions migrations/20211027102722-base_schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = {
async up(db) {
// TODO write your migration here.
await db.createCollection('users');
await db.collection('users').createIndex({ email: 1 });
await db.collection('users').createIndex({ role: 1 });

await db.createCollection('refreshTokens');
await db.collection('refreshTokens').createIndex({ userId: 1 });
await db.collection('refreshTokens').createIndex({ expires: 1 });

},

async down(db) {
await db.dropCollection('users');
await db.dropCollection('refreshTokens');
},
};
10 changes: 10 additions & 0 deletions nodemon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"watch": [
"src"
],
"ext": "ts,json",
"ignore": [
"src/**/*.spec.ts"
],
"exec": "tsc && node dist/app.js"
}
Loading

0 comments on commit 2c74f94

Please sign in to comment.