1
1
import fs from 'fs' ;
2
2
import path from "path" ;
3
3
import crypto from "crypto" ;
4
-
5
- import {
6
- getMinerPath ,
7
- getMinerFile ,
8
- } from "../App/ConfigUnpacker.js" ;
4
+ import { getForeignMiner } from "./RequestHandlers.js" ;
5
+ import { initSingleVenv , getVenvStatusDeleteIfDone } from "../App/PyVenvHelper.js" ;
6
+ import { getMinerPath , getMinerFile } from "../App/ConfigUnpacker.js" ;
9
7
import {
10
8
stopProcess ,
11
9
getStatusDeleteIfDone ,
12
10
getProcessStatusList ,
13
11
processStart ,
14
12
} from "../App/Wrapper.js" ;
15
- import {
16
- getForeignMiner ,
17
- } from "./RequestHandlers.js" ;
18
- import {
19
- initSingleVenv ,
20
- getVenvStatusDeleteIfDone ,
21
- } from "../App/PyVenvHelper.js" ;
22
13
23
14
const port = 5000 ; // The host port express will listen on.
24
15
25
16
export function initEndpoints ( app , configList ) {
26
- app . get ( "/" , function ( req , res ) {
17
+
18
+ app . get ( "/" , function ( req , res ) {
27
19
res . send ( "Default page" ) ;
28
20
} ) ;
29
- app . get ( `/ping` , function ( req , res ) {
21
+
22
+ app . get ( `/ping` , function ( req , res ) { // return pong
30
23
res . send ( "pong" ) ;
31
24
} ) ;
32
- app . get ( `/configurations` , function ( req , res ) {
25
+
26
+ app . get ( `/configurations` , function ( req , res ) { // return all configurations
33
27
res . send ( configList ) ;
34
28
} ) ;
35
- // Endpoint to return algorithm file that needs to be shadowed.
36
- app . get ( `/shadow/:minerId` , function ( req , res ) {
29
+
30
+ app . get ( `/shadow/:minerId` , function ( req , res ) { // return algorthm file of a miner
37
31
const minerId = req . params . minerId ;
38
32
console . log ( `Getting a request on /shadow/${ minerId } ` ) ;
39
33
let requestedConfig = configList . find ( miner => miner . MinerId == minerId ) ;
@@ -52,37 +46,29 @@ export function initEndpoints(app, configList) {
52
46
return ;
53
47
}
54
48
55
- // else, all is good, return file
56
- res . setHeader ( 'Content-disposition' , 'attachment; filename=shadow-miner' ) ;
57
-
58
- // TODO: Consider if these are necessary? Leave them out for now, since we're testing with a .py script.
59
- // res.setHeader('Content-type', 'application/x-msdownload'); //for exe file
60
- // res.setHeader('Content-type', 'application/x-rar-compressed'); //for rar file
49
+ // else, no issues, return file
50
+ res . setHeader ( 'Content-disposition' , 'attachment; filename=shadow-miner' ) ;
61
51
var file = fs . createReadStream ( pathToFile ) ;
62
- file . pipe ( res ) ; //send file
52
+ file . pipe ( res ) ;
63
53
} ) ;
64
- // Endpoint to return requirements file that needs to be shadowed.
65
- app . get ( `/shadow/requirements/:minerId` , function ( req , res ) {
54
+
55
+ app . get ( `/shadow/requirements/:minerId` , function ( req , res ) { // Return requirements file of a miner
66
56
console . log ( `Getting a request on /shadow/requirements/${ req . params . minerId } ` ) ;
67
57
let requestedConfig = configList . find ( miner => miner . MinerId == req . params . minerId ) ;
68
58
if ( ! requestedConfig . Shadow ) res . status ( 400 ) . send ( `Invalid request, cannot shadow Miner with id \"${ requestedConfig . MinerId } \" and label: \"${ requestedConfig . MinerLabel } \".` ) ;
69
59
else {
70
60
res . setHeader ( 'Content-disposition' , 'attachment; filename=shadow-miner' ) ;
71
-
72
- // TODO: Consider if these are necessary? Leave them out for now, since we're testing with a .py script.
73
- // res.setHeader('Content-type', 'application/x-msdownload'); //for exe file
74
- // res.setHeader('Content-type', 'application/x-rar-compressed'); //for rar file
75
- const pathToFile = path . join ( getMinerPath ( requestedConfig ) , "requirements.txt" ) ; // TODO: Name of the file shouldn't just be hardcoded in here.
61
+ const pathToFile = path . join ( getMinerPath ( requestedConfig ) , "requirements.txt" ) ;
76
62
if ( ! fs . existsSync ( pathToFile ) ) res . status ( 404 ) . send ( `Unable to find requirements file for requested miner.` ) ;
77
63
else {
78
64
var file = fs . createReadStream ( pathToFile ) ;
79
- file . pipe ( res ) ; //send file
65
+ file . pipe ( res ) ;
80
66
}
81
67
}
82
68
} ) ;
83
- // Initiate shadow process - request foreign miner on "shadow/:minerId" to get the foreign miner .exe/script
84
- app . post ( `/shadow` , async function ( req , res ) {
85
- console . log ( `Getting a request on /shadow` ) ;
69
+
70
+ app . post ( `/shadow` , async function ( req , res ) { // Initiate cloning process: request foreign miner on "shadow/:minerId" to get the foreign miner .exe/script
71
+ console . log ( `\nGetting a request on /shadow` ) ;
86
72
87
73
const venvInitId = crypto . randomUUID ( ) ;
88
74
let body = await req . body ;
@@ -98,55 +84,54 @@ export function initEndpoints(app, configList) {
98
84
} ) ;
99
85
} ) ;
100
86
101
- app . get ( `/shadow/status/:venvInitId` , async function ( req , res ) {
87
+
88
+ app . get ( `/shadow/status/:venvInitId` , async function ( req , res ) { // Return status of cloning action
102
89
let venvInitId = req . params . venvInitId ;
103
90
console . log ( `Getting a request on /shadow/status for id ${ venvInitId } ` ) ;
104
91
let venvStatus = await getVenvStatusDeleteIfDone ( venvInitId ) ;
105
92
if ( venvStatus ) res . status ( 200 ) . send ( venvStatus ) ;
106
93
else res . status ( 400 ) . send ( `No process exists with ID: ${ venvInitId } ` ) ;
107
94
} ) ;
108
95
96
+ app . post ( `/miner` , async function ( req , res ) { // Start miner - return process id.
97
+ console . log ( "Received POST request on /miner" ) ;
98
+ function sendProcessId ( processId , error ) {
99
+ if ( error ) {
100
+ console . error ( "Error: " + error ) ;
101
+ res . status ( 400 ) . send ( error ) ;
102
+ }
103
+ else {
104
+ res . send ( processId . toString ( ) ) ;
105
+ }
106
+ }
107
+ const body = await req . body ;
108
+ const ownUrl = req . protocol + '://' + req . get ( 'host' ) + req . originalUrl ;
109
+ processStart ( sendProcessId , body , ownUrl , configList ) ;
110
+ } ) ;
109
111
110
-
111
- app . get ( `/status` , async function ( req , res ) {
112
+ app . get ( `/status` , async function ( req , res ) { // Return status of all active miners
112
113
console . log ( `Getting a request on /status for status list` ) ;
113
114
res . status ( 200 ) . send ( getProcessStatusList ( ) ) ;
114
115
} ) ;
115
116
116
- app . get ( `/status/:processId` , async function ( req , res ) {
117
+ app . get ( `/status/:processId` , async function ( req , res ) { // Return status of specific miner
117
118
let processId = req . params . processId ;
118
119
// console.log(`Getting a request on /status for id ${processId}`);
119
120
let statusDict = await getStatusDeleteIfDone ( processId ) ;
120
121
if ( statusDict ) res . status ( 200 ) . send ( statusDict ) ;
121
122
else res . status ( 400 ) . send ( `No process exists with ID: ${ processId } ` ) ;
122
123
} ) ;
123
124
124
- app . delete ( `/stop/:processId` , async function ( req , res ) {
125
+ app . delete ( `/stop/:processId` , async function ( req , res ) { // Stop miner - return confirmation.
125
126
let processId = req . params . processId
126
127
console . log ( `Getting a request on /stop for id ${ processId } ` ) ;
127
128
let result = await stopProcess ( processId ) ;
128
129
if ( result ) res . status ( 200 ) . send ( `Killed process with ID: ${ processId } ` ) ;
129
130
else res . status ( 400 ) . send ( `No active process with ID: ${ processId } ` ) ;
130
131
} ) ;
131
132
132
- app . post ( `/miner` , async function ( req , res ) {
133
- console . log ( "Received POST request on /miner" ) ;
134
- function sendProcessId ( processId , error ) {
135
- if ( error ) {
136
- console . error ( "Error: " + error ) ;
137
- res . status ( 400 ) . send ( error ) ;
138
- }
139
- else {
140
- // console.log(`Sending processId ${processId}`);
141
- res . send ( processId . toString ( ) ) ;
142
- }
143
- }
144
- const body = await req . body ;
145
- const ownUrl = req . protocol + '://' + req . get ( 'host' ) + req . originalUrl ;
146
- processStart ( sendProcessId , body , ownUrl , configList ) ;
147
- } ) ;
148
133
149
- app . listen ( port , '0.0.0.0' , ( ) => {
134
+ app . listen ( port , '0.0.0.0' , ( ) => { // Start listening on port {port}
150
135
console . log ( `Example app listening on port ${ port } ` ) ;
151
136
} ) ;
152
137
}
0 commit comments