|
|
@@ -1,6 +1,6 @@
|
|
|
import 'dotenv/config'
|
|
|
-import grpc from "grpc"
|
|
|
-import express from "express"
|
|
|
+import grpc from "grpc"
|
|
|
+import express, { Request } from "express"
|
|
|
import bodyParser from "body-parser"
|
|
|
import basicAuth from "express-basic-auth"
|
|
|
import {
|
|
|
@@ -10,7 +10,8 @@ import {
|
|
|
import { queries } from 'iroha-helpers'
|
|
|
import util from 'util'
|
|
|
import { exec } from 'child_process'
|
|
|
-import cors from 'cors'
|
|
|
+import cors from 'cors'
|
|
|
+import * as sql from 'sqlite3'
|
|
|
|
|
|
const app = express();
|
|
|
app.use(bodyParser.json());
|
|
|
@@ -18,7 +19,7 @@ app.use(bodyParser.json());
|
|
|
//TODO is cors package necesary? basic middleware could suffice
|
|
|
app.use(cors()); //TODO: set only safe origins
|
|
|
app.use(basicAuth({
|
|
|
- users: { admin: 'superPasswd' }
|
|
|
+ users: { admin: 'superPasswd' }
|
|
|
}));
|
|
|
|
|
|
|
|
|
@@ -35,10 +36,38 @@ const CHAIN4ALL_RASTER_CLIP_SCRIPT_PATH = process.env.CHAIN4ALL_RASTER_CLIP_SCRI
|
|
|
const CHAIN4ALL_SERVICE_PORT = process.env.CHAIN4ALL_SERVICE_PORT || 3000;
|
|
|
const PRICE_MODIFIER: number = parseFloat(process.env.PRICE_MODIFIER || "0.5");
|
|
|
|
|
|
+const DB_FILE_NAME: string = "data/transfers.db";
|
|
|
+
|
|
|
app.get("/", (req, res) => {
|
|
|
res.send("Chain4All Blockchain service");
|
|
|
});
|
|
|
|
|
|
+app.get("/transactions", async (req, res, next) => {
|
|
|
+ let db = getDbConnection();
|
|
|
+
|
|
|
+ db.all(
|
|
|
+ "SELECT hash, user " +
|
|
|
+ "FROM transfers " +
|
|
|
+ "ORDER BY id DESC " +
|
|
|
+ "LIMIT 10;",
|
|
|
+ [],
|
|
|
+ (err, rows) => {
|
|
|
+ if(err){
|
|
|
+ next(err);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ console.log(rows);
|
|
|
+ }
|
|
|
+ );
|
|
|
+});
|
|
|
+
|
|
|
+app.post("/transaction", async (req, res, next) => {
|
|
|
+ let db = getDbConnection();
|
|
|
+
|
|
|
+
|
|
|
+});
|
|
|
+
|
|
|
app.post("/price", (req, res) => { //add caching of same requests
|
|
|
if (req.body && req.body.area) {
|
|
|
res.send({ price: getPrice(req.body.area) });
|
|
|
@@ -73,25 +102,39 @@ app.post("/buy", async (req, res, next) => {
|
|
|
|
|
|
let extent: number[] = JSON.parse(txDetail.description).extent as number[];
|
|
|
let dataFileId: string = Date.now().toString();
|
|
|
-
|
|
|
+
|
|
|
let dataCommand = CHAIN4ALL_RASTER_CLIP_SCRIPT_PATH + ' ' + extent[0] + ' ' + extent[1] + ' ' + extent[2] + ' ' + extent[3] + ' ' + dataFileId;
|
|
|
console.debug(dataCommand);
|
|
|
|
|
|
const { stdout, stderr } = await asyncExec(dataCommand);
|
|
|
-
|
|
|
+
|
|
|
console.debug(stdout);
|
|
|
|
|
|
- if(stderr){
|
|
|
+ if (stderr) {
|
|
|
console.warn(stderr);
|
|
|
}
|
|
|
|
|
|
- res.send({dataUrl: "https://gis.lesprojekt.cz/chain4all/raster_" + dataFileId + ".tif"});
|
|
|
+ res.send({ dataUrl: "https://gis.lesprojekt.cz/chain4all/raster_" + dataFileId + ".tif" });
|
|
|
}
|
|
|
catch (err) {
|
|
|
next(err);
|
|
|
}
|
|
|
});
|
|
|
|
|
|
+function getDbConnection() {
|
|
|
+ let db = new sql.Database(DB_FILE_NAME);
|
|
|
+
|
|
|
+ db.run(
|
|
|
+ "CREATE TABLE [IF NOT EXISTS] transfers ( " +
|
|
|
+ "id INTEGER PRIMARY KEY, " +
|
|
|
+ "hash TEXT NOT NULL, " +
|
|
|
+ "user TEXT NOT NULL, " +
|
|
|
+ ") [WITHOUT ROWID]; "
|
|
|
+ );
|
|
|
+
|
|
|
+ return db;
|
|
|
+}
|
|
|
+
|
|
|
async function getTransactionDetail(txHash: string, user: string) {
|
|
|
let quer: any = await queries.getAccountTransactions({
|
|
|
privateKey: IROHA_ADMIN_PRIV,
|
|
|
@@ -116,7 +159,7 @@ async function getTransactionDetail(txHash: string, user: string) {
|
|
|
return quer.transactionsList[0].payload.reducedPayload.commandsList[0].transferAsset;
|
|
|
}
|
|
|
|
|
|
-function getPrice(area: number) : number {
|
|
|
+function getPrice(area: number): number {
|
|
|
return area * PRICE_MODIFIER;
|
|
|
}
|
|
|
|