1
+ const express = require ( 'express' )
2
+ const init = node => {
3
+ const getSub = async ( req , res ) => {
4
+ try {
5
+ console . log ( req . params . id ) ;
6
+ console . log ( req . body ) ;
7
+ req . body . rootKey . publicKey = Buffer . from ( Object . values ( req . body . rootKey . publicKey ) ) ;
8
+ req . body . rootKey . secretKey = Buffer . from ( Object . values ( req . body . rootKey . secretKey ) ) ;
9
+ const output = node . getSub ( req . body . rootKey , req . params . id ) ;
10
+ if ( typeof output == "object" ) res . write ( JSON . stringify ( output ) ) ;
11
+ else if ( typeof output == "string" ) res . write ( output ) ;
12
+ res . status ( 200 ) . end ( )
13
+ } catch ( err ) {
14
+ console . log ( err )
15
+ res . write ( JSON . stringify ( err ) ) ;
16
+ res . status ( 500 ) . end ( )
17
+ }
18
+ } ;
19
+ const findNodes = async ( req , res ) => {
20
+ try {
21
+ console . log ( req . params . id )
22
+ console . log ( req . body )
23
+ req . body . key . publicKey = Buffer . from ( Object . values ( req . body . key . publicKey . data ) )
24
+ console . log ( req . body . key . publicKey . toString ( 'hex' ) )
25
+ const results = await node . lookup ( req . body . key . publicKey . toString ( 'hex' ) )
26
+ const output = [ ] ;
27
+ for ( remote of results ) {
28
+ for ( peer of remote . peers ) {
29
+ const hex = peer . publicKey . toString ( 'hex' ) ;
30
+ if ( ! output . includes ( hex ) ) {
31
+ output . push ( hex )
32
+ }
33
+ }
34
+ }
35
+
36
+ console . log ( 'output' , output )
37
+ if ( typeof output == "object" ) res . write ( JSON . stringify ( output ) )
38
+ else if ( typeof output == "string" ) res . write ( output )
39
+ res . status ( 200 ) . end ( )
40
+ } catch ( err ) {
41
+ console . log ( err )
42
+ res . write ( JSON . stringify ( err ) ) ;
43
+ res . status ( 500 ) . end ( )
44
+ }
45
+ } ;
46
+ const startNode = ( req , res ) => {
47
+ try {
48
+ req . body . hostKey . publicKey = Buffer . from ( Object . values ( req . body . hostKey . publicKey ) ) ;
49
+ req . body . hostKey . secretKey = Buffer . from ( Object . values ( req . body . hostKey . secretKey ) ) ;
50
+ const output = node . run ( sub , "startNode" , {
51
+ name : req . params . name
52
+ } ) ;
53
+
54
+ if ( typeof output == "object" ) res . write ( JSON . stringify ( output ) ) ;
55
+ else if ( typeof output == "string" ) res . write ( output ) ;
56
+ res . status ( 200 ) . end ( )
57
+ } catch ( err ) {
58
+ console . log ( err )
59
+ res . write ( JSON . stringify ( err ) ) ;
60
+ res . status ( 500 ) . end ( )
61
+ }
62
+ } ;
63
+ const router = express . Router ( ) ;
64
+ router . get ( "/callNode/:name" , startNode ) ;
65
+ router . post ( "/findNodes/:name" , findNodes ) ;
66
+ router . post ( "/getSub/:id" , getSub ) ;
67
+ return router
68
+ } ;
69
+ module . exports = init ;
0 commit comments