| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- 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' },
- challenge: 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: number = parseFloat(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: Array<Array<number>>) : number {
- 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: Array<Array<number>>) : number{
- return getArea(extent) * PRICE_MODIFIER;
- }
- function errorMiddleware(err: any, req: any, res: any, next: any): void { //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}`)
- });
|