UNPKG

5.78 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.paths)
30 this.middleware = new middleware(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 })
67 } else {
68 var app = express()
69
70 app.use(bodyParser.urlencoded());
71 app.use(bodyParser.json());
72 app.use(fileUpload());
73
74 if (this.middleware.exists('express')) {
75 app.use(this.middleware.emit('express', {
76 paths: this.paths,
77 config: this.config,
78 env: this.env,
79 routes: Routes
80 }))
81 }
82
83 var publicPath = this.config.app.publicPath || this.paths.public.public;
84
85 if (exists(publicPath)) {
86 app.use(express.static(publicPath));
87 }
88
89 app.use(Routes.auto());
90
91 return app
92 }
93 }
94/**
95 * Testar multiple antes de publicar
96 */
97 this.multiple = () => {
98 if (this.init() && this.config.multiple) {
99 var config = this.config.multiple;
100
101 if (config[this.env]) {
102 config = config[this.env];
103 }
104
105 server.use(evh.vhost(server.enabled('trust proxy')));
106 server.listen(config.port);
107
108 config.sites.forEach((site) => {
109 if (site[this.env]) {
110 site = site[this.env];
111 }
112
113 try {
114 var app = require(path.resolve(site.path))(site, config);
115 } catch (err) {
116 var app = express()
117 app.use(bodyParser.urlencoded({
118 extended: false
119 }));
120 app.use(bodyParser.json());
121 app.use(fileUpload());
122 app.use(express.static(site.path));
123 }
124
125 if (!Array.isArray(site.domain)) {
126 site.domain = [site.domain];
127 }
128
129 for (var key in site.domain) {
130 evh.register(site.domain[key], app);
131 console.log(`Site ${site.name}, domain ${site.domain[key]}, registed in port ${config.port}`)
132 }
133
134 })
135 }
136 }
137
138 this.cli = (root, callback) => {
139 if(typeof root == 'function'){
140 callback = root;
141
142 var configsLocals = [
143 path.resolve('../'),
144 path.resolve('../../'),
145 path.resolve('../../../'),
146 path.resolve('../../../../'),
147 path.resolve('../../../../../')
148 ]
149
150 for(var key in configsLocals){
151 if(exists(path.resolve(configsLocals[key], 'cupcoffee.json'))){
152 this.root = configsLocais[key];
153 break;
154 }
155 }
156 }
157 else{
158 if(exists(path.resolve(root, 'cupcoffee.json'))){
159 this.root = root;
160 }
161 else{
162 console.error('Cannot find cupcoffee.json')
163 return false;
164 }
165 }
166
167 if(this.init(this.root)){
168 var cli = require('./cli')(this.config.app[this.env], this.paths)
169 return callback(cli);
170 }
171
172 return false;
173 }
174
175 this.start = () => {
176 if(!this.init()){
177 return false;
178 }
179
180 var port = this.config.app[this.env].port,
181 hostname = this.config.app[this.env].hostname,
182 events = this.events
183
184 this.app().listen(port, hostname, function() {
185 events.emit('startServer', {
186 port,
187 hostname
188 })
189 console.log(`Server running at http://${hostname}:${port}/`);
190 });
191 };
192
193 return this;
194}