forked from Shiba-Community-Dev/shibarium-bridge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
173 lines (128 loc) · 5.06 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Required Modules
const express = require('express')
const app = express();
const server = require('http').createServer(app)
const io = require('socket.io')(server, {cors: {orgin: "*"}})
const open = require('open');
const env = require('dotenv').config()
// Events
const Broadcaster = require('events');
const broadcaster = new Broadcaster();
// Import Additional Required functions
const {folderExist, setupFolders,sleep } = require("./setup/setup");
const {start_geth, init_geth, showProcess} = require("./geth");
const { response } = require('express');
// Server Port Settings
const port = process.env.PORT || 9000;
//App config
let setupflag = 0;
// Server Routes
app.use("/bridge", express.static(__dirname + "/bridge"));
app.use("/setup", express.static(__dirname + "/setup"));
app.use("/@metamask", express.static(__dirname + "/node_modules/@metamask"));
app.use("/bowser", express.static(__dirname + "/node_modules/bowser"));
app.get('/', function(req, res){
res.redirect('/bridge');
});
app.get('/setup', function(req, res){
res.render('index');
});
app.get('/bridge', function(req, res){
res.render('index');
});
// Check for existence of app operational folders
let foldersFound = folderExist(setupFolders);
//run http server
server.listen(port, () => {
console.log(`Server listening at http://localhost:${port}`)
});
if (foldersFound == setupFolders.length) {
open('http://localhost:9000/bridge');
} else {
open('http://localhost:9000/setup');
}
console.log(setupflag + " => " + foldersFound);
function check_setup() {
broadcaster.emit("forHtml", "Start Check Up.....",0);
sleep(2000);
// If the setup folders were found: => Start Geth
if (foldersFound == setupFolders.length && setupflag == 0) {
sleep(2000)
broadcaster.emit("forHtml", "Geth Folders Initialized : pass",1);
broadcaster.emit("forHtml","Attempting to start geth node...",2)
sleep(2000)
//Check if thier is current running Geth process
let isrunning = new Promise((resolve, reject)=> {
let x = showProcess("geth");
if (x) { resolve("true")}
if (!x){ reject("false")}
})
isrunning.then(
function(value){
if (value == "true") {
console.log("Check up determined: Geth is currently running.")}
}, function(error) {
console.log("Check up Determined: There are no running geth processes ");
sleep(2000);
if (setupflag == 0){ startNow();}
})
}else{
setupflag = 1;
sleep(5000)
broadcaster.emit("forHtml","<error missing config folders=> found " + foldersFound + " folders but " + setupFolders.length + " folders Exist",0)
sleep(2000)
broadcaster.emit("forHtml","Will now attempt to initalize geth.... please wait....",0)
sleep(2000)
console.log("Test 3: " +setupflag) //test
let did_init =new Promise((resolve, reject)=> {
let o = init_geth();
console.log("Test 1: " +o) //test
if (o) { resolve("true")}
if (!o){ reject("false")}
})
did_init.then(
function(value){
console.log("Test 2: " +value) //test
if (value === "true") {
if (setupflag == 1) {
setupflag = 0;
console.log("Test 4: " +setupflag) //test
broadcaster.emit("forHtml", "Success!: initalization suceeded!!",0);
sleep(2000);
broadcaster.emit("forHtml", " Select Button Above to Proceed to Bridge",1);
}
}
}, function(error) {
console.log("Check up Determined: Intializing geth failed.. " + error);
sleep(2000);
broadcaster.emit("forHtml", "Initalization Failed...",0)
})
}
}
function startNow() {
let did_start = start_geth();
sleep(5000);
if (did_start) {
showProcess("geth");
sleep(2000);
broadcaster.emit("forHtml", "Success!!.. Geth is currently running",3);
sleep(2000);
broadcaster.emit("forHtml", " ",4);
sleep(2000);
broadcaster.emit("forHtml", " ",5);
}
}
process.on('exit', async () => {});
io.on("connection", (socket) => {
console.log("There is a client connected with id:" + socket.id)
socket.on("run", (method) => { // recieve {method} requested from {run} event emited by client
if (method == "check") {
check_setup();
}
})
// Register Listener for broadcaster responses
broadcaster.on("forHtml", function (responseData,casevalue) {
console.log(responseData);
socket.emit("console", responseData,casevalue);
})
});