Skip to content

Commit

Permalink
Merge pull request #5 from newsdaycom/upgrades
Browse files Browse the repository at this point in the history
Upgrades
  • Loading branch information
tc-mccarthy authored Feb 26, 2024
2 parents 799144a + 6e637b8 commit 2199568
Show file tree
Hide file tree
Showing 8 changed files with 1,935 additions and 853 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:18-bullseye-slim
FROM node:20-bookworm-slim

# Set up directories in advance so we can control the permissions
RUN mkdir -p /usr/app/bin && mkdir -p /usr/app/node_modules && chown -R node:node /usr/app
Expand Down
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

Microservices are becoming the name of the game. Much what we are building are API-only microservices. Here's a template to get started quickly!

## Getting started

Search and replace `express-docker-template` with the name of your service (slugified). Then run `bash ./rebuild` to start up. Then begin development!

## File descriptions

**build-image** | This script will bake a fresh docker image with all of your code and push it to docker hub using the current timestamp as a tag. Modify this file to reflect the username/repo_name of your image's repo and it will do the rest
Expand All @@ -15,7 +19,7 @@ One of two ways to do this is to add an `nginx` rule to the `local.tools.newsday
1. Go to the `sites-available` directory located in `<your virtual_machines directory->/proxy/nginx-config/`
2. Open the file `local.tools.newsday.com.conf` in your preferred text editor.

To open the file in `nano` for example use the command `nano local.tools.newsday.conf`
To open the file in `nano` for example use the command `nano local.tools.newsday.conf`

3. Inside the file you will find various rules for different microservices that follow a similar format. Use the format below to add an additional rule for your microservice, note the braces mark the rule blocks so ensure your rule isn't placed inside another rule block.

Expand All @@ -27,7 +31,6 @@ location ~ /<preferred URL for microservice>(.*) {
}
```

where `hostname` is the `hostname` for your microservice as defined in its `docker-compose.yaml` file.
4. Save, exit and then restart Docker (one is to click the docker icon in the menu bar on the tip and click "Restart").
where `hostname` is the `hostname` for your microservice as defined in its `docker-compose.yaml` file. 4. Save, exit and then restart Docker (one is to click the docker icon in the menu bar on the tip and click "Restart").

Ensure your microservice is running (execute the `bash rebuild` command) then navigate to `local.tools.newsday.com/<your microservice endpoint>` and you should be able to see some message indicating the microservice is online.
19 changes: 12 additions & 7 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
version: '3'
version: "3"

## Search and replace 'express-docker-template' with the name of the service

services:
service-name:
container_name: service-name
hostname: service-name
express-docker-template:
container_name: express-docker-template
hostname: express-docker-template
volumes:
- .:/usr/app
- /usr/app/node_modules
Expand All @@ -14,11 +16,14 @@ services:
- ENV
- BUILD_VERSION=local-build # DO NOT SPECIFY WHEN DEPLOYING TO STAGE
- NODE_ENV=${ENV} # CAN ONLY BE 'local' OR 'production', hardcode to 'production' WHEN DEPLOYING TO STAGE AND PROD
- NODE_OPTIONS=--enable-source-maps
- LOG_LEVEL=info
- LOG_PRETTY=on
logging:
driver: 'json-file'
driver: "json-file"
options:
max-size: '5m'
max-file: '2'
max-size: "5m"
max-file: "2"
networks:
default:
name: special-projects
Expand Down
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import express from 'express';
import cookieParser from 'cookie-parser';
import logger from 'morgan';
// import logger from "morgan";
import apiRouter from './routes/api';
import api_headers from './lib/api_headers';
import api_errors from './lib/api_errors';
import logger from './lib/logger';

const app = express();

app.use(logger('dev'));
app.use(logger.info);
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
Expand All @@ -21,7 +22,7 @@ app.get('/', (req, res, next) => {
});

app.listen(3000, () => {
console.info('Service running on port 3000');
logger.info('Service running on port 3000');
});

export default app;
20 changes: 20 additions & 0 deletions lib/logger.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import winston from 'winston';

const log_format = [
winston.format.errors({ stack: true }),
winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
winston.format.json()
];

if (process.env.LOG_PRETTY === 'on') {
log_format.push(winston.format.prettyPrint());
}

const logger = winston.createLogger({
level: process.env.LOG_LEVEL || 'info',
format: winston.format.combine(...log_format),
defaultMeta: { hostname: process.env.HOSTNAME || 'localhost' },
transports: [new winston.transports.Console()]
});

export default logger;
36 changes: 19 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,36 +1,38 @@
{
"name": "express-docker-template",
"version": "2.2.0",
"version": "2.3.0",
"private": true,
"scripts": {
"dev": "npx eslint --fix --ext 'js' .; npx webpack watch --mode development",
"dev": "npx eslint --fix --ext 'js,cjs,mjs' .; npx webpack watch --mode development",
"build": "echo 'Building production pack'; npx eslint --fix --ext 'js' .; npx webpack --mode production",
"start": "node ./bin/server.js"
"start": "node ./bin/server.js",
"lint:fix": "npx eslint --fix --ext 'js,cjs,mjs' ."
},
"dependencies": {
"cookie-parser": "^1.4.4",
"debug": "^2.6.9",
"express": "^4.16.1",
"http-errors": "^2.0.0",
"morgan": "^1.9.1"
"morgan": "^1.9.1",
"winston": "^3.11.0"
},
"devDependencies": {
"@babel/core": "^7.18.0",
"@babel/eslint-parser": "^7.17.0",
"@babel/preset-env": "^7.18.0",
"babel-loader": "^8.2.5",
"eslint": "^8.16.0",
"@babel/core": "^7.23.9",
"@babel/eslint-parser": "^7.23.10",
"@babel/preset-env": "^7.23.9",
"babel-loader": "^9.1.3",
"eslint": "^8.56.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^6.0.0",
"eslint-plugin-proposal": "^2.3.3",
"eslint-webpack-plugin": "^3.1.1",
"nodemon": "^2.0.22",
"nodemon-webpack-plugin": "^4.8.0",
"terser-webpack-plugin": "^5.3.3",
"webpack": "^5.72.1",
"webpack-cli": "^4.9.2",
"webpack-dev-server": "^4.9.0"
"eslint-webpack-plugin": "^4.0.1",
"nodemon": "^3.0.3",
"nodemon-webpack-plugin": "^4.8.2",
"terser-webpack-plugin": "^5.3.10",
"webpack": "^5.90.1",
"webpack-cli": "^5.1.4",
"webpack-dev-server": "^5.0.1"
}
}
10 changes: 5 additions & 5 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module.exports = {
module: {
rules: [
{
test: /\.(js)$/,
test: /\.(m|c)?(js)$/,
exclude: /node_modules/,
use: ['babel-loader']
}
Expand All @@ -39,7 +39,7 @@ module.exports = {
minimize: process.env.ENV !== 'local',
minimizer: [
new TerserPlugin({
test: /\.(js)$/,
test: /\.(m|c)?(js)$/,
extractComments: true,
parallel: true,
terserOptions: {
Expand All @@ -53,7 +53,7 @@ module.exports = {
plugins: [
new ESLintLoader({
fix: true,
files: ['**/*.js']
files: ['**/*.js', '**/*.mjs', '**/*.cjs']
}),
new NodemonPlugin({
// If using more than one entry, you can specify
Expand All @@ -73,7 +73,7 @@ module.exports = {
ignore: ['*.js.map'],

// Extensions to watch.
ext: 'js,njk,json',
ext: 'js,njk,json,mjs,cjs',

// Unlike the cli option, delay here is in milliseconds (also note that it's a string).
// Here's 1 second delay:
Expand All @@ -93,6 +93,6 @@ module.exports = {
})
],
resolve: {
extensions: ['*', '.js']
extensions: ['*', '.js', '.mjs', '.cjs']
}
};
Loading

0 comments on commit 2199568

Please sign in to comment.