Skip to content

Commit

Permalink
Containerizing app with docker
Browse files Browse the repository at this point in the history
  • Loading branch information
Popovkov57 committed Oct 3, 2022
1 parent 6fa98db commit 2e9cc64
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 15 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
21 changes: 21 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]


32 changes: 19 additions & 13 deletions authMiddleware.js
Original file line number Diff line number Diff line change
@@ -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();
Expand Down
16 changes: 16 additions & 0 deletions deploy-package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
1 change: 0 additions & 1 deletion src/app/model/rest.datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export class RestDataSource {
}

saveOrder(order: Order): Observable<Order> {
console.log(JSON.stringify(order));
return this.http.post<Order>(this.baseUrl + "orders", order);
}

Expand Down
1 change: 0 additions & 1 deletion src/app/model/static.datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ export class StaticDataSource {
}

saveOrder(order: Order): Observable<Order> {
console.log(JSON.stringify(order));
return from([order]);
}
}

0 comments on commit 2e9cc64

Please sign in to comment.