-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
221 lines (207 loc) · 7.21 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/**
* _____ __
* / ___// /_ _____
* \__ \/ / / / / _ \
* ___/ / / /_/ / __/
* /____/_/\__, /\___/
* /____/
* Copyright 2017 Slye Development Team. All Rights Reserved.
* Licence: MIT License
*/
// Import all required modules
const cluster = require('cluster')
const glob = require('glob')
const server = require('./server')
const libs = require('./libs') || {}
const CLIENT = require('./libs/client')
// todo: comment this function
function extend(object, parent){
let keys = Object.getOwnPropertyNames(parent)
for(let i = 0; i < keys.length;i++){
if(!object.hasOwnProperty(keys[i]))
object[keys[i]] = parent[keys[i]]
}
return object
}
/**
* Use this function to create a new app
* @return {object}
* Available methods:
* loadEndpoints(pattern)
* loadModels(pattern)
* maxCPU(num)
* redis(redis)
* run(port)
*/
function createApp(){
let __SlyeExpress__ = {
__uploader_status__: false
};
function pushFiles(array, pattern){
// Create array of file names
if(!__SlyeExpress__[array])
__SlyeExpress__[array] = [];
// Search for files using glob library
let files = glob.sync(pattern)
// Save result value as a variable
let re = 0;
// Push files to __SlyeExpress__[array]
for(let i = 0;i < files.length;i++){
let file = files[i]
// Prevent overwrite one path multiple times
// & add one to result when we push a something into list
if(__SlyeExpress__[array].indexOf(file) == -1 && (++re))
__SlyeExpress__[array].push(file)
}
return re
}
return extend({
/**
* Load a file containg one or more endpoints
* @param {string} pattern Glob pattern
* @return {number} Number of new files added to endpoints array
*/
loadEndpoints(pattern){
return pushFiles('__files__endpoints', pattern)
},
/**
* Load a model object
* @param {string} pattern Glob pattern
* @return {number} Number of new loaded models
*/
loadModels(pattern){
return pushFiles('__files__models', pattern)
},
/**
* Set max number of CPU cores to use to launch this app
* @param {number} num Max number of CPU cores
* zero means to use all system resources
* @throw This function will throw an error when `num` is not a number
*/
maxCPU(num){
__SlyeExpress__.maxCPU = parseInt(num);
if(isNaN(__SlyeExpress__.maxCPU)){
let func = 'SlyeExpress.createApp().maxCPU(num)'
let err = `${func}: num must be a valid number, ${num} given.`
throw new Error(err)
}
__SlyeExpress__.maxCPU = num
},
/**
* Set redis interface
* @param {redis} redis Redis interface
*/
redis(redis){
__SlyeExpress__.redis = redis
redis.on('connect', function () {
console.log("Connected to Redis")
})
},
/**
* Register a new variable so it can be used in all application scopes
* sometimes you may want to set a configuration value using this method
* @param {string} key Variable name
* @param {*} value Variable value
*/
set(key, value){
__SlyeExpress__[key] = value
},
mw(method){
if(!__SlyeExpress__.__mw)
__SlyeExpress__.__mw = []
__SlyeExpress__.__mw.push(method)
},
uploader:{
disable(){
__SlyeExpress__.__uploader_status__ = true
},
enable(){
__SlyeExpress__.__uploader_status__ = false
},
maxSize(size){
__SlyeExpress__.__uploader_max_size__ = size
},
storage(SEStorage){
__SlyeExpress__.__MulterStorage__ = SEStorage
},
},
allowOrigin(sites){
__SlyeExpress__._ao = sites
},
client(appname){
if(!__SlyeExpress__.__clients)
__SlyeExpress__.__clients = {}
__SlyeExpress__.__clients[appname] = new CLIENT(appname)
return __SlyeExpress__.__clients[appname]
},
/**
* Run server
* @param {number} port Port to run server
* Default value is first open port starting from 3000
*/
run(port){
if (cluster.isMaster) {
let startTimes = {}
console.log(`Master ${process.pid} is running`)
// Count machine's CPUs
let machineCPUs = require('os').cpus().length
// Minimum of `machineCPUs` & `__SlyeExpress__.maxCPU` is what user
// really wants (Think about it!)
let cpuCount = Math.min(machineCPUs, __SlyeExpress__.maxCPU || 0)
// Zero means use all resources
cpuCount = cpuCount == 0 ? machineCPUs : cpuCount
// Create a worker for each CPU
for (var i = 0; i < cpuCount; i += 1) {
cluster.fork()
}
cluster.on('listening', (worker) => {
startTimes[worker.process.pid] = Date.now()
});
cluster.on('exit', (worker, code, signal) => {
let pid = worker.process.pid
// Uptime in ms
let upTime = Date.now() - startTimes[pid]
if(upTime > 5000){
console.log(`worker ${pid} died, start it automatically`)
cluster.fork()
}else{
console.log(`worker ${pid} died immediately`)
}
});
} else {
__SlyeExpress__.worker = cluster.worker
server(__SlyeExpress__, port)
console.log(`Worker ${process.pid} started`)
}
}
}, libs(__SlyeExpress__));
}
/**
* Each SlyeExpress app has two main parts:
* a) Using `createApp` to create a new app
* b) Register a new Model or Endpoints in special scopes
* and each of theme has it's own importing structure
* (I know that's stupid but it really works!)
*
* To create an app use this structure:
* const app = require('SlyeExpress').createApp()
*
* Or when you need SlyeExpress API in your endpoints use this:
* const SlyeExpress = require('SlyeExpress')()
* Note: the last `()` is important!
* Note: this import statement must be at file header
* (Or somewhere that runs instantly after importing with no delay)
*
* To understand how second one works imagine this code:
* function loadEndpoint(){
* global.__SlyeExpress__ = ...
* require('path to endpoint')
* delete global.__SlyeExpress__
* }
* @constructor
*/
function SlyeExpress(){
return extend(global.__SlyeExpress__, libs(global.__SlyeExpress__))
}
SlyeExpress.createApp = createApp
module.exports = SlyeExpress