-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathframe.js
executable file
·152 lines (130 loc) · 4.02 KB
/
frame.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
#! /usr/local/bin/node
var program = require('commander');
program
.version('0.0.1')
.option('-u, --username <username>', 'Username to which this frame will be linked.')
.option('-f, --framename <framename>', 'Name for the frame.')
.option('-d, --dom <dom>', 'The root domain (including port) at which the frame server is accessible. Defaults to localhost:8888.')
.option('-r, --reset', 'If present, reset the configuration, i.e. treat this as an entirely new frame.')
.option('-c, --chromium', 'If this flag is present, force the use chromium.')
.parse(process.argv);
if (!program.username) {
console.log('Username is required.');
program.outputHelp();
process.exit();
} else {
console.log('Starting as ' + program.username);
}
var username = program.username,
root_domain = program.dom || "localhost:8888",
chromium = program.chromium,
reset = program.reset,
framename = program.framename || 'New Frame';
var connect = require('connect');
var http = require('http');
var serveStatic = require('serve-static');
var open = require('open');
var uuid = require('node-uuid');
var exec = require('child_process').exec;
var fs = require('fs');
var request = require('request-json');
var client = request.createClient('http://'+root_domain);
var conf;
console.log(client);
// If reset flag is present, remove the conf
// TODO: delete existing frame from server on reset.
if (reset) {
try {
fs.unlinkSync('./conf.json');
} catch(e) {
// file missing, do nothing
}
}
var app = connect();
app.use('/config', function(req, res, next) {
res.end(JSON.stringify(conf));
});
app.use(serveStatic('./static', {
'index': ['frame.html']
}));
// try to get frame conf from local file:
try {
conf = require('./conf.json');
if (conf.owner !== username || conf.name !== framename) {
// TODO: if the supplied user or framename don't match the conf, update the frame on the server?
var frame = {
owner: username,
users: [username],
name: framename,
active: false,
settings: {
visible: true
}
};
client.put('/frames/'+conf._id.$oid, frame, function(err, res, body) {
if (err) console.log(err);
console.log("body", body);
if (res.statusCode === 200) {
createConfigFile(body);
conf = body;
conf.root_domain = root_domain;
startServer();
}
});
} else {
conf.root_domain = root_domain;
if (conf) startServer();
}
} catch (e) {
// no conf file, create new frame and save resulting conf
var frame = {
owner: username,
users: [username],
name: framename,
active: false,
settings: {
visible: true
}
};
client.post('/frames', frame, function(err, res, body) {
if (err) console.log(err);
console.log("body", body);
if (res.statusCode === 200) {
createConfigFile(body);
conf = body;
conf.root_domain = root_domain;
startServer();
}
});
}
function createConfigFile(conf) {
fs.writeFile('conf.json', JSON.stringify(conf), function(err) {
if (err) {
console.log(err);
}
});
}
//create node.js http server and listen on port
function startServer() {
http.createServer(app).listen(7000);
launchFrame();
}
function launchFrame() {
// if we're on linux, let's try to open the thing with chromium in kiosk mode
if (/^linux/.test(process.platform)) {
console.log('linux', process.platform);
var xinitrc_path = __dirname + '/bin/.xinitrc';
exec('xinit ' + xinitrc_path, function(error, stdout, stderr) {
console.log(error, stdout, stderr);
});
} else {
console.log('not linux', process.platform);
if (chromium) {
exec('/Applications/Chromium.app/Contents/MacOS/Chromium --kiosk http://localhost:7000', function(error, stdout, stderr) {
console.log(error, stdout, stderr);
});
} else {
open("http://localhost:7000");
}
}
}