Skip to content

Commit

Permalink
Merge pull request #11 from santoshshinde2012/feature/crypto
Browse files Browse the repository at this point in the history
Added encryption module & global environment object
  • Loading branch information
santoshshinde2012 authored Sep 10, 2021
2 parents 43402d6 + 50f804f commit 202dd0e
Show file tree
Hide file tree
Showing 27 changed files with 488 additions and 122 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
NODE_ENV=local
PORT=3146
BASE_URL=http://localhost:3146
BASE_URL=http://localhost:3146
8 changes: 6 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
PORT=3000
HOST=localhost
NODE_ENV=local
PORT=3146
BASE_URL=http://localhost:3146

APPLY_ENCRYPTION=true;
SECRET_KEY=sTJQgn5E8d8jMY15PhARwDrW4my6bLwE
6 changes: 5 additions & 1 deletion .env.local
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
NODE_ENV=local
PORT=3146
HOST=localhost
BASE_URL=http://localhost:3146

APPLY_ENCRYPTION=true
SECRET_KEY=sTJQgn5E8d8jMY15PhARwDrW4my6bLwE
4 changes: 4 additions & 0 deletions .env.test
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
NODE_ENV=test
PORT=3146
BASE_URL=http://localhost:3146

APPLY_ENCRYPTION=false
SECRET_KEY=sTJQgn5E8d8jMY15PhARwDrW4my6bLwE
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@
}
]
}
}
}
4 changes: 4 additions & 0 deletions .github/codeql/codeql-configuration.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name : CodeQL Configuration

paths:
- './src'
18 changes: 18 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"
- package-ecosystem: npm
directory: "/"
schedule:
interval: "weekly"
open-pull-requests-limit: 10
target-branch: "dev"
# Increase the version requirements
# only when required
versioning-strategy: increase-if-necessary
labels:
- "dependencies"
- "npm"
72 changes: 72 additions & 0 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# For most projects, this workflow file will not need changing; you simply need
# to commit it to your repository.
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.
#
# ******** NOTE ********
# We have attempted to detect the languages in your repository. Please check
# the `language` matrix defined below to confirm you have the correct set of
# supported CodeQL languages.
#
name: "CodeQL"

on:
push:
branches: [ main ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ main ]
schedule:
- cron: '17 10 * * 2'

jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write

strategy:
fail-fast: false
matrix:
language: [ 'javascript' ]
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ]
# Learn more:
# https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed

steps:
- name: Checkout repository
uses: actions/checkout@v2

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
with:
config-file: ./.github/codeql/codeql-configuration.yml
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.
# queries: ./path/to/local/query, your-org/your-repo/queries@main

# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v1

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl

# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
# and modify them (or add more) to build your code if your project
# uses a compiled language

#- run: |
# make bootstrap
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1
46 changes: 26 additions & 20 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
# This workflow will do a clean install of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Node.js CI

name: CI
on:
push:
branches: [ master ]
branches:
- main
- master
- release-*
pull_request:
branches: [ master ]
types: [opened, synchronize, reopened]
branches:
- main
- master
- release-*

jobs:
build:

runs-on: ubuntu-latest

CI:
strategy:
matrix:
node-version: [12.x, 14.x, 16.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

# node-version: [10.x, 12.x, 14.x]
node-version: [14.x]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
- name: Checkout Code
uses: actions/checkout@v2
- name: Set up nodejs version ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: npm run build --if-present
- name: Install package
run: npm install
- name: Linter
run: npm run lint
- name: Build
run: npm run build --if-present
- name: Test
run: npm run test
29 changes: 29 additions & 0 deletions .github/workflows/update-package-lock.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Update package-lock.json

on:
schedule:
# This is probably 6am UTC, which is 10pm PST or 11pm PDT
# Alternatively, 6am local is also fine
- cron: '0 6 * * *'
workflow_dispatch: {}

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 12
registry-url: https://registry.npmjs.org/

- name: Configure git and update package-lock.json
run: |
git config user.email "[email protected]"
git config user.name "Santosh Shinde"
npm install --package-lock-only
git add -f package-lock.json
if git commit -m "Update package-lock.json"; then
git push
fi
57 changes: 28 additions & 29 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
{
"bracketSpacing": true,
"printWidth": 80,
"proseWrap": "preserve",
"semi": true,
"singleQuote": true,
"trailingComma": "all",
"tabWidth": 4,
"useTabs": true,
"parser": "typescript",
"arrowParens": "always",
"requirePragma": true,
"insertPragma": true,
"endOfLine": "lf",
"overrides": [
{
"files": "*.json",
"options": {
"singleQuote": false
}
},
{
"files": ".*rc",
"options": {
"singleQuote": false,
"parser": "json"
}
"bracketSpacing": true,
"printWidth": 80,
"proseWrap": "preserve",
"semi": true,
"singleQuote": true,
"trailingComma": "all",
"tabWidth": 4,
"useTabs": true,
"parser": "typescript",
"arrowParens": "always",
"requirePragma": true,
"insertPragma": true,
"endOfLine": "lf",
"overrides": [
{
"files": "*.json",
"options": {
"singleQuote": false
}
]
}

},
{
"files": ".*rc",
"options": {
"singleQuote": false,
"parser": "json"
}
}
]
}
24 changes: 19 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Try it!! I am happy to hear your feedback or any kind of new features.
- Followed SOLID Principles
- Based on Typescript Syntax
- Simple Enviroment Configuration
- Global Enviroment Object
- Request/Response Encryption & Decryption Implementation
- Easily Add new feature
- Integrated winston Logger
- Production Ready Skeleton
Expand Down Expand Up @@ -70,13 +72,25 @@ Try it!! I am happy to hear your feedback or any kind of new features.

![Workflow](https://github.com/santoshshinde2012/node-boilerplate/blob/master/wiki/boilerplate-workflow.png?raw=true)

## Encryption

Set the `APPLY_ENCRYPTION` environment variable to `true` to enable encryption.

## Global Environment Object

You can directly access the environment attributes in any component/file using global environment object. For more details please check file `src/global.ts`.

*Example*

To access the `applyEncryption` attribute from `Envionment` class to Response Handler, write `environment.applyEncryption`;

## Default System Health Status API

- `${host}/api/system` - Return the system information in response
- `${host}/api/time` - Return the current time in response
- `${host}/api/usage` - Return the process and system memory usage in response
- `${host}/api/process` - Return the process details in response
- `${host}/api/error` - Return the error generated object in response
- `${host}/api/status/system` - Return the system information in response
- `${host}/api/status/time` - Return the current time in response
- `${host}/api/status/usage` - Return the process and system memory usage in response
- `${host}/api/status/process` - Return the process details in response
- `${host}/api/status/error` - Return the error generated object in response

## Refrences

Expand Down
3 changes: 3 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
module.exports = {
preset: 'ts-jest',
roots: [
"./tests"
],
testEnvironment: 'node',
setupFiles: ['dotenv/config'],
};
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
"version": "1.0.0",
"description": "",
"main": "index.js",
"types": "./types/global.d.ts",
"scripts": {
"start": "nodemon index.js",
"build": "npm run lint && tsc -p .",
"start:local": "npm run lint && NODE_ENV=local nodemon index.js",
"start:test": "npm run lint && NODE_ENV=test nodemon build/server.js",
"start:test": "npm run lint && NODE_ENV=test node build/server.js",
"start:staging": "npm run lint && NODE_ENV=staging node build/server.js",
"start:production": "npm run lint && NODE_ENV=production build/server.js",
"start:production": "npm run lint && NODE_ENV=production node build/server.js",
"lint": "eslint src/**/*.ts",
"lint-fix": "eslint --fix src/**/*.ts",
"pretty": "prettier --write 'src/**/*.ts'",
"prepare": "husky install",
"test:custom": "npm test -- tests/unit-tests/lib/crypto.spec.ts",
"precommit": "npm run lint-fix && npm run pretty",
"prepush": "npm run lint ",
"test": "jest --coverage",
Expand Down
7 changes: 0 additions & 7 deletions src/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,13 @@ import * as express from 'express';
import * as http from 'http';
import * as helmet from 'helmet';
import registerRoutes from './routes';
import Environment from './environments/environment';
import addErrorHandler from './middleware/error-handler';

export default class App {
public express: express.Application;

public httpServer: http.Server;

public env : Environment;

constructor(env: Environment) {
this.env = env;
}

public async init(): Promise<void> {
this.express = express();
this.httpServer = http.createServer(this.express);
Expand Down
13 changes: 13 additions & 0 deletions src/environments/environment.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
interface IEnvironment {
port: number;
secretKey: string;
applyEncryption: boolean;
getCurrentEnvironment(): string;
setEnvironment(env: string): void;
isProductionEnvironment(): boolean;
isDevEnvironment(): boolean;
isTestEnvironment(): boolean;
isStagingEnvironment(): boolean;
}

export default IEnvironment;
Loading

0 comments on commit 202dd0e

Please sign in to comment.