From e6cd7a0f6d507efb9fff5d71a60b428b2a504d01 Mon Sep 17 00:00:00 2001 From: Mahesh Asolkar Date: Mon, 2 Jan 2023 11:19:30 -0800 Subject: [PATCH] Configurable website in Docker --- .dockerignore | 2 + Dockerfile | 17 +++ README.md | 54 ++++++++++ package.json | 10 ++ siteinfo.js | 48 +++++++++ website.js | 294 ++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 425 insertions(+) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 README.md create mode 100644 package.json create mode 100644 siteinfo.js create mode 100644 website.js diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..93f1361 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,2 @@ +node_modules +npm-debug.log diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..627f38d --- /dev/null +++ b/Dockerfile @@ -0,0 +1,17 @@ +# Based on node +FROM node:13-alpine + +RUN apk add git + +# Create app directory +WORKDIR /usr/src/app + +# Install app dependencies +RUN git clone https://git.heshapps.com/asolkar/mma-website.git . + +RUN npm install +RUN npm ci --only=production + +EXPOSE 3000 +CMD ["node", "website.js"] + diff --git a/README.md b/README.md new file mode 100644 index 0000000..f5be94d --- /dev/null +++ b/README.md @@ -0,0 +1,54 @@ +# Build docker image + + % cd + % docker build --tag mahesh/mma-website --no-cache . + +*Note*: If `--no-cache` is not used, cloning of git repo from the docker file does not work in subsequent builds + +# Run docker container + + % docker run --publish 49330:3000 --detach mahesh/mma-website + 7e4dfed65d776e3c8cd8e5c6e970699b561bbbc6f2d63d5d75b7ee68c9672358 + +# Get a shell in running container + + % docker exec --interactive --tty 7e4dfed65d776e3c8cd8e5c6e970699b561bbbc6f2d63d5d75b7ee68c9672358 /bin/bash + +# Manage docker containers + +* List running containers + +``` + % docker ps + CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES + d511b20c73d6 mahesh/mma-website "docker-entrypoint.s…" 8 minutes ago Up 8 minutes 0.0.0.0:49330->3000/tcp upbeat_lederberg +``` + +* List all docker containrs + +``` + % docker ps --all + CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES + d511b20c73d6 mahesh/mma-website "docker-entrypoint.s…" 12 minutes ago Exited (137) 3 minutes ago upbeat_lederberg + % docker ps --all --quiet + d511b20c73d6 +``` + +* Stop a running container + +``` + % docker stop d511b20c73d6 +``` + +* Remove a container + +``` + % docker rm d511b20c73d6 +``` + +# Local debug + +``` + % SITE_NAME="asolkar.net" SITE_URL="https://asolkar.net" SITE_TITLE="Home of the Asolkars ..." node website.js + Server running at http://0.0.0.0:3000/ +``` diff --git a/package.json b/package.json new file mode 100644 index 0000000..5ad7d04 --- /dev/null +++ b/package.json @@ -0,0 +1,10 @@ +{ + "name": "mma_website_docker", + "version": "0.1", + "description": "Generic Website", + "author": "Mahesh Asolkar ", + "main": "website.js", + "scripts": { + "start": "node website.js" + } +} diff --git a/siteinfo.js b/siteinfo.js new file mode 100644 index 0000000..1bd9b0e --- /dev/null +++ b/siteinfo.js @@ -0,0 +1,48 @@ +// Exported functions +module.exports = { + site_info: function (req) { + + var ret = { + "site_name": "Website Template", + "site_url": "https://website.template", + "site_title": "Title of website", + }; + + if ("SITE_NAME" in process.env) { + ret.name = process.env.SITE_NAME; + } + if ("SITE_URL" in process.env) { + ret.url = process.env.SITE_URL; + } + if ("SITE_TITLE" in process.env) { + ret.title = process.env.SITE_TITLE; + } + + return ret; + }, + site_ga_stub: function (req) { + // Tracking disabled by DNT + if (("dnt" in req.headers) && (req.headers["dnt"] == 1)) { + return ``; + } + + // Google Analytics stub + if ("SITE_GA_ID" in process.env) { + return ` + + +`; + } + + // Default - no tracking information provided + return ""; + + } +} diff --git a/website.js b/website.js new file mode 100644 index 0000000..e1696be --- /dev/null +++ b/website.js @@ -0,0 +1,294 @@ +'use strict'; + +const http = require('http'); + +// ---------------------------------------------------------------------- +const hostname = '0.0.0.0'; +const port = 3000; + +// ---------------------------------------------------------------------- +var siteinfo = require('./siteinfo.js'); + +// ---------------------------------------------------------------------- +function getPage(req) { + var route = req.url.substring(1); + var website = siteinfo.site_info(req); + var ga_tracking_stub = siteinfo.site_ga_stub(req); + + console.log(`Serving route ${route}`); + + return ` + + ${website.title} + + + + + + + + + + + + + + + +
+ ${website.name} +
+
+
+
+
+
+ + +${ga_tracking_stub} + + +`; +} + +// ---------------------------------------------------------------------- +const server = http.createServer((req, res) => { + res.statusCode = 200; + res.setHeader('Content-Type', 'text/html'); + res.write(getPage(req)); + res.end(); + console.log(`Serviced request ${req}`); +}).listen(port, hostname, () => { + console.log(`Server running at http://${hostname}:${port}/`); +});