UNPKG

3.08 kBJavaScriptView Raw
1/* Copyright 2017 Agneta Network Applications, LLC.
2 *
3 * Source file: services/index.js
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17var loopback = require('loopback');
18var path = require('path');
19var boot = require('loopback-boot');
20var modelDefinitions = require('./model-definitions');
21var bootGenerator = require('./boot-generator');
22
23module.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 //console.log(middleware);
78 //console.log(bootGenerated.modelDefinitions);
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};