2017-12-12 20:41:32 +01:00
|
|
|
'use strict';
|
|
|
|
|
2017-12-18 20:39:51 +01:00
|
|
|
let fs = require( 'fs' );
|
|
|
|
let config;
|
|
|
|
let api;
|
|
|
|
|
|
|
|
if ( fs.existsSync( 'config.json' ) ) {
|
|
|
|
let contents = fs.readFileSync( 'config.json' );
|
|
|
|
config = JSON.parse( contents );
|
|
|
|
} else {
|
|
|
|
let contents = fs.readFileSync( 'config.example.json' );
|
|
|
|
config = JSON.parse( contents );
|
|
|
|
}
|
|
|
|
|
|
|
|
let driver;
|
|
|
|
let { DriverFactory } = require( './drivers/driverfactory' );
|
|
|
|
let driverFactory = new DriverFactory();
|
|
|
|
|
|
|
|
let { ApiController } = require( './controllers/apicontroller' );
|
2017-12-12 20:41:32 +01:00
|
|
|
|
|
|
|
let restify = require( 'restify' );
|
|
|
|
|
2017-12-18 20:39:51 +01:00
|
|
|
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() );
|
2017-12-18 20:39:51 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
));
|
|
|
|
|
2017-12-18 20:39:51 +01:00
|
|
|
driver = driverFactory.createFromConfig( config );
|
|
|
|
driver.write( driver.getBuffer() );
|
|
|
|
|
|
|
|
api = new ApiController( server, driver );
|
|
|
|
|
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.' );
|
|
|
|
|
2017-12-18 20:39:51 +01:00
|
|
|
});
|