1
- import fs from "fs" ;
2
- import path from " path" ;
3
- import util from " util" ;
4
- import { execFile , spawn , exec } from " child_process" ;
5
- import ValidationService from " ./validation.service" ;
1
+ import fs from 'fs' ;
2
+ import path from ' path' ;
3
+ import util from ' util' ;
4
+ import { execFile , spawn , exec } from ' child_process' ;
5
+ import ValidationService from ' ./validation.service' ;
6
6
const ROOT_DIR = `${ process . cwd ( ) } ` ;
7
- const SOURCE_DIR = path . join ( ROOT_DIR , " executor" ) ;
7
+ const SOURCE_DIR = path . join ( ROOT_DIR , ' executor' ) ;
8
8
const TARGET_DIR = `/app/codes` ;
9
- const IMAGE_NAME = " executor:1.0" ;
10
- const VOL_NAME = `my_vol` ;
11
- // const VOL_NAME = SOURCE_DIR;
9
+ const IMAGE_NAME = ' executor:1.0' ;
10
+ // const VOL_NAME = `my_vol`;
11
+ const VOL_NAME = SOURCE_DIR ;
12
12
13
13
class CodeService {
14
14
async execute ( code , input , lang , id ) {
15
15
try {
16
- ! input ? ( input = "" ) : null ;
16
+ ! input ? ( input = '' ) : null ;
17
17
18
- //validating code
18
+ // validating code
19
19
// await this.validateCode(code, input, lang, id);
20
20
const { isValid, message } = await ValidationService . execute (
21
21
code ,
@@ -25,7 +25,7 @@ class CodeService {
25
25
) ;
26
26
if ( ! isValid ) {
27
27
throw {
28
- message,
28
+ message
29
29
} ;
30
30
}
31
31
@@ -61,20 +61,24 @@ class CodeService {
61
61
async writeFile ( code , lang , input , id ) {
62
62
let fileName = `${ id } code` ;
63
63
switch ( lang ) {
64
- case " javascript" : {
65
- fileName += " .js" ;
64
+ case ' javascript' : {
65
+ fileName += ' .js' ;
66
66
break ;
67
67
}
68
- case " c++" : {
69
- fileName += " .cpp" ;
68
+ case ' c++' : {
69
+ fileName += ' .cpp' ;
70
70
break ;
71
71
}
72
- case "python" : {
73
- fileName += ".py" ;
72
+ case 'python' : {
73
+ fileName += '.py' ;
74
+ break ;
75
+ }
76
+ case 'java' : {
77
+ fileName += '.java' ;
74
78
break ;
75
79
}
76
80
default : {
77
- throw { message : " Invalid language" } ;
81
+ throw { message : ' Invalid language' } ;
78
82
}
79
83
}
80
84
const write = util . promisify ( fs . writeFile ) ;
@@ -84,30 +88,34 @@ class CodeService {
84
88
await write ( path . join ( SOURCE_DIR , `${ id } input.txt` ) , input ) ;
85
89
return {
86
90
file : fileName ,
87
- inputFile : `${ id } input.txt` ,
91
+ inputFile : `${ id } input.txt`
88
92
} ;
89
93
} catch ( error ) {
90
94
throw { message : error } ;
91
95
}
92
96
}
93
97
94
98
async writeCommand ( lang , file , input , id ) {
95
- let command = "" ;
99
+ let command = '' ;
96
100
switch ( lang ) {
97
- case " javascript" : {
101
+ case ' javascript' : {
98
102
command = `cd ${ TARGET_DIR } && node ${ file } < ${ input } ` ;
99
103
break ;
100
104
}
101
- case " c++" : {
105
+ case ' c++' : {
102
106
command = `cd ${ TARGET_DIR } && g++ -o ${ id } ${ file } && ./${ id } < ${ input } ` ;
103
107
break ;
104
108
}
105
- case " python" : {
109
+ case ' python' : {
106
110
command = `cd ${ TARGET_DIR } && python ${ file } < ${ input } ` ;
107
111
break ;
108
112
}
113
+ case 'java' : {
114
+ command = `cd ${ TARGET_DIR } && javac ${ file } && java Input < ${ input } ` ;
115
+ break ;
116
+ }
109
117
default : {
110
- throw { message : " Invalid language" } ;
118
+ throw { message : ' Invalid language' } ;
111
119
}
112
120
}
113
121
@@ -123,10 +131,10 @@ class CodeService {
123
131
async execChild ( runCode , runContainer , id , file , inputFile , lang ) {
124
132
return new Promise ( ( resolve , reject ) => {
125
133
const execCont = exec ( `${ runContainer } ` ) ;
126
- execCont . on ( " error" , ( err ) => {
127
- throw { status : " 404" , message : err } ;
134
+ execCont . on ( ' error' , err => {
135
+ throw { status : ' 404' , message : err } ;
128
136
} ) ;
129
- execCont . stdout . on ( " data" , ( ) => {
137
+ execCont . stdout . on ( ' data' , ( ) => {
130
138
exec ( `${ runCode } ` , async ( error , stdout , stderr ) => {
131
139
await this . endContainer ( id ) ;
132
140
await this . deleteFiles ( file , inputFile , lang , id ) ;
@@ -141,19 +149,24 @@ class CodeService {
141
149
}
142
150
143
151
async deleteFiles ( fileName , inputName , lang , id ) {
144
- fs . unlinkSync ( path . join ( SOURCE_DIR , fileName ) , ( err ) => {
152
+ fs . unlinkSync ( path . join ( SOURCE_DIR , fileName ) , err => {
145
153
if ( err ) throw { message : err } ;
146
154
} ) ;
147
155
if ( inputName ) {
148
- fs . unlinkSync ( path . join ( SOURCE_DIR , inputName ) , ( err ) => {
156
+ fs . unlinkSync ( path . join ( SOURCE_DIR , inputName ) , err => {
149
157
if ( err ) throw { message : err } ;
150
158
} ) ;
151
159
}
152
- if ( lang == " c++" ) {
153
- fs . unlinkSync ( path . join ( SOURCE_DIR , id ) , ( err ) => {
160
+ if ( lang == ' c++' ) {
161
+ fs . unlinkSync ( path . join ( SOURCE_DIR , id ) , err => {
154
162
if ( err ) throw { message : err } ;
155
163
} ) ;
156
164
}
165
+ if ( lang == 'java' ) {
166
+ fs . unlinkSync ( path . join ( SOURCE_DIR , 'Input.class' ) , err => {
167
+ if ( err ) throw err ;
168
+ } ) ;
169
+ }
157
170
}
158
171
159
172
async endContainer ( id ) {
@@ -162,7 +175,7 @@ class CodeService {
162
175
exec ( `${ exit } ` , ( error , stdout , stderr ) => {
163
176
if ( error ) {
164
177
console . log ( error ) ;
165
- } else console . log ( " Container stoped and deleted" ) ;
178
+ } else console . log ( ' Container stoped and deleted' ) ;
166
179
} ) ;
167
180
}
168
181
}
0 commit comments