| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- import 'dotenv/config'
- import express from "express"
- import bodyParser from "body-parser"
- import basicAuth from "express-basic-auth"
- const app = express();
- app.use(bodyParser.json());
- app.use(basicAuth({
- users : { 'admin' : 'superPasswd' },
- challange : true
- }));
- const CHAIN4ALL_SERVICE_PORT = process.env.CHAIN4ALL_SERVICE_PORT || 3000;
- const IROHA_API_HOST = process.env.IROHA_API_HOST || "http://localhost";
- const IROHA_API_PORT = process.env.IROHA_API_PORT || 5000;
- const IROHA_DOMAIN = process.env.IROHA_DOMAIN || "test";
- const IROHA_ASSET = process.env.IROHA_ASSET || "coin";
- const DATA_OWNER = process.env.DATA_OWNER || "admin"
- const PRICE_MODIFIER = process.env.PRICE_MODIFIER || 0.5;
- 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) });
- }
- else {
- res.status(400);
- throw Error(JSON.stringify({ error: { name: "Error, request has no body with \"extent\" property!" } }));
- }
- });
- app.post("/buy", async (req, res, next) => {
- try {
- if (!req.body) {
- res.status(400);
- throw Error(JSON.stringify({ error: { name: "Error, request has no body!" } }));
- }
- if (!req.body.txHash) {
- res.status(400);
- throw Error(JSON.stringify({ error: { name: "Error, request body has no \"txHash\" property!" } }));
- }
- }
- catch (err) {
- next(err);
- }
- });
- function getArea(extent) {
- let y1 = extent[0][1];
- let y4 = extent[3][1];
- let x1 = extent[0][0];
- let x2 = extent[1][0];
- let height = Math.abs(y1 - y4);
- let width = Math.abs(x1 - x2);
- return height * width;
- }
- function getPrice(extent) {
- return getArea(extent) * PRICE_MODIFIER;
- }
- function errorMiddleware(err, req, res, next) { //TODO: add custom Exception class
- console.log(err);
- res.status(500);
- res.send(err.message);
- }
- app.use(errorMiddleware);
- app.listen(CHAIN4ALL_SERVICE_PORT, () => {
- console.log(`Listening at http://localhost:${CHAIN4ALL_SERVICE_PORT}`)
- });
|