1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | var loopback = require('loopback');
|
18 | var path = require('path');
|
19 | var boot = require('loopback-boot');
|
20 | var modelDefinitions = require('./model-definitions');
|
21 | var bootGenerator = require('./boot-generator');
|
22 |
|
23 | module.exports = function(options) {
|
24 |
|
25 | options = options || {};
|
26 | var app = options.app || loopback();
|
27 | app.httpServer = options.server;
|
28 | options.app = app;
|
29 |
|
30 |
|
31 |
|
32 | require('./lib/secrets')(app, options);
|
33 | require('./lib/moment');
|
34 | require('./lib/log')(app);
|
35 | require('./lib/gis')(app);
|
36 | require('./lib/require')(app);
|
37 |
|
38 |
|
39 |
|
40 | app.set('view engine', 'ejs');
|
41 | app.set('json spaces', 2);
|
42 | app.set('trust proxy', 1);
|
43 | app.set('views', path.resolve(__dirname, 'views'));
|
44 |
|
45 |
|
46 |
|
47 | return {
|
48 | locals: options,
|
49 | init: function() {
|
50 | require('./lib/locals')(app, options);
|
51 |
|
52 | require('./lib/socket')({
|
53 | appOptions: options,
|
54 | app: app,
|
55 | });
|
56 |
|
57 | return Promise.resolve();
|
58 |
|
59 | },
|
60 | start: function() {
|
61 |
|
62 | if (options.building) {
|
63 | return;
|
64 | }
|
65 |
|
66 | var bootGenerated = bootGenerator(app, {
|
67 | models: app.configurator.load('model-config'),
|
68 | modelDefinitions: [],
|
69 | _definitions: {}
|
70 | });
|
71 |
|
72 | return modelDefinitions(app, bootGenerated)
|
73 | .then(function() {
|
74 | return new Promise(function(resolve, reject) {
|
75 |
|
76 | var middleware = app.configurator.load('middleware', true);
|
77 |
|
78 |
|
79 | var bootOptions = {
|
80 | appRootDir: __dirname,
|
81 | models: bootGenerated.models,
|
82 | modelDefinitions: bootGenerated.modelDefinitions,
|
83 | middleware: middleware,
|
84 | dataSources: app.configurator.load('datasources'),
|
85 | bootDirs: [
|
86 | path.join(__dirname, 'boot'),
|
87 | path.join(app.get('services_dir'), 'boot')
|
88 | ]
|
89 | };
|
90 |
|
91 | boot(app, bootOptions, function(err) {
|
92 | if (err) {
|
93 | reject(err);
|
94 | return;
|
95 | }
|
96 | app.indexes.updateDatasources(['db', 'db_prd']);
|
97 | app.emit('booted');
|
98 | resolve();
|
99 | });
|
100 | });
|
101 | });
|
102 | }
|
103 | };
|
104 | };
|