1
0
Fork 0
mirror of https://github.com/sexybiggetje/pixdisp.git synced 2024-11-24 18:31:02 +01:00
pixdisp/pixdisp.js

80 lines
1.8 KiB
JavaScript
Raw Permalink Normal View History

2017-12-12 20:41:32 +01:00
'use strict';
let fs = require( 'fs' );
2018-01-05 15:26:03 +01:00
let os = require( 'os' );
let config,
vmcontroller,
2018-01-05 15:26:03 +01:00
api,
contents;
if ( fs.existsSync( 'config.json' ) ) {
2018-01-05 15:26:03 +01:00
contents = fs.readFileSync( 'config.json' );
} else if ( fs.existsSync( os.homedir() + '/.pixdisp/config.json' ) ) {
contents = fs.readFileSync( os.homedir() + '/.pixdisp/config.json' );
} else {
2018-01-05 15:26:03 +01:00
contents = fs.readFileSync( 'config.example.json' );
}
2018-01-05 15:26:03 +01:00
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' );
2017-12-12 20:41:32 +01:00
let restify = require( 'restify' );
let bindAddr = config.bindAddr;
let bindPort = config.bindPort;
2017-12-12 20:41:32 +01:00
let server = restify.createServer( {
handleUpgrades: true
} );
server.use( restify.plugins.queryParser() );
server.use( restify.plugins.bodyParser( {
mapParams: true
} ) );
2017-12-12 20:41:32 +01:00
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 );
2017-12-12 20:41:32 +01:00
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.' );
});