UNPKG

6.13 kBJavaScriptView Raw
1"use strict";
2
3var path = require('path'),
4 bodyParser = require('body-parser'),
5 fileUpload = require('express-fileupload'),
6 evh = require('express-vhost'),
7 express = require('express'),
8 exists = require('fs-exists-sync'),
9 events = require('./events/index.js'),
10 middleware = require('./middleware/index.js'),
11 server = express();
12
13module.exports = (root) => {
14 this.root = path.resolve('.');
15
16 this.init = (rootInit = false) => {
17 if(rootInit){
18 this.root = rootInit;
19 }
20
21 this.paths = require('./configs/paths')(this.root);
22
23 this.config = require(path.join(this.paths.root, 'cupcoffee.json'));
24
25 this.env = (process.env.NODE_CUPCOFFEE_ENV) ?
26 process.env.NODE_CUPCOFFEE_ENV : (process.env.NODE_ENV) ?
27 process.env.NODE_ENV : 'development';
28
29 this.events = new events(this.config, this.paths)
30 this.middleware = new middleware(this.config, this.paths)
31
32 if (this.config.app) {
33 if (!this.config.app[this.env]) {
34 if (this.config.app.env) {
35 if (this.config.app[this.config.app.env]) {
36 this.env = this.config.app.env;
37 }
38 } else if (this.config.app['development']) {
39 this.env = 'development';
40 } else if (this.config.app['production']) {
41 this.env = 'production';
42 } else {
43 console.error("CUPCOFFEE: NODE_CUPCOFFEE_ENV or NODE_ENV need to be declared.")
44 return false;
45 }
46 }
47 }
48
49 return true;
50 }
51
52 this.app = () => {
53 if(this.init()){
54 var Routes = module.exports.routes = new(require('./routes'))(this.config.app[this.env], this.paths);
55 }
56 else{
57 return false;
58 }
59
60 if (this.events.exists('createApp')) {
61 return this.exists.emit('createApp', {
62 paths: this.paths,
63 config: this.config,
64 env: this.env,
65 routes: Routes,
66 controller: Routes.controller,
67 model: Routes.model,
68 view: Routes.view,
69 logger: Routes.logger
70 })
71 } else {
72 var app = express()
73
74 app.use(bodyParser.urlencoded());
75 app.use(bodyParser.json());
76 app.use(fileUpload());
77
78 if (this.middleware.exists('express')) {
79 app.use(this.middleware.emit('express', {
80 paths: this.paths,
81 config: this.config,
82 env: this.env,
83 routes: Routes,
84 controller: Routes.controller,
85 model: Routes.model,
86 view: Routes.view,
87 logger: Routes.logger
88 }))
89 }
90
91 var publicPath = this.config.app.publicPath || this.paths.public.public;
92
93 if (exists(publicPath)) {
94 app.use(express.static(publicPath));
95 }
96
97 app.use(Routes.auto());
98
99 return app
100 }
101 }
102/**
103 * Testar multiple antes de publicar
104 */
105 this.multiple = () => {
106 if (this.init() && this.config.multiple) {
107 var config = this.config.multiple;
108
109 if (config[this.env]) {
110 config = config[this.env];
111 }
112
113 server.use(evh.vhost(server.enabled('trust proxy')));
114 server.listen(config.port);
115
116 config.sites.forEach((site) => {
117 if (site[this.env]) {
118 site = site[this.env];
119 }
120
121 try {
122 var app = require(path.resolve(site.path))(site, config);
123 } catch (err) {
124 var app = express()
125 app.use(bodyParser.urlencoded({
126 extended: false
127 }));
128 app.use(bodyParser.json());
129 app.use(fileUpload());
130 app.use(express.static(site.path));
131 }
132
133 if (!Array.isArray(site.domain)) {
134 site.domain = [site.domain];
135 }
136
137 for (var key in site.domain) {
138 evh.register(site.domain[key], app);
139 console.log(`Site ${site.name}, domain ${site.domain[key]}, registed in port ${config.port}`)
140 }
141
142 })
143 }
144 }
145
146 this.cli = (root, callback) => {
147 if(typeof root == 'function'){
148 callback = root;
149
150 var configsLocals = [
151 path.resolve('../'),
152 path.resolve('../../'),
153 path.resolve('../../../'),
154 path.resolve('../../../../'),
155 path.resolve('../../../../../')
156 ]
157
158 for(var key in configsLocals){
159 if(exists(path.resolve(configsLocals[key], 'cupcoffee.json'))){
160 this.root = configsLocais[key];
161 break;
162 }
163 }
164 }
165 else{
166 if(exists(path.resolve(root, 'cupcoffee.json'))){
167 this.root = root;
168 }
169 else{
170 console.error('Cannot find cupcoffee.json')
171 return false;
172 }
173 }
174
175 if(this.init(this.root)){
176 var cli = require('./cli')(this.config.app[this.env], this.paths)
177 return callback(cli);
178 }
179
180 return false;
181 }
182
183 this.start = () => {
184 if(!this.init()){
185 return false;
186 }
187
188 var port = this.config.app[this.env].port,
189 hostname = this.config.app[this.env].hostname,
190 events = this.events
191
192 this.app().listen(port, hostname, function() {
193 events.emit('startServer', {
194 port,
195 hostname
196 })
197 console.log(`Server running at http://${hostname}:${port}/`);
198 });
199 };
200
201 return this;
202}