From fdb592deb3676bce7418c96acb6c573b511dc5c6 Mon Sep 17 00:00:00 2001 From: Mahesh Asolkar Date: Sun, 26 Jan 2020 17:41:23 -0800 Subject: [PATCH] Initial commit --- .dockerignore | 2 ++ Dockerfile | 18 ++++++++++++++++++ README.md | 32 +++++++++++++++++++++++++++++++ ask.js | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 10 ++++++++++ 5 files changed, 114 insertions(+) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 README.md create mode 100644 ask.js create mode 100644 package.json 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..c57f025 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,18 @@ +# Based on node +FROM node:13 + +# Create app directory +WORKDIR /usr/src/app + +# Install app dependencies +COPY package*.json ./ + +RUN npm install +# RUN NPM ci --only=production + +# Bundle app source +COPY . . + +EXPOSE 3000 +CMD ["node", "ask.js"] + diff --git a/README.md b/README.md new file mode 100644 index 0000000..42d6f46 --- /dev/null +++ b/README.md @@ -0,0 +1,32 @@ +# Build docker image + + % cd + % docker build --tag mahesh/mma-ask-app . + +# Run docker container + + % docker run --publish 49330:3000 --detach + +# Manage docker containers + +* List running containers + + % docker ps + CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES + d511b20c73d6 mahesh/mma-ask-app "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-ask-app "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 diff --git a/ask.js b/ask.js new file mode 100644 index 0000000..9e21eb7 --- /dev/null +++ b/ask.js @@ -0,0 +1,52 @@ +'use strict'; + +const http = require('http'); + +// ---------------------------------------------------------------------- +const hostname = '0.0.0.0'; +const port = 3000; + +// ---------------------------------------------------------------------- +var data = { + 'canaarohihavecoffee': { + 'target': Date.parse('2020-05-24'), + 'question': "Can Aarohi have coffee" + } +}; + +// ---------------------------------------------------------------------- +function getPage(req) { + var question = "Do I understand your query"; + var answer = "No!"; + + var route = req.url.substring(1); + + console.log(`Serving route ${route}`); + + if (route in data) { + var scope = data[route]; + question = scope.question; + answer = (Date.now() > scope.target) ? "Yes!" : "No!"; + } + + return ` + + ${question}? + + +

${question}?

+

${answer}

+ +`; +} + +// ---------------------------------------------------------------------- +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}/`); +}); diff --git a/package.json b/package.json new file mode 100644 index 0000000..958716b --- /dev/null +++ b/package.json @@ -0,0 +1,10 @@ +{ + "name": "mma_ask_docker_app", + "version": "0.5", + "description": "Ask app", + "author": "Mahesh Asolkar ", + "main": "ask.js", + "scripts": { + "start": "node ask.js" + } +}