epic_explorer/server.ts

392 lines
12 KiB
TypeScript
Raw Normal View History

2019-07-16 11:47:19 +05:30
import "zone.js/dist/zone-node";
import "reflect-metadata";
const fs = require("fs");
const path = require("path");
const template = fs
.readFileSync(path.join(__dirname, ".", "browser", "index.html"))
.toString();
2019-07-24 01:14:38 +05:30
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
const { window } = new JSDOM(template);
global["window"] = window;
2022-12-23 10:21:39 +00:00
2019-07-24 01:14:38 +05:30
Object.defineProperty(window.document.body.style, "transform", {
2019-07-16 11:47:19 +05:30
value: () => {
return {
enumerable: true,
configurable: true
};
}
});
2022-12-23 10:21:39 +00:00
// function noOp () { }
// if (typeof window.URL.createObjectURL === 'undefined') {
// Object.defineProperty(window.URL, 'createObjectURL', { value: noOp})
// }
// global["URL"] = window.URL;
// const Blob = require("cross-blob");
// global['Blob'] = Blob;
2019-07-24 01:14:38 +05:30
global["document"] = window.document;
2019-07-16 11:47:19 +05:30
import { enableProdMode } from "@angular/core";
2019-08-14 16:25:23 +05:30
import { Global } from "./server/global";
2019-07-16 11:47:19 +05:30
2019-06-27 12:18:08 +05:30
// Express Engine
2019-07-16 11:47:19 +05:30
import { ngExpressEngine } from "@nguniversal/express-engine";
2019-06-27 12:18:08 +05:30
// Import module map for lazy loading
2019-07-16 11:47:19 +05:30
import { provideModuleMap } from "@nguniversal/module-map-ngfactory-loader";
import express from "express";
import { Request, Response, NextFunction } from "express";
import bodyParser from "body-parser";
//import { logger } from "./server/utils";
import swaggerJSDoc from "swagger-jsdoc";
2019-09-10 18:38:51 +05:30
import { errorMiddleware, redisMiddleware } from "./server/middlewares";
2019-07-16 11:47:19 +05:30
import {
getRepository,
In,
getConnection,
2019-08-14 16:25:23 +05:30
getConnectionManager,
createConnections
2019-07-16 11:47:19 +05:30
} from "typeorm";
import { resolve } from "path";
import {
BlockchainBlockController,
BlockchainInputController,
BlockchainKernelController,
BlockchainOutputController
} from "./server/controllers";
2019-08-14 16:25:23 +05:30
import {
BlockchainBlock,
BlockchainInput,
BlockchainKernel,
BlockchainOutput
2022-12-23 10:21:39 +00:00
// PeerIp
2019-08-14 16:25:23 +05:30
} from "./server/entities";
2019-08-07 18:20:34 +05:30
import { universalGetLatestBlockDetails } from "./server/socket";
2019-07-16 11:47:19 +05:30
import { dbConfig } from "./server/ormconfig";
import { config } from "dotenv";
2019-09-19 12:10:17 +05:30
import {latestBlockDetails, Details, GetBlocktime, averageblockdifficulty, network_hashrate} from './server/utils';
2019-07-16 11:47:19 +05:30
config({ path: resolve(__dirname, "../.env") });
2019-08-14 16:25:23 +05:30
// const connectionManager = getConnectionManager();
// const connection = connectionManager.create(dbConfig);
2019-07-16 11:47:19 +05:30
import { join } from "path";
2019-08-21 15:33:54 +05:30
var cron = require('node-cron');
2019-07-09 14:52:36 +05:30
2019-06-27 12:18:08 +05:30
// Faster server renders w/ Prod mode (dev mode never needed)
enableProdMode();
// Express server
const app = express();
2019-07-16 11:47:19 +05:30
2022-12-23 10:21:39 +00:00
const PORT = process.env.PORT || 4001;
2019-08-09 11:52:25 +05:30
const DIST_FOLDER = join(process.cwd(), "live");
2019-07-16 11:47:19 +05:30
const controllers = [
new BlockchainBlockController(),
new BlockchainInputController(),
new BlockchainKernelController(),
new BlockchainOutputController()
];
2019-08-08 18:50:20 +05:30
app.use(function(req, res, next) {
2022-12-23 10:21:39 +00:00
// res.header('Access-Control-Allow-Origin', '*');
// res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
// res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With, Accept, Authtoken,cookie_id');
2019-08-09 15:45:17 +05:30
2019-08-08 18:50:20 +05:30
// intercept OPTIONS method
if ('OPTIONS' == req.method) {
return res.status(200).end();
}
else {
next();
}
}
);
2019-07-16 11:47:19 +05:30
app.use(bodyParser.json({ limit: "50mb" }));
2019-07-09 14:52:36 +05:30
app.use(bodyParser.urlencoded({ extended: false }));
2019-07-24 01:14:38 +05:30
app.use(
"/api-docs/**",
express.static(path.join(__dirname, "./server/swagger"))
);
2019-07-16 11:47:19 +05:30
app.get("/swagger.json", function(req, res) {
res.setHeader("Content-Type", "application/json");
2019-07-09 14:52:36 +05:30
res.send(
swaggerJSDoc({
2019-07-16 11:47:19 +05:30
swaggerDefinition: require("./swagger/swagger.json"),
apis: ["**/*.ts"]
})
2019-07-09 14:52:36 +05:30
);
});
2019-06-27 12:18:08 +05:30
2019-08-21 15:33:54 +05:30
2019-07-16 11:47:19 +05:30
controllers.forEach(controller => {
app.use("/epic_explorer/v1", controller.router);
});
2019-09-04 14:47:26 +05:30
2019-07-16 11:47:19 +05:30
// Example Express Rest API endpoints
2019-09-06 13:06:29 +02:00
import request from 'request-promise';
2022-12-23 10:21:39 +00:00
//import { PeerIp } from './server/entities/PeerIp';
2019-09-04 14:47:26 +05:30
2019-09-10 18:38:51 +05:30
app.get("/api", redisMiddleware('60'), async (req, res) => {
2019-09-06 13:06:29 +02:00
try {
let option = req.query.q;
2019-09-04 14:47:26 +05:30
let blockDetails = await latestBlockDetails();
2019-09-06 13:06:29 +02:00
if(option)
{
let result; //500
if(option == "circulating")
// result= Number(blockDetails.coin_existence - blockDetails.totalFoundationReward);
result = Number(blockDetails.coin_existence);
2019-09-18 10:41:58 +05:30
else if(option == "reward")
2020-01-14 10:36:01 +05:30
result= Number(blockDetails.currentReward);
2019-09-06 13:06:29 +02:00
else if(option == "getblockcount")
2020-01-14 10:36:01 +05:30
result= Number(blockDetails.block_height);
2019-09-06 13:06:29 +02:00
else if(option == "getdifficulty-randomx")
result = Number(blockDetails.TotalDifficultyRandomx);
2019-09-06 13:06:29 +02:00
else if(option == "getdifficulty-cuckoo")
result = Number(blockDetails.TotalCuckoo);
2019-09-06 13:06:29 +02:00
else if(option == "getdifficulty-progpow")
result = Number(blockDetails.TotalDifficultyProgpow);
2022-12-23 10:21:39 +00:00
// else if(option == "gettargetdifficulty-randomx")
// result = Number(blockDetails.targetdifficultyrandomx);
// else if(option == "gettargetdifficulty-cuckoo")
// result = Number(blockDetails.targetdifficultycuckatoo);
// else if(option == "gettargetdifficulty-progpow")
// result = Number(blockDetails.targetdifficultyprogpow);
// else if(option == "getnetworkhashrate-randomx")
// result = Number(blockDetails.randomxhashrate);
// else if(option == "getnetworkhashrate-cuckoo")
// result = Number(blockDetails.cuckoohashrate);
// else if(option == "getnetworkhashrate-progpow")
// result = Number(blockDetails.progpowhashrate);
2019-09-06 13:06:29 +02:00
else if(option == "totalcoins")
2020-01-14 10:36:01 +05:30
result = Number(blockDetails.coin_existence);
else if(option == "maxcoins")
2020-01-14 10:36:01 +05:30
result = Number(21000000);
2019-09-18 13:15:27 +05:30
else if(option == "average-blocktime")
{
let data = await averageblockdifficulty();
result = Number(data);
}
2019-09-19 12:10:17 +05:30
else if(option == "network-hashrate-cuckoo")
{
2020-01-10 12:15:45 +05:30
let data = await network_hashrate(blockDetails.block_height,31,blockDetails.targetdifficultycuckatoo,"Cuckoo");
2019-09-19 12:10:17 +05:30
result = data;
}
else if(option == "network-hashrate-progpow")
{
2020-01-10 12:15:45 +05:30
let data = await network_hashrate(blockDetails.block_height,16,blockDetails.targetdifficultyprogpow,"ProgPow");
2019-09-19 12:10:17 +05:30
result = data;
}
else if(option == "network-hashrate-randomx")
{
2020-01-10 12:15:45 +05:30
let data = await network_hashrate(blockDetails.block_height,16,blockDetails.targetdifficultyrandomx,"RandomX");
2019-09-19 12:10:17 +05:30
result = data;
}
2019-09-06 13:06:29 +02:00
else if(option == "getblockhash")
{
let height = req.query.height;
if(height) {
2019-09-06 18:52:10 +05:30
let heighthash = await Details(height);
if(heighthash[0].hash)
result = heighthash[0].hash;
else
result = 'Invalid height';
} else if(height <0) {
result = '"height" parameter missing or invalid';
2019-09-06 13:06:29 +02:00
} else {
result = '"height" parameter missing or invalid';
}
2019-09-06 18:52:10 +05:30
}
else if(option == "getblockheight")
{
let hash = req.query.hash;
if(hash) {
let heighthash = await Details(hash);
if(heighthash[0].height)
result = heighthash[0].height;
else
result = 'Invalid hash';
} else {
result = '"hash" parameter missing or invalid';
}
2019-09-06 13:06:29 +02:00
}
2019-09-06 19:19:43 +05:30
else if(option == "getblocktime"){
let height = req.query.height;
if(height) {
let blockTime = await GetBlocktime(height);
if(blockTime[0].alter)
result = blockTime[0].alter;
else
result = 'Invalid height';
} else if(height < 0) {
result = '"height" parameter missing or invalid';
} else {
result = '"height" parameter missing or invalid';
}
}
2019-09-06 13:06:29 +02:00
else if(option == "info")
{
2019-09-06 18:52:10 +05:30
result = {
"Name": "Epic Cash",
"Symbol": "EPIC",
"TotalSupply": 21000000,
"CurrentSupply": blockDetails.coin_existence,
"MinerCurrentBlockReward": blockDetails.userReward,
"Algorithms": "Cuckoo, RandomX, ProgPow",
"Target_Difficulty": {
"Cuckoo": blockDetails.targetdifficultycuckaroo + blockDetails.targetdifficultycuckatoo,
"RandomX": blockDetails.targetdifficultyrandomx,
"ProgPow": blockDetails.targetdifficultyprogpow
},
"Total_Difficulty": {
"Cuckoo": blockDetails.TotalCuckoo,
"RandomX": blockDetails.TotalDifficultyRandomx,
"ProgPow": blockDetails.TotalDifficultyProgpow
},
"BlockHeight": blockDetails.block_height,
"Blockchain": "MimbleWimble",
"Homepage": "https://epic.tech",
"Explorer": "https://explorer.epic.tech",
"API": "https://explorer.epic.tech/api",
"Logo": "https://explorer.epic.tech/assets/img/logo.png",
"ICO": "NO",
"Premine": "NO",
"Mainnet": "YES",
"Genesis": "09-03-2019, 02:09:00 UTC",
"BlockInterval": 60,
"GIT": "https://gitlab.com/epiccash",
"Whitepaper":"https://epic.tech/whitepaper",
"Colors": { "off-white": "#f3f4f2", "off-black": "#222223", "gold" :"#bf9b30" }
};
}
2019-09-06 13:06:29 +02:00
else
result= "Invalid Option";
var type = typeof result;
2019-09-06 18:52:10 +05:30
2019-09-06 13:06:29 +02:00
if (type == "object") {
res.status(200).json({...result});
}
else {
2019-09-06 18:52:10 +05:30
res.status(200).json(result);
2019-09-06 13:06:29 +02:00
}
}
else
{
2019-09-06 18:52:10 +05:30
//let result= 5;
2019-09-11 12:02:48 +02:00
res.redirect('/api-index');
2019-09-06 13:06:29 +02:00
}
}
catch(err){
2019-09-06 18:52:10 +05:30
res.status(500).send("Internal Server Error");
2019-09-06 13:06:29 +02:00
}
2019-09-04 14:47:26 +05:30
});
2019-09-06 13:06:29 +02:00
2019-07-24 01:14:38 +05:30
app.get("/epic_explorer/v1/**", (req, res) => {
res.send({ msg: "Api works." });
});
2019-08-09 15:45:17 +05:30
app.use(errorMiddleware);
2019-06-27 12:18:08 +05:30
// * NOTE :: leave this as require() since this file is built Dynamically from webpack
2019-07-16 11:47:19 +05:30
const { AppServerModuleNgFactory, LAZY_MODULE_MAP } = require("./server/main");
2019-06-27 12:18:08 +05:30
// Our Universal express-engine (found @ https://github.com/angular/universal/tree/master/modules/express-engine)
2019-07-16 11:47:19 +05:30
app.engine(
"html",
ngExpressEngine({
bootstrap: AppServerModuleNgFactory,
providers: [provideModuleMap(LAZY_MODULE_MAP)]
})
);
2019-06-27 12:18:08 +05:30
2019-07-16 11:47:19 +05:30
app.set("views", join(DIST_FOLDER, "browser"));
2019-07-24 01:14:38 +05:30
app.set("view engine", "html");
2019-06-27 12:18:08 +05:30
// Server static files from /browser
2019-07-16 11:47:19 +05:30
app.get(
"*.*",
express.static(join(DIST_FOLDER, "browser"), {
maxAge: "1y"
})
);
2019-06-27 12:18:08 +05:30
// All regular routes use the Universal engine
2019-07-16 11:47:19 +05:30
app.get("*", (req, res) => {
2019-07-24 01:14:38 +05:30
res.sendFile(join(DIST_FOLDER, "browser") + "/index.html", { req });
2019-06-27 12:18:08 +05:30
});
// Start up the Node server
2019-08-14 16:25:23 +05:30
console.log(__dirname);
// connection
// .connect()
// .then(() => {
2022-12-23 10:21:39 +00:00
createConnections([ {
2019-08-14 16:25:23 +05:30
name: 'Floonet',
type: 'postgres',
host: process.env.FLOONET_DB_HOST,
port: Number(process.env.FLOONET_DB_PORT),
username: process.env.FLOONET_DB_USERNAME,
password: process.env.FLOONET_DB_PASSWORD,
database: process.env.FLOONET_DB_DATABASE,
synchronize: false,
logging: false,
entities: [BlockchainBlock,
BlockchainInput,
BlockchainKernel,
BlockchainOutput],
}, {
name: 'Testnet',
type: 'postgres',
host: process.env.TESTNET_DB_HOST,
port: Number(process.env.TESTNET_DB_PORT),
username: process.env.TESTNET_DB_USERNAME,
password: process.env.TESTNET_DB_PASSWORD,
database: process.env.TESTNET_DB_DATABASE,
synchronize: false,
logging: false,
entities: [BlockchainBlock,
BlockchainInput,
BlockchainKernel,
BlockchainOutput],
}]).then(async () => {
2022-12-23 10:21:39 +00:00
2019-08-07 18:20:34 +05:30
const server = app.listen(PORT, () => {
2019-07-16 11:47:19 +05:30
console.log(`Node Express server listening on http://localhost:${PORT}`);
});
2019-08-21 15:33:54 +05:30
2020-01-13 12:53:43 +05:30
// cron.schedule('*/30 * * * * *', () => {
// universalGetLatestBlockDetails('Testnet');
// });
2019-08-22 18:58:32 +05:30
var interval;
2019-08-07 18:20:34 +05:30
const io = require("socket.io").listen(server);
io.sockets.on("connection", socket => {
2019-08-22 18:58:32 +05:30
// if (interval) {
// clearInterval(interval);
// }
2019-08-21 15:33:54 +05:30
//console.log(socket.handshake.query.network);
//var network = "Testnet";
let key = process.env.REDIS_KEY + socket.handshake.query.network + 'Latest_Block_details'
2019-08-22 18:58:32 +05:30
interval = setInterval(function() {
2019-08-21 15:33:54 +05:30
Global.client.get(key, function(err, reply){
2020-01-13 12:53:43 +05:30
// socket.emit("latestblockdetail", JSON.parse(reply) );
2019-08-21 15:33:54 +05:30
});
2019-08-22 18:58:32 +05:30
},10000);
2019-08-21 15:33:54 +05:30
//socket.on("disconnect", () => console.log("Client disconnected"));
2019-08-07 18:20:34 +05:30
});
2019-07-16 11:47:19 +05:30
})
.catch(error => {
console.log("connection failed..", error);
});