-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pixdisp.js
79 lines (58 loc) · 1.83 KB
/
pixdisp.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
'use strict';
let fs = require( 'fs' );
let os = require( 'os' );
let config,
vmcontroller,
api,
contents;
if ( fs.existsSync( 'config.json' ) ) {
contents = fs.readFileSync( 'config.json' );
} else if ( fs.existsSync( os.homedir() + '/.pixdisp/config.json' ) ) {
contents = fs.readFileSync( os.homedir() + '/.pixdisp/config.json' );
} else {
contents = fs.readFileSync( 'config.example.json' );
}
if ( typeof contents === 'undefined' ||
contents === false || contents === undefined ||
contents === '' ) {
console.log( 'Invalid or no configuration' );
process.exit();
}
config = JSON.parse( contents );
let driver;
let { DriverFactory } = require( './drivers/driverfactory' );
let driverFactory = new DriverFactory();
let { VMController } = require( './controllers/vmcontroller' );
let { ApiController } = require( './controllers/apicontroller' );
let restify = require( 'restify' );
let bindAddr = config.bindAddr;
let bindPort = config.bindPort;
let server = restify.createServer( {
handleUpgrades: true
} );
server.use( restify.plugins.queryParser() );
server.use( restify.plugins.bodyParser( {
mapParams: true
} ) );
server.use( restify.plugins.jsonp() );
server.use( restify.plugins.gzipResponse() );
server.use( restify.plugins.throttle(
{
burst: 50,
rate: 30,
ip: true
}
));
driver = driverFactory.createFromConfig( config );
driver.write( driver.getBuffer() );
vmcontroller = new VMController( driver );
api = new ApiController( server, driver, vmcontroller );
server.get(/.*/, restify.plugins.serveStatic({
'directory': 'www',
'default': 'index.html'
}));
server.pre( restify.pre.userAgentConnection() );
server.listen( bindPort, bindAddr, function() {
console.log( '%s listening at %s ', server.name , server.url );
console.log( 'Ready.' );
});