Procházet zdrojové kódy

get price from area and basic auth hotfix

kunickyd před 3 roky
rodič
revize
2afc319ab2
1 změnil soubory, kde provedl 11 přidání a 10 odebrání
  1. 11 10
      app.ts

+ 11 - 10
app.ts

@@ -14,13 +14,14 @@ import  cors  from 'cors'
 
 const app = express();
 app.use(bodyParser.json());
-app.use(basicAuth({
-    users: { admin: 'superPasswd' },
-    challenge: true
-}));
 
 //TODO is cors package necesary? basic middleware could suffice 
 app.use(cors()); //TODO: set only safe origins
+app.use(basicAuth({
+    users: { admin: 'superPasswd' }    
+}));
+
+
 
 const asyncExec = util.promisify(exec);
 
@@ -38,13 +39,13 @@ app.get("/", (req, res) => {
     res.send("Chain4All Blockchain service");
 });
 
-app.post("/price", (req, res) => {
-    if (req.body && req.body.extent) {
-        res.send({ price: getPrice(req.body.extent) });
+app.post("/price", (req, res) => { //add caching of same requests    
+    if (req.body && req.body.area) {
+        res.send({ price: getPrice(req.body.area) });
     }
     else {
         res.status(400);
-        throw Error(JSON.stringify({ error: { name: "Error, request has no body with \"extent\" property!" } }));
+        throw Error(JSON.stringify({ error: { name: "Error, request has no body with \"area\" property!" } }));
     }
 });
 
@@ -129,8 +130,8 @@ function getArea(extent: Array<Array<number>>): number {
     return height * width;
 }
 
-function getPrice(extent: Array<Array<number>>): number {
-    return getArea(extent) * PRICE_MODIFIER;
+function getPrice(area: number) : number {
+    return area * PRICE_MODIFIER;
 }
 
 function errorMiddleware(err: any, req: any, res: any, next: any): void { //TODO: add custom Exception class