forked from c9/cloud9
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
89 lines (77 loc) · 2.78 KB
/
Copy pathindex.js
File metadata and controls
89 lines (77 loc) · 2.78 KB
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
/**
* @copyright 2010, Ajax.org Services B.V.
* @license GPLv3 <http://www.gnu.org/licenses/gpl.txt>
*/
require("../../support/paths");
var Connect = require("connect");
var IO = require("socket.io");
var Fs = require("fs");
var Path = require("path");
var IdeServer = require("./ide");
var User = require("./user");
var middleware = require("./middleware");
exports.main = function(options) {
var projectDir = options.workspace,
port = options.port,
ip = options.ip,
user = options.user,
group = options.group;
if (!Path.existsSync(projectDir))
throw new Error("Workspace directory does not exist: " + projectDir);
var ideProvider = function(projectDir, server) {
var uid = "owner" + Math.random().toString().slice(2);
// load plugins:
var exts = {};
Fs.readdirSync(Path.normalize(__dirname + "/ext")).forEach(function(name){
exts[name] = require("./ext/" + name);
});
// create web socket
var socketOptions = {
transports: ['websocket', 'htmlfile', 'xhr-multipart', 'xhr-polling', 'jsonp-polling']
};
var socketIo = IO.listen(server, socketOptions);
socketIo.on("connection", function(client) {
ide.addClientConnection(uid, client, null);
});
var name = projectDir.split("/").pop();
var serverOptions = {
workspaceDir: projectDir,
davPrefix: "/workspace",
baseUrl: "",
debug: options.debug,
staticUrl: "/static",
workspaceId: name,
name: name,
version: options.version
};
var ide = new IdeServer(serverOptions, server, exts);
return function(req, res, next) {
req.session.uid = uid;
ide.addUser(uid, User.OWNER_PERMISSIONS);
ide.handle(req, res, next);
};
};
var server = Connect.createServer();
//server.use(Connect.logger());
server.use(Connect.conditionalGet());
server.use(Connect.cookieDecoder());
server.use(Connect.session({
key: "cloud9.sid"
}));
server.use(ideProvider(projectDir, server));
server.use(middleware.staticProvider(Path.normalize(__dirname + "/../../support"), "/static/support"));
server.use(middleware.staticProvider(Path.normalize(__dirname + "/../../client"), "/static"));
//obfuscate process rights if configured
if (group)
process.setgid(group);
if (user)
process.setuid(user);
server.listen(port, ip);
};
process.on("uncaughtException", function(e) {
console.log("uncaught exception:");
console.log(e.stack + "");
});
if (module === require.main) {
exports.main({workspace: ".", port: 3000, ip: '127.0.0.1'});
}