Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
anechunaev committed Jan 19, 2022
0 parents commit 5c4ceba
Show file tree
Hide file tree
Showing 47 changed files with 8,905 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.awcache/
dist/
node_modules/
.DS_Store/
.git/
Dockerfile
71 changes: 71 additions & 0 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# 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: [ master ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ master ]
schedule:
- cron: '15 4 * * 0'

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:
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
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
dist/
node_modules/
.awcache-browser/
.awcache-server/
.awcache
.DS_Store/
.vscode/
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
registry=https://registry.npmjs.org/
75 changes: 75 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
FROM node:16-alpine as builder

ARG PORT=8080
ARG HOST='0.0.0.0'

ENV HOST=$HOST \
PORT=$PORT \
DB_HOST=$DB_HOST \
DB_USER=$DB_USER \
DB_ACCESS_TOKEN=$DB_ACCESS_TOKEN \
TERM=xterm \
LANG=en_US.UTF-8 \
TZ='Europe/Moscow' \
NO_UPDATE_NOTIFIER=true \
NPM_CONFIG_USERCONFIG=/tmp/.npmrc \
NPM_CONFIG_CACHE=/tmp/npm-cache \
NPM_CONFIG_PREFIX=/tmp/npm-global

# Java and OpenJDK is used for OpenAPI tools
RUN apk add openjdk11 && \
mkdir -p /usr/share/app \
&& chown 1001:0 /usr/share/app \
&& mkdir -p /tmp/npm-cache \
&& mkdir -p /tmp/npm-global

WORKDIR /usr/share/app

COPY package.json .
COPY package-lock.json .
COPY .npmrc .
RUN npm ci --no-audit --no-fund
COPY static ./static
COPY bin ./bin
COPY config ./config
COPY tsconfig.json .
COPY globals.d.ts .
COPY openapi.json .
COPY docs ./docs
COPY src ./src

ARG NODE_ENV=production
ENV NODE_ENV $NODE_ENV

RUN chown -R 1001:0 ./docs
RUN sh ./bin/build.sh



FROM node:16-alpine as runner

ARG PORT=8080
ARG HOST='0.0.0.0'
ARG NODE_ENV=production

ENV HOST=$HOST \
PORT=$PORT \
DB_HOST=$DB_HOST \
DB_USER=$DB_USER \
DB_ACCESS_TOKEN=$DB_ACCESS_TOKEN \
NODE_ENV=$NODE_ENV

WORKDIR /usr/share/app
RUN chown -R 1001:0 .

COPY --from=builder --chown=1001:0 /usr/share/app/bin ./bin
COPY --from=builder --chown=1001:0 /usr/share/app/dist ./dist
COPY --from=builder --chown=1001:0 /usr/share/app/docs/html ./docs/html
COPY --from=builder --chown=1001:0 /usr/share/app/static ./static
COPY --from=builder --chown=1001:0 /usr/share/app/node_modules/full-icu ./node_modules/full-icu

EXPOSE $PORT
HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 CMD [ "./bin/healthcheck.sh" ]

USER 1001
CMD [ "./bin/start.sh" ]
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2022 Artem Nechunaev <artem@nechunaev.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
64 changes: 64 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Modern Node.js API Boilerplate

## Technologies
- TypeScript v4, Node.js v16, Express v4
- API Schema: OpenAPI v3

## Limitations and restrictions
- TLS/SSL not supported, it should be implemented by platform router or reverse-proxy

## Docker repository

`bungubot/nodejs-api-boilerplate`

## Features

### Build process

- [x] Containerized build process with Docker
- [x] Multi-stage build with Docker

### Server-side application

- [x] Graceful shutdown
- [x] Internationalization support with `Intl` API for Node.js
- [ ] Optional: Use GraphQL to implement client-server-client messaging
- [ ] Optional: Add GRPC protocol

### Development tools

- [ ] Quality assurance tools
- [ ] Integrate framework for unit-testing
- [ ] Integrate framework for e2e testing
- [ ] Coverage reports
- [ ] Performance reports
- [ ] Linters and code-style checkers
- [ ] Watcher
- [ ] Hot module replacement for client-side code
- [ ] Iterative assets building

## How to build
Production build:
```sh
$ npm run build
```

Development build:
```sh
$ npm run dev:build
```

Start server:
```sh
$ npm start
# or
$ ./bin/start.sh
```

It is possible to start an application cluster on a local development machine. Docker Compose is used for running development environment. Just change `./dev/docker-compose.yaml` according to your application and run it:

```sh
$ cd dev
$ docker-compose build
$ docker-compose up
```
4 changes: 4 additions & 0 deletions bin/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh

npm run docgen:html
npm run build
7 changes: 7 additions & 0 deletions bin/healthcheck.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh

STATUSCODE=$(curl --silent --output /dev/null --write-out "%{http_code}" http://0.0.0.0:$PORT/healthcheck)

if test $STATUSCODE -ne 200; then
exit 1
fi
32 changes: 32 additions & 0 deletions bin/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/sh

prep_term()
{
unset term_child_pid
unset term_kill_needed
trap 'handle_term' TERM INT
}

handle_term()
{
if [ "${term_child_pid}" ]; then
kill -TERM "${term_child_pid}" 2>/dev/null
else
term_kill_needed="yes"
fi
}

wait_term()
{
term_child_pid=$!
if [ "${term_kill_needed}" ]; then
kill -TERM "${term_child_pid}" 2>/dev/null
fi
wait ${term_child_pid}
trap - TERM INT
wait ${term_child_pid}
}

prep_term
/usr/bin/env node --icu-data-dir=./node_modules/full-icu ./dist/server &
wait_term
64 changes: 64 additions & 0 deletions config/webpack/server.dev.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const { resolve } = require("path");
const webpack = require("webpack");
const ForkTsCheckerWebpackPlugin = require("fork-ts-checker-webpack-plugin");

module.exports = {
mode: "development",
target: "node",
context: resolve(__dirname),
devtool: "cheap-module-source-map",
entry: {
server: resolve(__dirname, "../../src/index.ts"),
},
output: {
filename: "[name].js",
chunkFilename: "[name].chunk.js",
publicPath: "/dist/",
path: resolve(__dirname, "../../dist"),
},
resolve: {
extensions: [".js", ".jsx", ".ts", ".tsx", ".json"],
modules: [resolve(__dirname, "../../src"), "node_modules"],
},
module: {
rules: [
{
test: /\.(j|t)sx?$/,
use: [
{
loader: "ts-loader",
options: {
configFile: "tsconfig.json",
transpileOnly: true,
logInfoToStdOut: true,
},
},
],
exclude: /node_modules/,
},
],
},
optimization: {
chunkIds: "named",
moduleIds: "named",
},
plugins: [
new webpack.DefinePlugin({
"process.env": {
NODE_ENV: JSON.stringify("development"),
HOST: JSON.stringify(process.env.HOST),
PORT: JSON.stringify(process.env.PORT),
},
PRODUCTION: JSON.stringify(false),
__dirname: JSON.stringify(__dirname),
}),
new webpack.optimize.LimitChunkCountPlugin({
maxChunks: 1,
}),
new ForkTsCheckerWebpackPlugin({
typescript: {
configFile: "../../tsconfig.json",
},
}),
],
};
Loading

0 comments on commit 5c4ceba

Please sign in to comment.