Initial commit

This commit is contained in:
Mahesh Asolkar 2020-01-26 17:41:23 -08:00
commit fdb592deb3
5 changed files with 114 additions and 0 deletions

2
.dockerignore Normal file
View File

@ -0,0 +1,2 @@
node_modules
npm-debug.log

18
Dockerfile Normal file
View File

@ -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"]

32
README.md Normal file
View File

@ -0,0 +1,32 @@
# Build docker image
% cd <app source directory>
% 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

52
ask.js Normal file
View File

@ -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 `<html>
<head>
<title>${question}?</title>
</head>
<body>
<h1>${question}?</h1>
<p>${answer}</p>
</body>
</html>`;
}
// ----------------------------------------------------------------------
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}/`);
});

10
package.json Normal file
View File

@ -0,0 +1,10 @@
{
"name": "mma_ask_docker_app",
"version": "0.5",
"description": "Ask app",
"author": "Mahesh Asolkar <mahesh@mahesha.com>",
"main": "ask.js",
"scripts": {
"start": "node ask.js"
}
}