-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
146 lines (128 loc) · 3.77 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
var async = require("async");
var MASTER_SERVER = require("./master-server");
var authorized = false;
var open = require("open");
var url = require("url");
/*
npm install -g codesmith
codesmith bigbrother
https://www.npmjs.com/package/background-service-runner
*/
//globals here are fine
var STUDENT_PORTAL_LISTENER, snitcher, MASTER_SERVER_URL;
function cleanup(options,err){
if (options.cleanup){
if(snitcher) snitcher.close();
console.log('clean');
}
if (err) console.log(err.stack);
if (options.exit) process.exit();
}
//do something when app is closing
process.on('exit', cleanup.bind(null,{cleanup:true}));
//catches ctrl+c event
process.on('SIGINT', cleanup.bind(null, {exit:true}));
//catches uncaught exceptions
process.on('uncaughtException', cleanup.bind(null, {exit:true}));
async.series([
createStudentPortalListener,
startOurMonitor,
updateProject,
// listenToStartASnitcher
],function(err){
if(err) throw err;
});
function createStudentPortalListener(next){
var express = require("express");
STUDENT_PORTAL_LISTENER = express();
STUDENT_PORTAL_LISTENER.use(function(req,res,next){
console.log(req.url);
res
.set("Access-Control-Allow-Methods: POST GET")
.set("Access-Control-Allow-Origin", "*")
.set("Access-Control-Allow-Headers","Origin, Content-Type");
next();
});
STUDENT_PORTAL_LISTENER.use(express.static(__dirname+"/test_tools"));
STUDENT_PORTAL_LISTENER.use(require('body-parser')());
STUDENT_PORTAL_LISTENER.post("/send-help-request",function(req,res,next){
require("./help-request/route")(snitcher,req,res,next);
});
var t = setTimeout(function(){
require("./Abstract/scouter")(function(text){
return text === "yes I am";
},8123,1,254,function(e,ips){
if(e) throw e;
stdms = ips[0]+":8123";
open("http://"+stdms);
});
},10*1000);
STUDENT_PORTAL_LISTENER.post("/token",function(req,res){
console.log("post token");
console.log(req.body.token);
if(t){
clearTimeout(t);
t = false;
}
if(!req.body.href){
return res.status().end();
}
MASTER_SERVER_URL = "http://"+req.body.user+":"+req.body.token+"@"+url.parse(req.body.href).host;
MASTER_SERVER_URL = url.parse(MASTER_SERVER_URL);
console.log(MASTER_SERVER_URL);
MASTER_SERVER.authorize(MASTER_SERVER_URL.href);
res.status(200).end();
next();
});
STUDENT_PORTAL_LISTENER.use(function(err,req,res,next){
if(err) console.error(err.stack);
next();
});
STUDENT_PORTAL_LISTENER.listen(8001);
}
/*
TODO: Handle Branch changes
*/
function startOurMonitor(next){
console.log("starting out monitor");
var cp = require("child_process");
var PT = require("stream").PassThrough;
snitcher = new (require("./snitcher"))(process.cwd());
/*
TODO: This is getting freaking ugly
*/
snitcher.on("new-project",function(subject,head,root,next){
console.log("creating project");
MASTER_SERVER.createProject(subject,root,next);
});
snitcher.on("commit",function(subject,head,message,root){
/*
testRunner(self.path,function(err,test_res){
TODO: Reimplement tests when all tests can be run with "npm test"
*/
MASTER_SERVER.sendCommit(subject,head,message,root,function(e,res){
if(e) throw e;
console.log("success");
});
});
snitcher.on("fsdiff",function(subject,type,path,root){
/*
var testRunner = require("./snitcher/testrunner");
testRunner(self.path,function(err,test_res){
TODO: Reimplement tests when all tests can be run with "npm test"
*/
MASTER_SERVER.sendFSDiff(subject,type,path,root,function(e,res){
if(e) throw e;
console.log("success");
});
MASTER_SERVER.updateProject(snitcher.getId(),root,function(e,res){
if(e) throw e;
console.log("success");
});
});
snitcher.start(next);
}
function updateProject(next){
console.log("updating project");
MASTER_SERVER.updateProject(snitcher.getId(),snitcher.path,next);
}