Browse Source

Phase III Task 4

master
Yutsuo 7 years ago
parent
commit
8c0435895d
  1. 25
      docker-compose.yml
  2. 2
      nginx/Dockerfile
  3. 0
      nginx/html/admin.html
  4. 3
      nginx/html/index.html
  5. 0
      nginx/html/nay.html
  6. 43
      nginx/html/test.html
  7. 0
      nginx/html/test.js
  8. 0
      nginx/html/test2.html
  9. 1
      nginx/html/yay.html
  10. 9
      nginx/nginx.conf
  11. 5
      nginx2/Dockerfile
  12. 13
      nginx2/html/test.html
  13. 11
      nginx2/nginx.conf
  14. 11
      node/Dockerfile
  15. 34
      node/app.js
  16. 2
      node/package.json

25
docker-compose.yml

@ -7,23 +7,10 @@ services:
build: ./nginx
image: custom/nginx
volumes:
- nginx1_data1:/usr/share/nginx/html
- nginx1_data2:/etc/nginx
links:
- app
- prometheus
networks:
- network_1
nginx2:
container_name: nginx2
restart: always
build: ./nginx2
image: custom/nginx2
volumes:
- nginx2_data1:/usr/share/nginx/html
- nginx2_data2:/etc/nginx
- nginx_data1:/usr/share/nginx/html
- nginx_data2:/etc/nginx
ports:
- 80:80
- 3002:80
networks:
- network_1
app:
@ -73,10 +60,8 @@ volumes:
mongo_data:
grafana_data:
prom_data:
nginx1_data1:
nginx1_data2:
nginx2_data1:
nginx2_data2:
nginx_data1:
nginx_data2:
networks:
network_1:
ipam:

2
nginx/Dockerfile

@ -1,3 +1,5 @@
FROM nginx
COPY nginx.conf /etc/nginx/nginx.conf
COPY ./html/ /data/www/

0
nginx2/html/admin.html → nginx/html/admin.html

3
nginx2/html/index.html → nginx/html/index.html

@ -1,9 +1,10 @@
<meta charset="UTF-8">
<body>
<div>
<h1>WELCOME</h1>
</div>
<div class="form-header" style="background-color:black; color:white; padding:20px; left:50%; ">
<h2>Authenticate thyself</h2>
<h2 style="font-family: Verdana, Geneva, Tahoma, sans-serif">Authenticate thyself</h2>
</div>
<form method="post" action="http://localhost:3001/token" novalidate>
<div class="form-field" style="background-color:black; color:white; padding:20px; left:50%; ">

0
nginx2/html/nay.html → nginx/html/nay.html

43
nginx/html/test.html

@ -0,0 +1,43 @@
<meta charset="UTF-8">
<script>
// function getAPI(){
// fetch('http://localhost:3001/')
// .then(function (res) {
// res.text()
// .then(function(result){
// document.getElementById('output').innerHTML = result;
// console.log(result);
// })
// })
// }
function getAPI(){
fetch('http://localhost:3001/')
.then(function (res){
res.json().then(function(data){
document.getElementById('output').innerHTML = JSON.stringify(data);
console.log(JSON.stringify(data));
console.log(JSON.stringify(data, ['message']));
console.log('why, though');
console.log(Object.values(data));
console.log(Object.keys(data));
})
})
}
</script>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body onload="getAPI()">
<h1>HTML calling REST API</h1>
<div id="output"></div>
</body>
</html>

0
nginx2/html/test.js → nginx/html/test.js

0
nginx2/html/test2.html → nginx/html/test2.html

1
nginx2/html/yay.html → nginx/html/yay.html

@ -1,3 +1,4 @@
<meta charset="UTF-8">
<body>
<div>
<h1>THOUS HAST SUCCEEDED</h1>

9
nginx/nginx.conf

@ -3,16 +3,9 @@ worker_processes 1;
events { worker_connections 1024; }
http {
upstream app {
server app:3001;
}
server {
listen 80;
location / {
proxy_pass http://app;
root /data/www;
}
}
}

5
nginx2/Dockerfile

@ -1,5 +0,0 @@
FROM nginx
COPY nginx.conf /etc/nginx/nginx.conf
COPY ./html/ /data/www/

13
nginx2/html/test.html

@ -1,13 +0,0 @@
<script src="http://localhost:3001/app.js"></script>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<h1>TEST HTML calling NodeJS</h1>
<div id="message"></div>
</body>
</html>

11
nginx2/nginx.conf

@ -1,11 +0,0 @@
worker_processes 1;
events { worker_connections 1024; }
http {
server {
location / {
root /data/www;
}
}
}

11
node/Dockerfile

@ -11,14 +11,19 @@ COPY .npmrc .npmrc
# This results from the way the Docker image is being built (layers and cache),
# and this is what we should do:
COPY package.json /app
RUN npm install
# RUN npm install
RUN ["npm", "install"]
COPY . /app
ENV mongousr=myTester
ENV mongopwd=test
RUN sh check-env.sh
# RUN sh check-env.sh
RUN ["sh", "check-env.sh"]
CMD node app.js
# CMD node app.js
CMD ["node", "app.js"]
EXPOSE 3001

34
node/app.js

@ -1,3 +1,4 @@
'use strict'
const express = require('express');
const app = express();
const mongoose = require('mongoose');
@ -12,8 +13,12 @@ const jwt = require('jsonwebtoken');
const bodyParser= require('body-parser');
const secret = 'wowmuchsecretveryhiddenwow';
const cookieParser = require('cookie-parser');
const cookie = require('cookie');
const successUrl = 'http://localhost:3002/yay.html';
const failureUrl = 'http://localhost:3002/nay.html';
app.use(cookieParser());
app.use(cookie());
// const morgan = require('morgan');
// // use morgan to log requests to the console
@ -31,6 +36,13 @@ app.use(cookieParser());
// next();
// });
// setting CORS headers
app.all('/', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next()
});
// database connection (with retries)
const options = {
autoIndex: false, // Don't build indexes
@ -60,7 +72,7 @@ const libCounter = new Prometheus.Counter({
const libUptime = new Prometheus.Counter({
name: 'lib_upTime',
help: 'uptime A counter of the application\'s uptime in seconds created with prometheus nodejs library.'
})
});
console.log('mongousr: ', process.env.mongousr);
console.log('mongopwd: ', process.env.mongopwd);
@ -84,8 +96,8 @@ var thingies = mongoose.model('thingieName', testSchema);
// Default message for testing
app.get('/', (req, res, next)=>{
// res.json([{message:'yes, your nodejs app is really running'}]);
res.send('Oh hay');
res.json([{message:'yes, your nodejs app is really running'}]);
// res.send('Oh hay' + '\n');
counter++; // for prometheus invocation_count metric
libCounter.inc(); // for prometheus lib_invocation_count metric
console.log('Hello, I\'m inside endpoint \'/\'');
@ -175,27 +187,27 @@ app.post('/token', function(req, res) {
switch(req.body.username) {
case 'user1':
if (req.body.password === 'pass1') {
token = jwt.sign(claims_user, secret);
let token = jwt.sign(claims_user, secret);
// res.cookie('token',token);
res.setHeader('Set-Cookie', 'token=' + token + '; HttpOnly');
res.setHeader('Set-Cookie', 'Authorization=Bearer ' + token + '; HttpOnly');
console.log('JWT Token: ' + token);
console.log(jwt.decode(token));
res.redirect('http://localhost/yay.html');
res.redirect(successUrl);
} else {
res.redirect('http://localhost/nay.html');
res.redirect(failureUrl);
}
break;
case 'power':
if (req.body.password === 'weak') {
token = jwt.sign(claims_power, secret);
let token = jwt.sign(claims_power, secret);
// res.cookie('token',token);
res.setHeader('Set-Cookie', 'token=' + token + '; HttpOnly');
console.log('JWT Token: ' + token);
console.log(jwt.decode(token));
res.redirect('http://localhost/yay.html');
res.redirect(successUrl);
} else {
res.redirect('http://localhost/nay.html');
res.redirect(failureUrl);
}
break;
default:
@ -208,7 +220,7 @@ app.post('/token', function(req, res) {
// Restricted route root
const restrictedRoutes = express.Router();
app.use('/', restrictedRoutes);
app.use('/restricted', restrictedRoutes);
restrictedRoutes.use(function (req, res, next) {
let sentToken = req.headers['token'];
@ -263,7 +275,7 @@ restrictedRoutes.use(function (req, res, next) {
// });
// Restricted endpoint
restrictedRoutes.get('/restricted', (req, res) => {
restrictedRoutes.get('/', (req, res) => {
// successMsg = JSON.stringify({secret:'You have access to restricted contents!'});
res.status(200).json([{secret:'You have access to restricted contents!'}]);
// res.status(200).send(successMsg);

2
node/package.json

@ -22,7 +22,7 @@
"body-parser": "^1.18.3",
"jsonwebtoken": "^8.4.0",
"morgan": "^1.9.1",
"colors": "^1.3.3",
"cookie": "^0.3.1",
"cookie-parser": "^1.4.3"
}
}

Loading…
Cancel
Save