-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
252 lines (204 loc) · 5.89 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
var express = require('express');
var bodyParser = require('body-parser');
var logger = require('morgan');
var Promise =require('bluebird')
var schedule = require('node-schedule')
var pathFn = require('path');
var mkdirp = require('mkdirp');
var queuefun = require('queue-fun')
var config = require('./config.js');
var judge = require('./lib/judge.js')
var myfs = require('./lib/fs.js')
var fs = require('fs');
var shortid= require('shortid');
var app = express();
/* ------lib---- */
var unzip = require('./lib/zip.js').unzip;
/* global function */
var join = pathFn.join;
var _Queue = queuefun.Queue(Promise);
var Queue = new _Queue(100);
app.use(logger('dev'));
app.use(bodyParser.json({limit: '50mb'})); // 限制上传5M
app.use(bodyParser.urlencoded({ extended: false , limit: '50mb' }));
/* judger server version */
app.get('/ping',function(req,res){
res.json({
Judger_version: config.version
});
})
/* 传方件 */
/* 头文件设置好type 和 uid 两个字段 */
app.post('/tmpfile',function(req,res){
var type = req.headers.type;
var uid = req.headers.uid;
var path = join(config['tmp_prefix'],uid);
mkdirp.sync(path);
var filePath = '';
if(type =='.zip')
filePath = join( path,'data.zip');
else{
res.json({
state:'fail',
msg:'do not support this type'
})
}
var w = fs.createWriteStream(filePath);
req.pipe(w);
req.on('end',function(){
//解压zip
if(type == '.zip'){
//var date = new Date(Date.now+30*60*1000);
//var j = schedule.scheduleJob( date,function(){
//if( fs.existsSync(path)){
//myfs.rmdir(path);
//console.log('删除超时文件夹: '+ path);
//}
//})
//return unzip(filePath,path)
//.then(function(){
//fs.unlink(filePath);
//res.json({
//state:'success',
//uid:uid
//});
//})
res.json({
state:'success',
uid:uid
})
}
else
res.json({
state:'fail',
uid:uid
});
})
})
app.post('/file',function(req,res){
var type = req.headers.type;
var uid = req.headers.uid;
var path = join(config['prefix'],uid);
mkdirp.sync(path);
var filePath = '';
if(type =='.zip')
filePath = join( path,'data.zip');
else{
res.json({
state:'fail',
msg:'do not support this type'
})
}
var w = fs.createWriteStream(filePath);
req.pipe(w);
req.on('end',function(){
//解压zip
if(type == '.zip'){
res.json({
state:'success',
uid:uid
});
}
else
res.json({
state:'success',
uid:uid
});
})
})
/* judger */
app.post('/judge',function(req,res){
var body = req.body; //得到请求参数
var judgerConfig = {};
var uid=''; //data数据文件
var tmp_uid='';
var output = false;
var language = '';
function returnError(res,arg){
res.json({
state:'fail',
msg:'need '+ arg +' args'
})
}
if( body.uid )
uid = body.uid;
else
returnError(res,'uid');
if( body.max_cpu_time)
judgerConfig.max_cpu_time = body.max_cpu_time;
else
returnError(res,'max_cpu_time');
if(body.max_memory)
judgerConfig.max_memory = body.max_memory;
else
returnError(res,'max_memory');
if(body.tmp_uid)
tmp_uid = body.tmp_uid;
else
returnError(res,'tmp_uid');
if(body.language)
language= body.language;
else
returnError(res,'language');
if(body.output)
output=true;
var data_path = '';
var code_path = '';
var out_path = '';
data_path = join(config.tmp_prefix,tmp_uid);
code_path = join(config.tmp_prefix,tmp_uid,'main.'+language);
out_path = join(config.tmp_prefix,tmp_uid,'out');
mkdirp.sync(out_path);
fs.chownSync(data_path,config.compiler_uid,config.compiler_gid);
function do_judger(c_path,s_src){
var d_path = '';
return myfs.writeFile(c_path,s_src) //写入代码代码
.then(function(){ //如果tmp_uid !== uid 那么 复制->解压 else 直接解压
if(tmp_uid !== uid){
var s_path = join(config.prefix,uid,'data.zip');
var d_path = join(config.tmp_prefix,tmp_uid,'data.zip');
return myfs.copyFile(s_path,d_path);
}
})
.then(function(){//解压
d_path = join(config.tmp_prefix,tmp_uid,'data.zip');
return unzip(d_path,data_path)
})
.then(function(){
fs.unlink(d_path)
return judge(data_path,out_path,code_path,language,judgerConfig,output)
})
.then(function(data){
res.json(data)
})
}
Queue.go(do_judger,[code_path,body.src])
})
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.json({
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.json({
message: err.message,
error: {}
});
});
module.exports = app;