This repository has been archived by the owner on Apr 1, 2023. It is now read-only.
forked from HandyOSS/HandyHost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HandyAPITemplate.js
119 lines (111 loc) · 2.79 KB
/
HandyAPITemplate.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
import fs from 'fs';
import path from 'path';
import url from 'url';
import {spawn} from 'child_process';
export class HandyAKT{
constructor(){
/*
create any env variables on machine restart. Sia needs them so possibly others..
try{
fs.mkdirSync(`${process.env.HOME}/.HandyHost/aktData`,{recursive:true})
}
catch(e){
//folder already exists
}
let acctName;
try{
acctName = fs.readFileSync(`${process.env.HOME}/.HandyHost/aktData/.nodeEnv`,'utf8');
}
catch(e){}
if(typeof acctName != "undefined"){
process.env.AKT_ACCT_NAME = acctName.replace(/\n/g,'');
//try startup here since we have already inited
}
*/
}
addSocketNamespace(ioNamespace){
//this.io.of('/dvpn')
console.log('init akt sockets');
this.ioNamespace = ioNamespace;
this.ioNamespace.adapter.on("create-room", (room) => {
if(room.indexOf('akt') == 0){
//start a Socket listener for this room
this.initSocketListener(room);
}
});
this.ioNamespace.adapter.on("delete-room", (room) => {
console.log(`room deleted ${room}`);
if(room.indexOf('akt') == 0){
//stop a Socket listener for this room
this.removeSocketListener(room);
}
});
this.ioNamespace.adapter.on("join-room", (room, id) => {
console.log(`socket ${id} has joined room ${room}`);
});
this.ioNamespace.adapter.on("leave-room", (room, id) => {
console.log(`socket ${id} has left room ${room}`);
});
console.log('setup connection events');
this.ioNamespace.on('connection',(socket)=>{
console.log('new connection');
this.addSocketConnection(socket);
});
}
addSocketConnection(socket){
console.log('add socket connection');
socket.emit('register');
socket.on('subscribe',()=>{
console.log('socket did subscribe');
socket.join('akt');
})
}
initSocketListener(room){
//TODO: add when we get more stats on nodes..
/*if(typeof this.socketRoomInterval == "undefined"){
//spin up an interval to send out stats
this.socketRoomInterval = setInterval(()=>{
this.sendSocketUpdates();
},60000);
}*/
}
removeSocketListener(room){
//everybody left the room, kill the update interval
/*clearInterval(this.socketRoomInterval);
delete this.socketRoomInterval;*/
}
sendSocketUpdates(){
/*
this.ioNamespace.to('dvpn').emit('update',{
chain:chainData,
wallet:walletData,
daemon: versionD
});
*/
}
api(path,requestBody,resolve,reject){
switch(`${path[1]}`){
/*
//example: a request for /api/dvpn/getState::
case 'getState':
//check if its installed
this.dvpnSetup.getState().then(data=>{
resolve(data);
}).catch(error=>{
reject(error);
})
break;*/
}
}
parseRequestBody(requestBody){
let parsed;
let err;
try{
parsed = JSON.parse(requestBody);
}
catch(e){
err = e;
}
return {parsed,err};
}
}