@modular-rest/server 1.4.3 → 1.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/application.js +191 -144
package/package.json
CHANGED
package/src/application.js
CHANGED
|
@@ -1,19 +1,70 @@
|
|
|
1
|
-
let koa = require(
|
|
2
|
-
const cors = require(
|
|
3
|
-
const koaBody = require(
|
|
4
|
-
const koaStatic = require(
|
|
5
|
-
var path = require(
|
|
6
|
-
var Combination = require(
|
|
7
|
-
let DataProvider = require(
|
|
8
|
-
let UserService = require(
|
|
9
|
-
|
|
10
|
-
let defaultServiceRoot = __dirname +
|
|
1
|
+
let koa = require("koa");
|
|
2
|
+
const cors = require("@koa/cors");
|
|
3
|
+
const koaBody = require("koa-body");
|
|
4
|
+
const koaStatic = require("koa-static-server");
|
|
5
|
+
var path = require("path");
|
|
6
|
+
var Combination = require("./class/combinator");
|
|
7
|
+
let DataProvider = require("./services/data_provider/service");
|
|
8
|
+
let UserService = require("./services/user_manager/service");
|
|
9
|
+
|
|
10
|
+
let defaultServiceRoot = __dirname + "/services";
|
|
11
|
+
|
|
12
|
+
// staticOptions = {
|
|
13
|
+
// /**
|
|
14
|
+
// * directory that is to be served
|
|
15
|
+
// */
|
|
16
|
+
// rootDir?: string | undefined;
|
|
17
|
+
// /**
|
|
18
|
+
// * optional rewrite path
|
|
19
|
+
// */
|
|
20
|
+
// rootPath?: string | undefined;
|
|
21
|
+
// /**
|
|
22
|
+
// * optional default file to serve if requested static is missing
|
|
23
|
+
// */
|
|
24
|
+
// notFoundFile?: string | undefined;
|
|
25
|
+
// /**
|
|
26
|
+
// * request access log to console
|
|
27
|
+
// */
|
|
28
|
+
// log?: boolean | undefined;
|
|
29
|
+
// /**
|
|
30
|
+
// * don't execute any downstream middleware. defaults to true
|
|
31
|
+
// */
|
|
32
|
+
// last?: boolean | undefined;
|
|
33
|
+
// /**
|
|
34
|
+
// * Browser cache max-age in milliseconds. defaults to 0
|
|
35
|
+
// */
|
|
36
|
+
// maxage?: number | undefined;
|
|
37
|
+
// /**
|
|
38
|
+
// * Allow transfer of hidden files. defaults to false
|
|
39
|
+
// */
|
|
40
|
+
// hidden?: boolean | undefined;
|
|
41
|
+
// /**
|
|
42
|
+
// * Try to serve the gzipped version of a file automatically when gzip is supported by a client and if the requested
|
|
43
|
+
// */
|
|
44
|
+
// gzip?: boolean | undefined;
|
|
45
|
+
// /**
|
|
46
|
+
// * Try to serve the brotli version of a file automatically when brotli is supported by a client and in the requested
|
|
47
|
+
// */
|
|
48
|
+
// brotli?: boolean | undefined;
|
|
49
|
+
// index?: string | undefined;
|
|
50
|
+
// }
|
|
11
51
|
|
|
12
52
|
/**
|
|
13
53
|
* @param {{
|
|
14
54
|
* cors: any; // Options for @koa/cors middleware.
|
|
15
55
|
* modulesPath: string; // Root directory of your router.js/db.js files.
|
|
16
|
-
*
|
|
56
|
+
* static: {
|
|
57
|
+
* rootDir: string; // Root directory of your static files.
|
|
58
|
+
* rootPath: string; // Root path of your static files.
|
|
59
|
+
* notFoundFile: string; // Not found file.
|
|
60
|
+
* log: boolean; // Log requests to console.
|
|
61
|
+
* last: boolean; // Don't execute any downstream middleware.
|
|
62
|
+
* maxage: number; // Browser cache max-age in milliseconds.
|
|
63
|
+
* hidden: boolean; // Allow transfer of hidden files.
|
|
64
|
+
* gzip: boolean; // Try to serve the gzipped version of a file automatically when gzip is supported by a client and if the requested
|
|
65
|
+
* brotli: boolean; // Try to serve the brotli version of a file automatically when brotli is supported by a client and in the requested
|
|
66
|
+
* index: string; // Index file.
|
|
67
|
+
* };
|
|
17
68
|
* onBeforeInit: (koaApp) => void; // A callback called before initializing the Koa server.
|
|
18
69
|
* onAfterInit: (koaApp) => void; // A callback called after server initialization.
|
|
19
70
|
* port: number; // Server port.
|
|
@@ -36,145 +87,141 @@ let defaultServiceRoot = __dirname + '/services';
|
|
|
36
87
|
* }} options
|
|
37
88
|
*/
|
|
38
89
|
module.exports = async function createRest(options) {
|
|
90
|
+
options = {
|
|
91
|
+
port: 3000,
|
|
92
|
+
dontListen: false,
|
|
93
|
+
mongo: {
|
|
94
|
+
atlas: false,
|
|
95
|
+
mongoBaseAddress: "mongodb://localhost:27017",
|
|
96
|
+
dbPrefix: "mrest_",
|
|
97
|
+
},
|
|
98
|
+
adminUser: {
|
|
99
|
+
email: "admin@email.com",
|
|
100
|
+
password: "@dmin",
|
|
101
|
+
},
|
|
102
|
+
|
|
103
|
+
...options,
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
let app = new koa();
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Plug in Cors
|
|
110
|
+
*/
|
|
111
|
+
app.use(cors(options.cors || {}));
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Plug in BodyParser
|
|
115
|
+
*/
|
|
116
|
+
let bodyParserOptions = {
|
|
117
|
+
multipart: true,
|
|
118
|
+
};
|
|
119
|
+
app.use(koaBody(bodyParserOptions));
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Plug In KoaStatic
|
|
123
|
+
*/
|
|
124
|
+
if (options.static) {
|
|
125
|
+
app.use(koaStatic(static));
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Run before hook
|
|
130
|
+
*/
|
|
131
|
+
if (options.onBeforeInit) options.onBeforeInit(app);
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Setup default services
|
|
135
|
+
*
|
|
136
|
+
* - Collect and plug in router.js/db.js of default services
|
|
137
|
+
* - Setting up default services
|
|
138
|
+
*/
|
|
139
|
+
|
|
140
|
+
// Plug in default routes
|
|
141
|
+
await Combination.combineRoutesByFilePath(path.join(defaultServiceRoot), app);
|
|
142
|
+
|
|
143
|
+
// Collect default databaseDefinitions
|
|
144
|
+
let defaultDatabaseDefinitionList =
|
|
145
|
+
await Combination.combineModulesByFilePath({
|
|
146
|
+
rootDirectory: defaultServiceRoot,
|
|
147
|
+
filename: {
|
|
148
|
+
name: "db",
|
|
149
|
+
extension: ".js",
|
|
150
|
+
},
|
|
151
|
+
combineWithRoot: true,
|
|
152
|
+
});
|
|
39
153
|
|
|
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
|
-
multipart: true
|
|
68
|
-
};
|
|
69
|
-
app.use(koaBody(bodyParserOptions));
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Plug In KoaStatic
|
|
73
|
-
*/
|
|
74
|
-
if (options.staticPath)
|
|
75
|
-
app.use(koaStatic({
|
|
76
|
-
rootDir: options.staticPath,
|
|
77
|
-
rootPath: '/assets/',
|
|
78
|
-
|
|
79
|
-
}));
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* Run before hook
|
|
83
|
-
*/
|
|
84
|
-
if (options.onBeforeInit) options.onBeforeInit(app);
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Setup default services
|
|
88
|
-
*
|
|
89
|
-
* - Collect and plug in router.js/db.js of default services
|
|
90
|
-
* - Setting up default services
|
|
91
|
-
*/
|
|
92
|
-
|
|
93
|
-
// Plug in default routes
|
|
94
|
-
await Combination.combineRoutesByFilePath(
|
|
95
|
-
path.join(defaultServiceRoot), app);
|
|
96
|
-
|
|
97
|
-
// Collect default databaseDefinitions
|
|
98
|
-
let defaultDatabaseDefinitionList = await Combination.combineModulesByFilePath({
|
|
99
|
-
rootDirectory: defaultServiceRoot,
|
|
100
|
-
filename: {
|
|
101
|
-
name: 'db',
|
|
102
|
-
extension: '.js'
|
|
103
|
-
},
|
|
104
|
-
combineWithRoot: true
|
|
154
|
+
// Plug in default databaseDefinitions
|
|
155
|
+
await DataProvider.addCollectionDefinitionByList({
|
|
156
|
+
list: defaultDatabaseDefinitionList,
|
|
157
|
+
mongoOption: options.mongo,
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
// Setting up default services
|
|
161
|
+
await require("./helper/presetup_services").setup(options);
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* User Services
|
|
165
|
+
*
|
|
166
|
+
* Plug in routes and database
|
|
167
|
+
*/
|
|
168
|
+
if (options.modulesPath) {
|
|
169
|
+
// Plug in user routes
|
|
170
|
+
await Combination.combineRoutesByFilePath(options.modulesPath, app);
|
|
171
|
+
|
|
172
|
+
// Collect user CollectionDefinitions (db.js files)
|
|
173
|
+
let userDatabaseDetail = [];
|
|
174
|
+
userDatabaseDetail = await Combination.combineModulesByFilePath({
|
|
175
|
+
rootDirectory: options.modulesPath,
|
|
176
|
+
filename: {
|
|
177
|
+
name: "db",
|
|
178
|
+
extension: ".js",
|
|
179
|
+
},
|
|
180
|
+
combineWithRoot: true,
|
|
105
181
|
});
|
|
106
182
|
|
|
107
|
-
//
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
mongoOption: options.mongo
|
|
111
|
-
})
|
|
112
|
-
|
|
113
|
-
// Setting up default services
|
|
114
|
-
await require('./helper/presetup_services').setup(options);
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* User Services
|
|
118
|
-
*
|
|
119
|
-
* Plug in routes and database
|
|
120
|
-
*/
|
|
121
|
-
if (options.modulesPath) {
|
|
122
|
-
|
|
123
|
-
// Plug in user routes
|
|
124
|
-
await Combination.combineRoutesByFilePath(options.modulesPath, app);
|
|
125
|
-
|
|
126
|
-
// Collect user CollectionDefinitions (db.js files)
|
|
127
|
-
let userDatabaseDetail = [];
|
|
128
|
-
userDatabaseDetail = await Combination.combineModulesByFilePath({
|
|
129
|
-
rootDirectory: options.modulesPath,
|
|
130
|
-
filename: {
|
|
131
|
-
name: 'db',
|
|
132
|
-
extension: '.js'
|
|
133
|
-
},
|
|
134
|
-
combineWithRoot: true
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
// combine additional CollectionDefinitions
|
|
138
|
-
if (options.collectionDefinitions) {
|
|
139
|
-
userDatabaseDetail.concat(options.collectionDefinitions)
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
// Plug in user CollectionDefinitions
|
|
143
|
-
await DataProvider.addCollectionDefinitionByList({
|
|
144
|
-
list: userDatabaseDetail || [],
|
|
145
|
-
mongoOption: options.mongo
|
|
146
|
-
})
|
|
147
|
-
|
|
148
|
-
// Plug in Verification method
|
|
149
|
-
if (typeof options.verificationCodeGeneratorMethod == 'function') {
|
|
150
|
-
UserService.main.setCustomVerificationCodeGeneratorMethod(
|
|
151
|
-
options.verificationCodeGeneratorMethod
|
|
152
|
-
)
|
|
153
|
-
}
|
|
183
|
+
// combine additional CollectionDefinitions
|
|
184
|
+
if (options.collectionDefinitions) {
|
|
185
|
+
userDatabaseDetail.concat(options.collectionDefinitions);
|
|
154
186
|
}
|
|
155
187
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
return new Promise((done, reject) => {
|
|
162
|
-
|
|
163
|
-
let server;
|
|
164
|
-
|
|
165
|
-
if (!options.dontListen) {
|
|
166
|
-
server = app.listen(options.port);
|
|
167
|
-
console.log('\x1b[35m', `KOAS has been launched on: localhost:${options.port}`);
|
|
168
|
-
}
|
|
188
|
+
// Plug in user CollectionDefinitions
|
|
189
|
+
await DataProvider.addCollectionDefinitionByList({
|
|
190
|
+
list: userDatabaseDetail || [],
|
|
191
|
+
mongoOption: options.mongo,
|
|
192
|
+
});
|
|
169
193
|
|
|
194
|
+
// Plug in Verification method
|
|
195
|
+
if (typeof options.verificationCodeGeneratorMethod == "function") {
|
|
196
|
+
UserService.main.setCustomVerificationCodeGeneratorMethod(
|
|
197
|
+
options.verificationCodeGeneratorMethod
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Run the server
|
|
204
|
+
*
|
|
205
|
+
* return KOA app object
|
|
206
|
+
*/
|
|
207
|
+
return new Promise((done, reject) => {
|
|
208
|
+
let server;
|
|
209
|
+
|
|
210
|
+
if (!options.dontListen) {
|
|
211
|
+
server = app.listen(options.port);
|
|
212
|
+
console.log(
|
|
213
|
+
"\x1b[35m",
|
|
214
|
+
`KOAS has been launched on: localhost:${options.port}`
|
|
215
|
+
);
|
|
216
|
+
}
|
|
170
217
|
|
|
171
|
-
|
|
172
|
-
|
|
218
|
+
// on after init
|
|
219
|
+
if (options.onAfterInit) options.onAfterInit(app);
|
|
173
220
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
});
|
|
221
|
+
//done
|
|
222
|
+
done({
|
|
223
|
+
app,
|
|
224
|
+
server,
|
|
179
225
|
});
|
|
180
|
-
}
|
|
226
|
+
});
|
|
227
|
+
};
|