From 8c0435895df3d5bf0c11564326f19214fe49b4fa Mon Sep 17 00:00:00 2001 From: Yutsuo Date: Mon, 7 Jan 2019 13:10:41 -0200 Subject: [PATCH] Phase III Task 4 --- docker-compose.yml | 25 ++++-------------- nginx/Dockerfile | 2 ++ {nginx2 => nginx}/html/admin.html | 0 {nginx2 => nginx}/html/index.html | 3 ++- {nginx2 => nginx}/html/nay.html | 0 nginx/html/test.html | 43 +++++++++++++++++++++++++++++++ {nginx2 => nginx}/html/test.js | 0 {nginx2 => nginx}/html/test2.html | 0 {nginx2 => nginx}/html/yay.html | 1 + nginx/nginx.conf | 9 +------ nginx2/Dockerfile | 5 ---- nginx2/html/test.html | 13 ---------- nginx2/nginx.conf | 11 -------- node/Dockerfile | 11 +++++--- node/app.js | 34 ++++++++++++++++-------- node/package.json | 2 +- 16 files changed, 86 insertions(+), 73 deletions(-) rename {nginx2 => nginx}/html/admin.html (100%) rename {nginx2 => nginx}/html/index.html (88%) rename {nginx2 => nginx}/html/nay.html (100%) create mode 100644 nginx/html/test.html rename {nginx2 => nginx}/html/test.js (100%) rename {nginx2 => nginx}/html/test2.html (100%) rename {nginx2 => nginx}/html/yay.html (82%) delete mode 100644 nginx2/Dockerfile delete mode 100644 nginx2/html/test.html delete mode 100644 nginx2/nginx.conf diff --git a/docker-compose.yml b/docker-compose.yml index 14ef211..caa2aaf 100755 --- a/docker-compose.yml +++ b/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: diff --git a/nginx/Dockerfile b/nginx/Dockerfile index d6eac3e..8f74bfc 100644 --- a/nginx/Dockerfile +++ b/nginx/Dockerfile @@ -1,3 +1,5 @@ FROM nginx COPY nginx.conf /etc/nginx/nginx.conf + +COPY ./html/ /data/www/ \ No newline at end of file diff --git a/nginx2/html/admin.html b/nginx/html/admin.html similarity index 100% rename from nginx2/html/admin.html rename to nginx/html/admin.html diff --git a/nginx2/html/index.html b/nginx/html/index.html similarity index 88% rename from nginx2/html/index.html rename to nginx/html/index.html index 15fbb2c..6733613 100644 --- a/nginx2/html/index.html +++ b/nginx/html/index.html @@ -1,9 +1,10 @@ +

WELCOME

-

Authenticate thyself

+

Authenticate thyself

diff --git a/nginx2/html/nay.html b/nginx/html/nay.html similarity index 100% rename from nginx2/html/nay.html rename to nginx/html/nay.html diff --git a/nginx/html/test.html b/nginx/html/test.html new file mode 100644 index 0000000..6f1bdcf --- /dev/null +++ b/nginx/html/test.html @@ -0,0 +1,43 @@ + + + + + + + + JS Bin + + +

HTML calling REST API

+
+ + \ No newline at end of file diff --git a/nginx2/html/test.js b/nginx/html/test.js similarity index 100% rename from nginx2/html/test.js rename to nginx/html/test.js diff --git a/nginx2/html/test2.html b/nginx/html/test2.html similarity index 100% rename from nginx2/html/test2.html rename to nginx/html/test2.html diff --git a/nginx2/html/yay.html b/nginx/html/yay.html similarity index 82% rename from nginx2/html/yay.html rename to nginx/html/yay.html index ab5e5c0..4bc535d 100644 --- a/nginx2/html/yay.html +++ b/nginx/html/yay.html @@ -1,3 +1,4 @@ +

THOUS HAST SUCCEEDED

diff --git a/nginx/nginx.conf b/nginx/nginx.conf index 44ce238..7ac6bcd 100644 --- a/nginx/nginx.conf +++ b/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; } } } \ No newline at end of file diff --git a/nginx2/Dockerfile b/nginx2/Dockerfile deleted file mode 100644 index 8f74bfc..0000000 --- a/nginx2/Dockerfile +++ /dev/null @@ -1,5 +0,0 @@ -FROM nginx - -COPY nginx.conf /etc/nginx/nginx.conf - -COPY ./html/ /data/www/ \ No newline at end of file diff --git a/nginx2/html/test.html b/nginx2/html/test.html deleted file mode 100644 index 3888b26..0000000 --- a/nginx2/html/test.html +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - JS Bin - - -

TEST HTML calling NodeJS

-
- - \ No newline at end of file diff --git a/nginx2/nginx.conf b/nginx2/nginx.conf deleted file mode 100644 index 7ac6bcd..0000000 --- a/nginx2/nginx.conf +++ /dev/null @@ -1,11 +0,0 @@ -worker_processes 1; - -events { worker_connections 1024; } - -http { - server { - location / { - root /data/www; - } - } -} \ No newline at end of file diff --git a/node/Dockerfile b/node/Dockerfile index ae6970e..c749069 100755 --- a/node/Dockerfile +++ b/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 diff --git a/node/app.js b/node/app.js index 8e3b2c6..0df77ec 100755 --- a/node/app.js +++ b/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); diff --git a/node/package.json b/node/package.json index 05a7fbd..a52b114 100755 --- a/node/package.json +++ b/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" } }