-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
120 lines (96 loc) · 3.23 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
111
112
113
114
115
116
117
118
119
120
const fs = require("fs");
const ip = require("ip");
const opn = require("opn");
const http = require("http");
const debug = require("debug");
const path = require("path");
const express = require("express");
const config = require("./config");
const msgs = require("./msgs");
const helper = require("./app/helper");
const reader = require('./app/reader');
const tree = require('./app/tree');
const app = new express();
const server = http.createServer(app);
app.use(express.static(config.publicFolder));
// set CORS
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*' );
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE' );
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type' );
res.setHeader('Access-Control-Allow-Credentials', true );
next();
});
/*
* public homepage
*/
app.get('/', (req, res) => fs.createReadStream('./public/home.html').pipe(res));
/*
* git tree
*/
app.get('/tree', (req, res) => {
var dir = path.join(config.gitDirectory, '.git');
reader.getTree(dir)
.then(tree => {
var nodes = [], edges = [];
var edgeTimeStamp = +new Date();
for (let node of tree.values()) {
let label = node.id;
if (node.name) label = node.name;
nodes.push({
id: node.id,
label: `${label}`,
type: node.type
});
node.fillEdges(edgeTimeStamp, edges, item => item.id); // Sync
}
return {
nodes,
edges
};
})
.then(data => {
// setup file watchers for WebSocket
reader.setupWatcher(dir);
debug('log')(`${dir} is ready`);
return data;
})
.then(data => res.json(data))
.catch(err => {
let msg = msgs[err.message];
if (msg) {
res.json({
error: msg
});
} else {
res.status(500).end();
}
});
});
/*
* node data
*/
app.get('/nodedata/:nodeid', function (req, res) {
let node_id = req.params.nodeid;
debug('log')('get node data : ' + node_id);
let node = tree.getInstance().get(node_id)
if (!node) return res.end(node_id);
res.json
(
{
type : node.type,
sha : node.sha,
size : node.size,
formattedSize : helper.formatSize(node.size),
chunked : ((node.size || node.data.length) > config.maxKeepFileSize),
data : helper.limitDataSize(node.data)
}
);
});
const socket = require('./app/socket');
socket.initialize(server);
server.listen(config.portNumber, function () {
console.log(`Server listening ::: ${config.portNumber}`);
console.log("ip = " + ip.address());
// opn(`http://${ip.address()}:${config.portNumber}`);
});