forked from justint/cs160-facedetection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.js
executable file
·278 lines (229 loc) · 7.37 KB
/
Server.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/* jshint esversion: 6 */
// Import modules
var express = require("express");
var session = require('express-session');
var multer = require("multer");
var bodyParser = require('body-parser');
var repl = require("repl");
var passport = require('passport');
var flash = require('connect-flash');
var configAuth = require('./config/auth');
const pg = require('pg');
const connectionString = process.env.DATABASE_URL || configAuth.database.url;
// Import our code
var Job = require("./lib/Job.js");
// Set up router, upload config
var router = express.Router();
var path = __dirname + '/views/';
var upload = multer({ dest: 'uploads/' });
var app = express();
console.log("Initializing server...");
// Initialize empty list of jobs
var jobList = {};
require('./config/passport')(passport); // pass passport for configuration
// Enable body-parser for JSON parsing
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(session({
secret: '<session secret>',
resave: true,
saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // use connect-flash for flash messages stored in session
require('./lib/routes.js')(app, passport, express); // load our routes and pass in our app and fully configured passport
router.use(function (req,res,next) {
console.log("/" + req.method);
next();
clearShell();
});
// Handle when user starts a job
app.post('/start-job', function (req, res){
console.log("Job " + req.body['job-number'] + " starting...");
//console.log(req.body);
//Start job pipeline
console.log("starting this job: " + JSON.stringify(jobList["" + req.body['job-number']]));
jobList["" + req.body['job-number']].execute( req.body['job-number'], jobList["" + req.body['job-number']].jobData );
updateJobStatus(req.body['ownerid'], req.body['job-number'], 1);
// Send success status to client
res.sendStatus(200);
});
// Handle when user creates a job
app.post('/create-job', upload.single('video-file'), function (req, res, next) {
console.log("Incoming job:");
console.log(req.body);
console.log(req.file);
if (!req.file.mimetype.match(/video\/./))
{
console.log("Bad file type, returning 415 error");
res.status(415).send({ error: "errorFiletype" });
}
else
{
// Create new job, add to job list
//TODO: pass in actual job data
var newJob = new Job(req.body['job-number'], [req.body, req.file]);
var status = 0;
jobList["" + req.body['job-number']] = newJob;
insertdb(
req.body['ownerid'],
req.body['cv-implementation'],
req.file['fieldname'],
req.body['job-number'],
req.file['originalname'],
req.file['encoding'],
req.file['mimetype'],
req.file['destination'],
req.file['filename'],
req.file['path'],
req.file['size'],
3
);
// Send success status to client
res.status(200).send();
}
clearShell();
});
// Get a list of jobs for a user
app.post('/getjobs', (req, res, next) => {
const results = [];
// Get a Postgres client from the connection pool
pg.connect(connectionString, (err, client, done) => {
// Handle connection errors
if(err) {
done();
console.log(err);
return res.status(500).json({success: false, data: err});
}
var ownerid = "" + req.body.ownerid;
const query = client.query("SELECT * FROM jobs WHERE ownerid=($1)",
[ownerid]);
//const query = client.query('SELECT * FROM jobs WHERE ownerid=' + req.body.ownerid + ';');
// Stream results back one row at a time
query.on('row', (row) => {
console.log("row!");
results.push(row);
});
// After all data is returned, close connection and return results
query.on('end', () => {
done();
return res.json(results);
});
});
});
//update a job's status
function updateJobStatus(ownerid, jobnumber, status) {
// Get a Postgres client from the connection pool
pg.connect(connectionString, (err, client, done) => {
// Handle connection errors
if(err) {
done();
console.log(err);
}
// SQL Query > Update Data
client.query('UPDATE jobs SET status=($3) WHERE ownerid=($1) AND jobnumber=($2)',
[ownerid, jobnumber, status]);
});
}
/*
returns a list of jobs for a user
*/
function getJobs(ownerid) {
var results = [];
// Get a Postgres client from the connection pool
pg.connect(connectionString, (err, client, done) => {
// Handle connection errors
if(err) {
done();
console.log(err);
}
// SQL Query > Select Data
const query = client.query('SELECT * FROM users WHERE ownerid=($1)', [ownerid]);
// Stream results back one row at a time
query.on('row', (row) => {
console.log("row: " + row);
results.push(row);
});
// After all data is returned, close connection and return results
query.on('end', () => {
done();
console.log("return results");
return results;
});
});
}
/*
inserts a job into database
*/
function insertdb(ownerid, cvimplementation, fieldname, jobnumber, originalname, encoding, mimetype, destination, filename, path, size, status) {
pg.connect(connectionString, (err, client, done) => {
// Handle connection errors
if(err) {
done();
console.log(err);
}
// SQL Query > Insert Data
client.query('INSERT INTO jobs(ownerid, cvimplementation, fieldname,' +
' jobnumber, originalname, encoding, mimetype, destination, filename, path, size, status)' +
' values($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)',
[ownerid, cvimplementation, fieldname, jobnumber, originalname, encoding, mimetype, destination, filename, path, size, status]);
});
}
// Client ping handler
app.post("/job-status", function(req, res){
console.log("Incoming client ping type " + req.body.pingType + " for Job " + req.body.jobNum);
switch(parseInt(req.body.pingType))
{
case 0: // Upload status
{
if (jobList["" + req.body.jobNum] !== null)
{
console.log("File upload complete, adding to client job list...");
res.status(200).send(req.body.jobNum);
}
break;
}
case 1: // Processing status
{
if (jobList["" + req.body.jobNum].complete) {
console.log("Sending job complete status...");
var jobFinishPath = "/download/" + jobList["" + req.body.jobNum].jobData[1].filename;
res.send(jobFinishPath);
}
else res.send(false);
break;
}
default: // Improper ping type
{
break;
}
}
clearShell();
});
// Serve static content
app.use('/css', express.static(__dirname+'/css'));
app.use('/js', express.static(__dirname+'/js'));
app.use('/img', express.static(__dirname+'/img'));
app.use('/templates', express.static(__dirname+'/views/templates'));
app.use("/", router);
// Handle 404 errors
app.use("*", function(req, res) {
res.sendFile(path + "404.html");
});
app.listen(3000, function() {
console.log("Live at Port 3000");
console.log("");
// Initialize shell
var replServer = repl.start("160-pipeline> ");
// Make jobList accessible to shell
replServer.context.jobList = jobList;
// Handle Cntl-C quits
replServer.on('exit', safeShutdown);
});
function clearShell() {
process.stdout.write("160-pipeline> ");
}
function safeShutdown() {
process.exit();
}