From 2e9cc64d6143d687874127ea01a18b3a50e87ef3 Mon Sep 17 00:00:00 2001 From: Popovkov57 Date: Mon, 3 Oct 2022 13:27:17 +0200 Subject: [PATCH] Containerizing app with docker --- .dockerignore | 1 + Dockerfile | 21 ++++++++++++++++++++ authMiddleware.js | 32 ++++++++++++++++++------------ deploy-package.json | 16 +++++++++++++++ src/app/model/rest.datasource.ts | 1 - src/app/model/static.datasource.ts | 1 - 6 files changed, 57 insertions(+), 15 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 deploy-package.json diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..b512c09 --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +node_modules \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..fc1d688 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,21 @@ + +FROM node:8.11.2 + +RUN mkdir -p /usr/src/sportsstore + +COPY dist/sport-store /usr/src/sportsstore/dist/SportsStore + +COPY authMiddleware.js /usr/src/sportsstore/ +COPY serverdata.json /usr/src/sportsstore/ +COPY server.js /usr/src/sportsstore/server.js +COPY deploy-package.json /usr/src/sportsstore/package.json + +WORKDIR /usr/src/sportsstore + +RUN npm install + +EXPOSE 80 + +CMD ["node", "server.js"] + + diff --git a/authMiddleware.js b/authMiddleware.js index 528404e..ce59f2d 100644 --- a/authMiddleware.js +++ b/authMiddleware.js @@ -1,30 +1,36 @@ const jwt = require("jsonwebtoken"); -const APP_SECRET = "myappsecreta"; +const APP_SECRET = "myappsecret"; const USERNAME = "admin"; const PASSWORD = "secret"; -module.exports = function(req, res, next ) { - if ((req.url == "/api/login" || req.url == "/login") && req.method == "POST") { - if (req.body != null && req.body.name == USERNAME && req.body.password == PASSWORD) { - let token = jwt.sign({data: USERNAME, expireIn: "1h"}, APP_SECRET); - res.json({success: true, token: token}); +module.exports = function (req, res, next) { + + if ((req.url == "/api/login" || req.url == "/login") + && req.method == "POST") { + if (req.body != null && req.body.name == USERNAME + && req.body.password == PASSWORD) { + let token = jwt.sign({ data: USERNAME, expiresIn: "1h" }, APP_SECRET); + res.json({ success: true, token: token }); } else { - res.json({success : false}); + res.json({ success: false }); } res.end(); return; - } else if ((((req.url.startsWith("/api/products") || req.url.startsWith("/products")) - ||(req.url.startsWith("/api/categories") || req.url.startsWith("/categories"))) && req.method != "GET") - ||((req.url.startsWith("/api/orders") || req.url.startsWith("/orders")) && req.method != "POST")) { + } else if ((((req.url.startsWith("/api/products") + || req.url.startsWith("/products")) + || (req.url.startsWith("/api/categories") + || req.url.startsWith("/categories"))) && req.method != "GET") + || ((req.url.startsWith("/api/orders") + || req.url.startsWith("/orders")) && req.method != "POST")) { let token = req.headers["authorization"]; - if(token != null && token.startsWith("Bearer<")){ - token = token.substring(7, token.length-1); + if (token != null && token.startsWith("Bearer<")) { + token = token.substring(7, token.length - 1); try { jwt.verify(token, APP_SECRET); next(); return; - } catch (error) {} + } catch (err) { } } res.statusCode = 401; res.end(); diff --git a/deploy-package.json b/deploy-package.json new file mode 100644 index 0000000..6b7fb56 --- /dev/null +++ b/deploy-package.json @@ -0,0 +1,16 @@ +{ + "dependencies": { + "bootstrap":"^5.2.1", + "font-awesome": "4.7" + }, + "devDependencies": { + "json-server": "^0.12.1", + "jsonwebtoken": "^8.1.1", + "express": "^4.16.3", + "https": "^1.0.0", + "connect-history-api-fallback": "^1.5.0" + }, + "script": { + "start": "node server.js" + } +} \ No newline at end of file diff --git a/src/app/model/rest.datasource.ts b/src/app/model/rest.datasource.ts index af7d0f2..003415b 100644 --- a/src/app/model/rest.datasource.ts +++ b/src/app/model/rest.datasource.ts @@ -26,7 +26,6 @@ export class RestDataSource { } saveOrder(order: Order): Observable { - console.log(JSON.stringify(order)); return this.http.post(this.baseUrl + "orders", order); } diff --git a/src/app/model/static.datasource.ts b/src/app/model/static.datasource.ts index f73b9e4..5dea3a4 100644 --- a/src/app/model/static.datasource.ts +++ b/src/app/model/static.datasource.ts @@ -28,7 +28,6 @@ export class StaticDataSource { } saveOrder(order: Order): Observable { - console.log(JSON.stringify(order)); return from([order]); } } \ No newline at end of file