-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
111 lines (102 loc) · 3.24 KB
/
index.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
#!/usr/bin/env node;
const Repo = require('./lib/RepoController.js');
const HttpDispatcher = require('httpdispatcher');
const http = require('http');
const fs = require('fs');
const Markdown = require('markdown-to-html').GithubMarkdown;
var PORT = 3000;
try {
if(process.argv[2] && typeof eval(process.argv[2]) === 'number' && process.argv[2] > 1024 && process.argv[2] < 65535)
{
PORT = process.argv[2];
}
initHTTPServer();
}
catch (err) {
console.log("Error starting server: " + err.message);
process.exit(1);
}
function initHTTPServer() {
let dispatcher;
let server;
let homepage;
dispatcher = new HttpDispatcher();
dispatcher.onGet('/', function (req,res)
{
try{
let md = new Markdown();
let opts = {title:"Repo-copy",stylesheet:'web/repo-copy.css'};
md.bufmax = 2048;
md.once('end',function() {
res.end();
})
md.render('./README.md',opts,function(err) {
if(err){
res.writeHead(200,{'Content-Type':'text/markdown'});
res.end('## Error retrieving homepage ##');
return;
}
md.pipe(res);
return;
});
}
catch(err)
{
res.writeHead(200,{'Content-Type':'text/markdown'});
res.end('## Error retrieving homepage ##');
}
});
dispatcher.onPost('/createRepo', function (req, res) {
let repo;
repo = new Repo();
repo.executeRest(req, res, 'create');
});
dispatcher.onPost('/getRepoConfig', function (req, res) {
let repo;
repo = new Repo();
repo.executeRest(req, res, 'get');
});
dispatcher.onPost('/repoAudit', function (req, res) {
let repo;
repo = new Repo();
repo.executeRest(req, res, 'audit');
})
server = http.createServer((request, response) => {
try {
response.respond = function (status, msg, err) {
var respText = {};
respText.msg = msg;
if (err && err.hasOwnProperty('message')) {
respText.error = err.message;
}
this.writeHead(status, {'Content-Type': 'application/json'});
this.end(JSON.stringify(respText));
};
dispatcher.dispatch(request, response);
}
catch (err)
{
if (err.message === 'SHUTDOWN') {
throw err;
}
response.respond(503, "Error dispatching HTTP request", err);
}
});
// Startup the server. If running on Heroku, use Heroku port, otherwise configured PORT
server.listen(process.env.PORT || PORT, () => {
console.log("Server listening on port " + (process.env.PORT || PORT));
});
// Cleanup after ourselves if we get nerve-pinched
process.on('SIGTERM', function () {
server.close(() => {
self.shutdown();
});
});
// Cleanup after ourselves if we get nerve-pinched
process.on('SIGTERM', function () {
server.close(() => {
self.shutdown();
console.log("Server shutdown successfully");
});
});
};