1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | const express = require('express');
|
7 | const nunjucks = require('nunjucks');
|
8 | const fs = require('fs-extra');
|
9 | const path = require('path');
|
10 | const util = require('./src/util/util');
|
11 | require('colors');
|
12 | const Q = require('q');
|
13 | const cors = require('cors');
|
14 | const test = process.env.TEST_SUITE;
|
15 | const noInstall = process.env.NO_INSTALL;
|
16 |
|
17 | module.exports = function(options) {
|
18 | options = options || {};
|
19 | const q = Q.defer();
|
20 |
|
21 | util.log('');
|
22 | const rl = require('readline').createInterface({
|
23 | input: require('fs').createReadStream('logo.txt')
|
24 | });
|
25 |
|
26 | rl.on('line', function(line) {
|
27 | util.log(
|
28 | line.substring(0,4) +
|
29 | line.substring(4, 30).cyan.bold +
|
30 | line.substring(30, 33) +
|
31 | line.substring(33, 42).green.bold +
|
32 | line.substring(42)
|
33 | );
|
34 | });
|
35 |
|
36 | rl.on('close', function() {
|
37 |
|
38 | util.log('');
|
39 | util.log(fs.readFileSync('welcome.txt').toString().green);
|
40 | });
|
41 |
|
42 |
|
43 | const app = options.app || express();
|
44 |
|
45 |
|
46 | const config = options.config || require('config');
|
47 |
|
48 |
|
49 | nunjucks.configure('client', {
|
50 | autoescape: true,
|
51 | express: app
|
52 | });
|
53 |
|
54 |
|
55 | if (config.allowedOrigins) {
|
56 | app.use(cors({
|
57 | origin: function(origin, callback) {
|
58 | if (!origin) {
|
59 | return callback(null, true);
|
60 | }
|
61 | if (config.allowedOrigins.indexOf(origin) === -1 && config.allowedOrigins.indexOf("*") === -1) {
|
62 | var msg = 'The CORS policy for this site does not allow access from the specified Origin.';
|
63 | return callback(new Error(msg), false);
|
64 | }
|
65 | return callback(null, true);
|
66 | }
|
67 | }));
|
68 | }
|
69 |
|
70 | app.use('/', express.static(path.join(__dirname, '/client/dist')));
|
71 |
|
72 |
|
73 | const server = options.server || require('./index')(config);
|
74 | const hooks = options.hooks || {};
|
75 |
|
76 | app.use(server.formio.middleware.restrictRequestTypes);
|
77 | server.init(hooks).then(function(formio) {
|
78 |
|
79 | const start = function() {
|
80 |
|
81 | if (fs.existsSync('app')) {
|
82 | const application = express();
|
83 | application.use('/', express.static(path.join(__dirname, '/app/dist')));
|
84 | config.appPort = config.appPort || 8080;
|
85 | application.listen(config.appPort);
|
86 | const appHost = `http://localhost:${config.appPort}`;
|
87 | util.log(` > Serving application at ${appHost.green}`);
|
88 | }
|
89 |
|
90 |
|
91 | app.use(options.mount || '/', server);
|
92 |
|
93 |
|
94 | app.formio = formio;
|
95 |
|
96 |
|
97 | return q.resolve({
|
98 | server: app,
|
99 | config: config
|
100 | });
|
101 | };
|
102 |
|
103 |
|
104 | const install = {
|
105 | download: false,
|
106 | extract: false,
|
107 | import: false,
|
108 | user: false
|
109 | };
|
110 |
|
111 |
|
112 | if (!fs.existsSync('client') && !test && !noInstall) {
|
113 | install.download = true;
|
114 | install.extract = true;
|
115 | }
|
116 |
|
117 |
|
118 | formio.db.collection('forms').estimatedDocumentCount(function(err, numForms) {
|
119 |
|
120 | if ((!err && numForms > 0) || test || noInstall) {
|
121 | if (!install.download && !install.extract) {
|
122 | return start();
|
123 | }
|
124 | }
|
125 |
|
126 |
|
127 | install.import = true;
|
128 | install.user = true;
|
129 |
|
130 |
|
131 | require('./install')(formio, install, function(err) {
|
132 | if (err) {
|
133 | if (err !== 'Installation canceled.') {
|
134 | return util.log(err.message);
|
135 | }
|
136 | }
|
137 |
|
138 |
|
139 | start();
|
140 | });
|
141 | });
|
142 | });
|
143 |
|
144 | return q.promise;
|
145 | };
|