Debian Codegraph @

extensions/VisualEditor/lib/ve/rebaser/src/app.js

raw symbols SCIP
1 /*!
2 * VisualEditor rebase server script.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 'use strict';
8
9 const
10 express = require( 'express' ),
11 http = require( 'http' ),
12 socketIO = require( 'socket.io' ),
13 mongodb = require( 'mongodb' ),
14 Logger = require( './Logger.js' ),
15 ve = require( '../../dist/ve-rebaser.js' ),
16 packageInfo = require( '../package.json' );
17
18 function initApp( options ) {
19 const app = express();
20
21 // get the options and make them available in the app
22 app.logger = options.logger; // the logging device
23 app.metrics = options.metrics; // the metrics
24 app.conf = options.config; // this app's config options
25 app.info = packageInfo; // this app's package info
26
27 app.use( express.static( __dirname + '/../..' ) );
28 app.set( 'view engine', 'ejs' );
29
30 app.get( '/', ( req, res ) => {
31 res.render( 'index' );
32 } );
33
34 // eslint-disable-next-line prefer-regex-literals
35 app.get( new RegExp( '/doc/edit/(.*)' ), ( req, res ) => {
36 const docName = req.params[ 0 ];
37 res.render( 'editor', { docName: docName } );
38 } );
39
40 // eslint-disable-next-line prefer-regex-literals
41 app.get( new RegExp( '/doc/raw/(.*)' ), ( req, res ) => {
42 // TODO return real data
43 // In order to provide HTML here, we'd need all of ve.dm (Document, Converter, all nodes)
44 // and none of that code is likely to work in nodejs without some work because of how heavily
45 // it uses the DOM.
46 // var docName = req.params[ 0 ];
47 res.status( 401 ).send( 'DOM in nodejs is hard' );
48 } );
49
50 const logger = new Logger( app.logger );
51 const mongoClient = new mongodb.MongoClient(
52 'mongodb://' + app.conf.mongodb.host + ':' + app.conf.mongodb.port
53 );
54 const documentStore = new ve.dm.DocumentStore( mongoClient, 'test', logger );
55 const protocolServer = new ve.dm.ProtocolServer( documentStore, logger );
56 app.transportServer = new ve.dm.TransportServer( protocolServer );
57 app.logger.log( 'info', 'Connecting to document store' );
58
59 return documentStore.connect().then( () => {
60 const dropDatabase = ( process.argv.includes( '--drop' ) );
61 if ( dropDatabase ) {
62 app.logger.log( 'info', 'Dropping database' );
63 }
64 return ( dropDatabase ? documentStore.dropDatabase() : Promise.resolve() ).then( () => app );
65 } );
66 }
67
68 function createServer( app ) {
69 // return a promise which creates an HTTP server,
70 // attaches the app to it, and starts accepting
71 // incoming client requests
72 let server;
73
74 return new Promise( ( resolve ) => {
75 server = http.createServer( app ).listen(
76 app.conf.port,
77 app.conf.interface,
78 resolve
79 );
80 const io = socketIO( server );
81 io.on(
82 'connection',
83 app.transportServer.onConnection.bind(
84 app.transportServer,
85 io.sockets.in.bind( io.sockets )
86 )
87 );
88 } ).then( () => {
89 app.logger.log( 'info',
90 'Worker ' + process.pid + ' listening on ' + ( app.conf.interface || '*' ) + ':' + app.conf.port );
91 return server;
92 } );
93 }
94
95 /**
96 * The service's entry point. It takes over the configuration
97 * options and the logger and metrics-reporting objects from
98 * service-runner and starts an HTTP server, attaching the application
99 * object to it.
100 *
101 * @param {Object} options
102 * @return {Promise} a promise for an http server.
103 */
104 module.exports = function ( options ) {
105 return initApp( options )
106 .then( createServer );
107 };