@projectinvicta/nails 3.0.7 → 3.0.8
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/dist/Controller.d.ts +34 -0
- package/dist/Controller.js +177 -0
- package/dist/Controller.js.map +1 -0
- package/dist/Nails.d.ts +35 -0
- package/dist/Nails.js +183 -0
- package/dist/Nails.js.map +1 -0
- package/dist/Router.d.ts +29 -0
- package/dist/Router.js +131 -0
- package/dist/Router.js.map +1 -0
- package/dist/application.d.ts +10 -0
- package/dist/application.js +26 -0
- package/dist/application.js.map +1 -0
- package/dist/config.d.ts +61 -0
- package/dist/config.js +2 -0
- package/dist/config.js.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { type RouteDefinition, type ControllerDoRouteParams } from './config.ts';
|
|
2
|
+
interface Router {
|
|
3
|
+
on(event: string, listener: Function): void;
|
|
4
|
+
removeAllListeners(event: string): void;
|
|
5
|
+
addRoutes(routes: RouteDefinition[]): void;
|
|
6
|
+
}
|
|
7
|
+
declare const DISABLE_AUTORENDER: {};
|
|
8
|
+
declare class Controller {
|
|
9
|
+
static router: Router;
|
|
10
|
+
static models: Record<string, any>;
|
|
11
|
+
static get DISABLE_AUTORENDER(): typeof DISABLE_AUTORENDER;
|
|
12
|
+
static setRouter(router_singleton: Router): void;
|
|
13
|
+
static setModels(models: Record<string, any>): void;
|
|
14
|
+
models: Record<string, any>;
|
|
15
|
+
routes: RouteDefinition[];
|
|
16
|
+
json: boolean;
|
|
17
|
+
constructor();
|
|
18
|
+
_getControllerName(): string;
|
|
19
|
+
/** Initializes local and global routes defined on the Controller subclass */
|
|
20
|
+
_registerControllerRoutes(): void;
|
|
21
|
+
/**** Network Methods *****/
|
|
22
|
+
/**
|
|
23
|
+
* The main entry function of the controller.
|
|
24
|
+
*
|
|
25
|
+
* @param {string} options.action The action called for this controller
|
|
26
|
+
* @param {object} options.params The parameter hash for this action
|
|
27
|
+
* @param {object} options.request The http request object from the http server
|
|
28
|
+
* @param {object} options.response The http response object from the http server
|
|
29
|
+
* @param {object} options.ws The WebSocket instance
|
|
30
|
+
* @param {object} options.next The Express NextFunction
|
|
31
|
+
*/
|
|
32
|
+
_do({ action, params, request, response, ws, next }: ControllerDoRouteParams): Promise<void>;
|
|
33
|
+
}
|
|
34
|
+
export default Controller;
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
// Global router instance
|
|
2
|
+
let router;
|
|
3
|
+
let controller_proto; // This will be assigned an instance of Controller
|
|
4
|
+
// TODO: refactor error generation
|
|
5
|
+
const NAME_REQUIRED_ERROR = () => {
|
|
6
|
+
return new Error('FATAL ERROR::: Named function required for Controller constructor method');
|
|
7
|
+
};
|
|
8
|
+
const DISABLE_AUTORENDER = new (class DisableAutorender {
|
|
9
|
+
});
|
|
10
|
+
// The base controller definition
|
|
11
|
+
class Controller {
|
|
12
|
+
// Static properties
|
|
13
|
+
static router; // Static reference to the router
|
|
14
|
+
static models; // Static reference to models, assuming key-value pairs
|
|
15
|
+
static get DISABLE_AUTORENDER() {
|
|
16
|
+
return DISABLE_AUTORENDER;
|
|
17
|
+
}
|
|
18
|
+
static setRouter(router_singleton) {
|
|
19
|
+
Controller.router = router = router_singleton;
|
|
20
|
+
}
|
|
21
|
+
static setModels(models) {
|
|
22
|
+
Controller.prototype.models = models;
|
|
23
|
+
}
|
|
24
|
+
// Instance properties
|
|
25
|
+
models; // This will be assigned via Controller.setModels
|
|
26
|
+
routes; // Assuming routes are defined on subclasses
|
|
27
|
+
json;
|
|
28
|
+
constructor() {
|
|
29
|
+
const controllerName = this._getControllerName();
|
|
30
|
+
router.removeAllListeners('dispatchTo:' + controllerName);
|
|
31
|
+
router.on('dispatchTo:' + controllerName, this._do.bind(this));
|
|
32
|
+
}
|
|
33
|
+
_getControllerName() {
|
|
34
|
+
return this.constructor.name.toLowerCase().replace(/controller$/, '');
|
|
35
|
+
}
|
|
36
|
+
/** Initializes local and global routes defined on the Controller subclass */
|
|
37
|
+
_registerControllerRoutes() {
|
|
38
|
+
// `this.json` is not explicitly defined on Controller, but expected on subclasses
|
|
39
|
+
// assuming it's a boolean or undefined
|
|
40
|
+
const defaultToJson = this.json;
|
|
41
|
+
const controllerName = this._getControllerName();
|
|
42
|
+
if (this.routes && this.routes.length) {
|
|
43
|
+
const localizedRoutes = this.routes.map(route => {
|
|
44
|
+
// TODO: throw an error if :controller is present in local routes
|
|
45
|
+
// TODO: also account for other malformed local routes
|
|
46
|
+
const routePrefix = `/${controllerName}/`;
|
|
47
|
+
const modifiedDestination = (route[1][0] == '/')
|
|
48
|
+
? route[1]
|
|
49
|
+
: (route[1].startsWith('./') ? route[1].replace('./', routePrefix) : routePrefix + route[1]);
|
|
50
|
+
const modifiedOptions = { ...route[2] };
|
|
51
|
+
if (!('json' in modifiedOptions))
|
|
52
|
+
modifiedOptions.json = defaultToJson;
|
|
53
|
+
if (!('action' in modifiedOptions)
|
|
54
|
+
&& typeof modifiedDestination === 'string' && !modifiedDestination.includes(":action")
|
|
55
|
+
&& !Object.values(modifiedOptions).includes('action')) {
|
|
56
|
+
const destinationParts = modifiedDestination.split("/");
|
|
57
|
+
modifiedOptions.action = destinationParts[destinationParts.length - 1];
|
|
58
|
+
}
|
|
59
|
+
modifiedOptions.controller = controllerName;
|
|
60
|
+
return [route[0], modifiedDestination, modifiedOptions];
|
|
61
|
+
});
|
|
62
|
+
router.addRoutes(localizedRoutes);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/**** Network Methods *****/
|
|
66
|
+
/**
|
|
67
|
+
* The main entry function of the controller.
|
|
68
|
+
*
|
|
69
|
+
* @param {string} options.action The action called for this controller
|
|
70
|
+
* @param {object} options.params The parameter hash for this action
|
|
71
|
+
* @param {object} options.request The http request object from the http server
|
|
72
|
+
* @param {object} options.response The http response object from the http server
|
|
73
|
+
* @param {object} options.ws The WebSocket instance
|
|
74
|
+
* @param {object} options.next The Express NextFunction
|
|
75
|
+
*/
|
|
76
|
+
async _do({ action, params, request, response, ws, next }) {
|
|
77
|
+
request.handled_by_controller = true;
|
|
78
|
+
const doAction = this[action]; // Action method on the controller
|
|
79
|
+
console.log(this.constructor.name, 'doing', action);
|
|
80
|
+
// TODO: let express handle errors
|
|
81
|
+
if (!doAction || typeof doAction !== 'function') {
|
|
82
|
+
const error = new Error('Action Not Available: #' + action);
|
|
83
|
+
if (response) {
|
|
84
|
+
response.status(404).send({
|
|
85
|
+
error: error.message + '\n' + error.stack
|
|
86
|
+
});
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
if (ws) {
|
|
90
|
+
ws.close(1003, "Action Not available: #" + action);
|
|
91
|
+
}
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
if (ws && !response) { // If it's a websocket request and not an http response
|
|
95
|
+
await doAction.call(this, params, ws, request);
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
// Override async with Controller definition.
|
|
99
|
+
// Not using prototypal function objects
|
|
100
|
+
let actionResponse;
|
|
101
|
+
try {
|
|
102
|
+
actionResponse = await doAction.call(this, params, request, response);
|
|
103
|
+
}
|
|
104
|
+
catch (e) {
|
|
105
|
+
// console.error(e);
|
|
106
|
+
return next(e);
|
|
107
|
+
// response.write({error: e.message, stack: e.stack});
|
|
108
|
+
// actionResponse = 500;
|
|
109
|
+
}
|
|
110
|
+
if ((typeof actionResponse) == 'number') {
|
|
111
|
+
response.sendStatus(actionResponse);
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
if (params._json) {
|
|
115
|
+
console.log("Sending JSON");
|
|
116
|
+
console.log("The ACTION RESPONSE", actionResponse);
|
|
117
|
+
response.json(actionResponse || {});
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
//let viewAction = params._action ? params._action.toString() : "index";
|
|
121
|
+
let viewPath = `${this._getControllerName()}/${action}`;
|
|
122
|
+
const renderView = (templateVars) => {
|
|
123
|
+
if (response.headersSent) {
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
if (!!params._json) {
|
|
127
|
+
response.json(templateVars === undefined ? params : templateVars);
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
response.render(viewPath, templateVars === undefined ? params : templateVars, function (err, html) {
|
|
131
|
+
if (err && err.message.match(/Failed to lookup view/)) {
|
|
132
|
+
response.render(viewPath + ".ejs", params);
|
|
133
|
+
}
|
|
134
|
+
else if (err) {
|
|
135
|
+
console.error(err);
|
|
136
|
+
response.sendStatus(400); // Changed from 400, err to just 400
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
response.send(html);
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
};
|
|
143
|
+
if (!response.headersSent
|
|
144
|
+
&& !params._async
|
|
145
|
+
&& actionResponse !== DISABLE_AUTORENDER) { // TODO: what does this do?
|
|
146
|
+
if (actionResponse instanceof Promise) {
|
|
147
|
+
// TODO: add a timeout so an unresolved promise doesn't preserve the
|
|
148
|
+
// connection indefinitely.
|
|
149
|
+
actionResponse
|
|
150
|
+
.then(renderView)
|
|
151
|
+
.catch((error) => {
|
|
152
|
+
console.error(error);
|
|
153
|
+
response.sendStatus(500); // Changed from 500, error to just 500
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
else {
|
|
157
|
+
renderView(actionResponse);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
const error_codes = [404, 500];
|
|
163
|
+
error_codes.forEach((ec) => {
|
|
164
|
+
Controller.prototype[ec] = function (params, request, response) {
|
|
165
|
+
//this._render('404');
|
|
166
|
+
console.error(ec.toString() + ' error with params', params);
|
|
167
|
+
const error = params['error'] || {};
|
|
168
|
+
const message = error.message || 'Error';
|
|
169
|
+
const stack_trace = error.stack || error || '';
|
|
170
|
+
response.setHeader('content-type', 'text/plain');
|
|
171
|
+
response.statusCode = ec;
|
|
172
|
+
response.end(message + '\n\n' + stack_trace);
|
|
173
|
+
};
|
|
174
|
+
Controller.prototype[ec.toString()] = Controller.prototype[ec];
|
|
175
|
+
});
|
|
176
|
+
export default Controller;
|
|
177
|
+
//# sourceMappingURL=Controller.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Controller.js","sourceRoot":"","sources":["../lib/Controller.ts"],"names":[],"mappings":"AAWA,yBAAyB;AACzB,IAAI,MAAc,CAAC;AACnB,IAAI,gBAA4B,CAAC,CAAC,kDAAkD;AAEpF,kCAAkC;AAClC,MAAM,mBAAmB,GAAG,GAAU,EAAE;IACtC,OAAO,IAAI,KAAK,CACZ,0EAA0E,CAAC,CAAC;AAClF,CAAC,CAAC;AAEF,MAAM,kBAAkB,GAAG,IAAI,CAAC,MAAM,iBAAiB;CAAG,CAAC,CAAC;AAE5D,iCAAiC;AACjC,MAAM,UAAU;IACd,oBAAoB;IACpB,MAAM,CAAC,MAAM,CAAS,CAAC,iCAAiC;IACxD,MAAM,CAAC,MAAM,CAAsB,CAAC,uDAAuD;IAE3F,MAAM,KAAK,kBAAkB;QAC3B,OAAO,kBAAkB,CAAC;IAC5B,CAAC;IAED,MAAM,CAAC,SAAS,CAAC,gBAAwB;QACvC,UAAU,CAAC,MAAM,GAAG,MAAM,GAAG,gBAAgB,CAAC;IAChD,CAAC;IAED,MAAM,CAAC,SAAS,CAAC,MAA2B;QAC1C,UAAU,CAAC,SAAS,CAAC,MAAM,GAAG,MAAM,CAAC;IACvC,CAAC;IAED,sBAAsB;IACtB,MAAM,CAAsB,CAAC,iDAAiD;IAC9E,MAAM,CAAoB,CAAC,4CAA4C;IACvE,IAAI,CAAU;IAEd;QACE,MAAM,cAAc,GAAG,IAAI,CAAC,kBAAkB,EAAE,CAAC;QACjD,MAAM,CAAC,kBAAkB,CAAC,aAAa,GAAG,cAAc,CAAC,CAAC;QAC1D,MAAM,CAAC,EAAE,CAAC,aAAa,GAAG,cAAc,EAAE,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;I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AAC;IACH,CAAC;CACF;AAED,MAAM,WAAW,GAAG,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;AAC/B,WAAW,CAAC,OAAO,CAAC,CAAC,EAAE,EAAE,EAAE;IACzB,UAAU,CAAC,SAAS,CAAC,EAAE,CAAC,GAAG,UAAU,MAA2B,EAAE,OAAgB,EAAE,QAAkB;QACpG,sBAAsB;QACtB,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC,QAAQ,EAAE,GAAG,oBAAoB,EAAE,MAAM,CAAC,CAAC;QAC5D,MAAM,KAAK,GAAG,MAAM,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC;QACpC,MAAM,OAAO,GAAG,KAAK,CAAC,OAAO,IAAI,OAAO,CAAC;QACzC,MAAM,WAAW,GAAG,KAAK,CAAC,KAAK,IAAI,KAAK,IAAI,EAAE,CAAC;QAC/C,QAAQ,CAAC,SAAS,CAAC,cAAc,EAAE,YAAY,CAAC,CAAC;QACjD,QAAQ,CAAC,UAAU,GAAG,EAAE,CAAC;QACzB,QAAQ,CAAC,GAAG,CAAC,OAAO,GAAG,MAAM,GAAG,WAAW,CAAC,CAAC;IAC/C,CAAC,CAAC;IACD,UAAU,CAAC,SAAiB,CAAC,EAAE,CAAC,QAAQ,EAAE,CAAC,GAAG,UAAU,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC;AAC1E,CAAC,CAAC,CAAC;AAEH,eAAe,UAAU,CAAC"}
|
package/dist/Nails.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import https, { type Server as HttpsServer } from 'node:https';
|
|
2
|
+
import { type Server as HttpServer } from 'node:http';
|
|
3
|
+
import { type Application } from 'express-ws';
|
|
4
|
+
import { Model, Sequelize } from 'sequelize';
|
|
5
|
+
import Router from './Router.js';
|
|
6
|
+
import { type Config } from './config.ts';
|
|
7
|
+
import Controller from './Controller.js';
|
|
8
|
+
export default class Nails {
|
|
9
|
+
static Controller: typeof Controller;
|
|
10
|
+
private static instance;
|
|
11
|
+
static get sequelize(): Sequelize;
|
|
12
|
+
static get initialized(): Promise<void>;
|
|
13
|
+
static get config(): Config;
|
|
14
|
+
static get application(): Application;
|
|
15
|
+
static get httpServer(): HttpServer<typeof import("http").IncomingMessage, typeof import("http").ServerResponse>;
|
|
16
|
+
static get httpsServer(): https.Server<typeof import("http").IncomingMessage, typeof import("http").ServerResponse>;
|
|
17
|
+
ready: Promise<boolean>;
|
|
18
|
+
config: Config;
|
|
19
|
+
application: Application;
|
|
20
|
+
Controller: typeof Controller;
|
|
21
|
+
sequelize: Sequelize;
|
|
22
|
+
initialized: Promise<void>;
|
|
23
|
+
router: Router;
|
|
24
|
+
Model: Model;
|
|
25
|
+
httpServer: HttpServer;
|
|
26
|
+
httpsServer: HttpsServer;
|
|
27
|
+
private modelFinalizations;
|
|
28
|
+
private modelMigrations;
|
|
29
|
+
constructor(appConfig: Config);
|
|
30
|
+
configure(): Promise<void>;
|
|
31
|
+
initializeModels(): Promise<void>;
|
|
32
|
+
startServer(): Promise<void>;
|
|
33
|
+
loadModels(absolutePath: string): Promise<void>;
|
|
34
|
+
private initializeControllers;
|
|
35
|
+
}
|
package/dist/Nails.js
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import https from 'node:https';
|
|
4
|
+
import { Model, Sequelize } from 'sequelize';
|
|
5
|
+
import expressApp from './application.js';
|
|
6
|
+
import Router from './Router.js';
|
|
7
|
+
import Controller from './Controller.js';
|
|
8
|
+
export default class Nails {
|
|
9
|
+
static Controller = Controller;
|
|
10
|
+
static instance;
|
|
11
|
+
static get sequelize() {
|
|
12
|
+
return this.instance.sequelize;
|
|
13
|
+
}
|
|
14
|
+
static get initialized() {
|
|
15
|
+
return this.instance.initialized;
|
|
16
|
+
}
|
|
17
|
+
static get config() {
|
|
18
|
+
return this.instance.config;
|
|
19
|
+
}
|
|
20
|
+
static get application() {
|
|
21
|
+
return this.instance.application;
|
|
22
|
+
}
|
|
23
|
+
static get httpServer() {
|
|
24
|
+
return this.instance.httpServer;
|
|
25
|
+
}
|
|
26
|
+
static get httpsServer() {
|
|
27
|
+
return this.instance.httpsServer;
|
|
28
|
+
}
|
|
29
|
+
modelFinalizations = [];
|
|
30
|
+
modelMigrations = [];
|
|
31
|
+
constructor(appConfig) {
|
|
32
|
+
Nails.instance = this;
|
|
33
|
+
this.config = appConfig;
|
|
34
|
+
this.application = expressApp;
|
|
35
|
+
this.Controller = Controller;
|
|
36
|
+
this.sequelize = this.config.db.options
|
|
37
|
+
? new Sequelize(this.config.db.address, this.config.db.options)
|
|
38
|
+
: new Sequelize(this.config.db.address);
|
|
39
|
+
this.router = new Router([]);
|
|
40
|
+
this.initialized = this.configure();
|
|
41
|
+
}
|
|
42
|
+
async configure() {
|
|
43
|
+
expressApp.set('nails_config', { config: this.config });
|
|
44
|
+
expressApp.set('view engine', 'ejs');
|
|
45
|
+
expressApp.set('views', this.config.service.VIEWS_ROOT);
|
|
46
|
+
expressApp.set("public_root", this.config.service.PUBLIC_ROOT);
|
|
47
|
+
console.log("Initializing Router...");
|
|
48
|
+
// this.router = new Router( app_config.routes || [] );
|
|
49
|
+
console.log("Application Router initialized");
|
|
50
|
+
// init models
|
|
51
|
+
try {
|
|
52
|
+
await this.sequelize.authenticate();
|
|
53
|
+
console.log('Connection has been established successfully.');
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
console.error('Unable to connect to the database:', error);
|
|
57
|
+
}
|
|
58
|
+
await this.initializeModels();
|
|
59
|
+
Controller.setRouter(this.router);
|
|
60
|
+
await this.initializeControllers();
|
|
61
|
+
this.router.addRoutes(this.config.routes);
|
|
62
|
+
}
|
|
63
|
+
async initializeModels() {
|
|
64
|
+
await this.loadModels(this.config.service.MODELS_ROOT);
|
|
65
|
+
await Promise.all(this.modelFinalizations.map((finalization) => finalization()));
|
|
66
|
+
// TODO: Implement a migration strategy
|
|
67
|
+
await this.sequelize.sync({ alter: { drop: false } });
|
|
68
|
+
}
|
|
69
|
+
async startServer() {
|
|
70
|
+
await this.initialized;
|
|
71
|
+
console.log("CONFIGURATION COMPLETE");
|
|
72
|
+
// TODO: Use logging middleware.
|
|
73
|
+
// Use the router middleware.
|
|
74
|
+
expressApp.use(this.router.expressRouter);
|
|
75
|
+
var host = this.config.service.HOST || 'localhost';
|
|
76
|
+
var port = this.config.service.PORT || 3000;
|
|
77
|
+
let startHttp = 'ENABLE_HTTP' in this.config.service && !!this.config.service.ENABLE_HTTP;
|
|
78
|
+
let startHttps = !!this.config.service.ENABLE_HTTPS;
|
|
79
|
+
if (!startHttp && !startHttps) {
|
|
80
|
+
console.error("Either ENABLE_HTTPS or ENABLE_HTTP must be set for nails to start");
|
|
81
|
+
}
|
|
82
|
+
let atLeastOneServerStarted = false;
|
|
83
|
+
return new Promise((resolve, reject) => {
|
|
84
|
+
if (atLeastOneServerStarted)
|
|
85
|
+
return resolve();
|
|
86
|
+
let serverStartedCallback = () => {
|
|
87
|
+
console.log("Started");
|
|
88
|
+
atLeastOneServerStarted = true;
|
|
89
|
+
resolve();
|
|
90
|
+
};
|
|
91
|
+
if (startHttp) {
|
|
92
|
+
console.log("starting nails HTTP server. listening to ", host + ':' + port);
|
|
93
|
+
if (this.httpServer) {
|
|
94
|
+
console.warn("Attempted to recreate http server");
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
this.httpServer = expressApp.listen(port, host, serverStartedCallback);
|
|
98
|
+
console.log("Done starting HTTP server");
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (startHttps) {
|
|
102
|
+
if (this.httpsServer) {
|
|
103
|
+
console.warn("Attempted to recreate HTTPS server");
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
console.log(`starting nails HTTPS server. Listening to ${host}:${this.config.service.SSL_PORT}`);
|
|
107
|
+
this.httpsServer = https.createServer({
|
|
108
|
+
key: this.config.service.PRIVATE_KEY,
|
|
109
|
+
cert: this.config.service.CERTIFICATE
|
|
110
|
+
}, expressApp).listen(this.config.service.SSL_PORT, serverStartedCallback);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
// TODO: Set a startup timeout and reject the promise so it does not hang forever;
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
async loadModels(absolutePath) {
|
|
117
|
+
if (!fs.existsSync(absolutePath))
|
|
118
|
+
return console.log('Cannot initialize. Path not found.', absolutePath);
|
|
119
|
+
if (!fs.statSync(absolutePath).isFile()) {
|
|
120
|
+
const directory_contents = fs.readdirSync(absolutePath);
|
|
121
|
+
for (const rel_path of directory_contents) {
|
|
122
|
+
await this.loadModels(path.join(absolutePath, rel_path));
|
|
123
|
+
}
|
|
124
|
+
;
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
console.log('attempting to import Model:', absolutePath);
|
|
128
|
+
// We just need to import each model once so the generateSuperclass
|
|
129
|
+
// method is called at least once for each model.
|
|
130
|
+
const modelModule = await import(absolutePath);
|
|
131
|
+
// TODO: make sure the name isn't constantly "model"
|
|
132
|
+
const modelClass = modelModule.default;
|
|
133
|
+
if (!(modelClass.prototype instanceof Model)) {
|
|
134
|
+
const errorMessage = `Default export is not a sequelize Model: ${absolutePath}`;
|
|
135
|
+
console.error(errorMessage);
|
|
136
|
+
throw errorMessage;
|
|
137
|
+
}
|
|
138
|
+
const schema = modelModule.schema;
|
|
139
|
+
const options = modelModule.options;
|
|
140
|
+
const defer = modelModule.defer;
|
|
141
|
+
const afterInitialize = modelModule.afterInitialize;
|
|
142
|
+
if (modelModule.finalize) {
|
|
143
|
+
this.modelFinalizations.push(modelModule.finalize);
|
|
144
|
+
}
|
|
145
|
+
if (modelModule.afterInitializeAll) {
|
|
146
|
+
this.modelFinalizations.push(modelModule.afterInitializeAll);
|
|
147
|
+
}
|
|
148
|
+
this.modelMigrations.push(modelModule.migrate);
|
|
149
|
+
modelClass.init(schema, { sequelize: this.sequelize, ...options });
|
|
150
|
+
if (modelClass && modelClass.name) {
|
|
151
|
+
console.log('imported model:', modelClass.name);
|
|
152
|
+
if (defer) {
|
|
153
|
+
await defer();
|
|
154
|
+
}
|
|
155
|
+
if (afterInitialize) {
|
|
156
|
+
await afterInitialize();
|
|
157
|
+
}
|
|
158
|
+
await modelClass.sync();
|
|
159
|
+
}
|
|
160
|
+
else
|
|
161
|
+
console.warn("No model found at:", absolutePath);
|
|
162
|
+
}
|
|
163
|
+
async initializeControllers(directory) {
|
|
164
|
+
if (!directory)
|
|
165
|
+
directory = this.config.service.CONTROLLERS_ROOT;
|
|
166
|
+
console.log('attempting to import:', directory);
|
|
167
|
+
if (!fs.existsSync(directory))
|
|
168
|
+
return console.error('Cannot initialize. Path not found.', directory);
|
|
169
|
+
if (fs.statSync(directory).isFile()) {
|
|
170
|
+
let subclass = (await import(directory)).default;
|
|
171
|
+
if (!(Controller.isPrototypeOf(subclass)))
|
|
172
|
+
return console.error(`Non-Controller export "${subclass.name}: ${typeof subclass}" found: ${directory}`);
|
|
173
|
+
const controller = new subclass();
|
|
174
|
+
controller._registerControllerRoutes();
|
|
175
|
+
return controller;
|
|
176
|
+
}
|
|
177
|
+
for (const rel_path of fs.readdirSync(directory)) {
|
|
178
|
+
await this.initializeControllers(path.join(directory, rel_path));
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
// const sequelize = new Sequelize();
|
|
183
|
+
//# sourceMappingURL=Nails.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Nails.js","sourceRoot":"","sources":["../lib/Nails.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,KAAmC,MAAM,YAAY,CAAC;AAI7D,OAAO,EAAoB,KAAK,EAAE,SAAS,EAA0C,MAAM,WAAW,CAAC;AACvG,OAAO,UAAU,MAAM,kBAAkB,CAAC;AAC1C,OAAO,MAAM,MAAM,aAAa,C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BAA0B,QAAQ,CAAC,IAAI,KAAK,OAAO,QAAQ,YAAY,SAAS,EAAE,CAAC,CAAC;YAC3G,MAAM,UAAU,GAAG,IAAI,QAAQ,EAAE,CAAC;YAClC,UAAU,CAAC,yBAAyB,EAAE,CAAC;YACvC,OAAO,UAAU,CAAC;QACpB,CAAC;QACD,KAAK,MAAM,QAAQ,IAAI,EAAE,CAAC,WAAW,CAAC,SAAS,CAAC,EAAE,CAAC;YACjD,MAAM,IAAI,CAAC,qBAAqB,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC,CAAC;QACnE,CAAC;IACH,CAAC;;AAIH,qCAAqC"}
|
package/dist/Router.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { EventEmitter } from 'node:events';
|
|
2
|
+
import { type WebSocket } from "ws";
|
|
3
|
+
import { type NextFunction, type Request, type Response } from 'express';
|
|
4
|
+
import { type Router as WsRouter, type WebsocketRequestHandler } from 'express-ws';
|
|
5
|
+
import { type RouteDefinition, type RouteOptions } from './config.js';
|
|
6
|
+
interface ExpressWsType {
|
|
7
|
+
app: any;
|
|
8
|
+
getWss: () => any;
|
|
9
|
+
applyTo: (router: any) => void;
|
|
10
|
+
}
|
|
11
|
+
declare class NailsRouter extends EventEmitter {
|
|
12
|
+
private static _webSocketsEnabled;
|
|
13
|
+
private static _expressWs;
|
|
14
|
+
static get webSocketsEnabled(): boolean;
|
|
15
|
+
static set webSocketsEnabled(enabled: boolean);
|
|
16
|
+
static set expressWs(ews: ExpressWsType);
|
|
17
|
+
static get expressWs(): ExpressWsType;
|
|
18
|
+
private application;
|
|
19
|
+
expressRouter: WsRouter;
|
|
20
|
+
routes: any[];
|
|
21
|
+
constructor(routes: RouteDefinition[]);
|
|
22
|
+
addRoutes(routes: RouteDefinition[]): void;
|
|
23
|
+
addRoute(route: RouteDefinition): void;
|
|
24
|
+
routeWs(ws: WebSocket, request: Request): void;
|
|
25
|
+
route(request: Request, response: Response, next: NextFunction): void;
|
|
26
|
+
get_websocket_handler(routeOptions: RouteOptions | undefined): WebsocketRequestHandler;
|
|
27
|
+
get_express_route_handler(route_options: RouteOptions | undefined): (request: Request, response: Response, next: NextFunction) => Promise<void>;
|
|
28
|
+
}
|
|
29
|
+
export default NailsRouter;
|
package/dist/Router.js
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { EventEmitter } from 'node:events';
|
|
2
|
+
import app, { expressRouter, expressStatic } from './application.js';
|
|
3
|
+
class NailsRouter extends EventEmitter {
|
|
4
|
+
static _webSocketsEnabled = true;
|
|
5
|
+
static _expressWs;
|
|
6
|
+
static get webSocketsEnabled() {
|
|
7
|
+
return this._webSocketsEnabled;
|
|
8
|
+
}
|
|
9
|
+
static set webSocketsEnabled(enabled) {
|
|
10
|
+
this._webSocketsEnabled = enabled;
|
|
11
|
+
}
|
|
12
|
+
static set expressWs(ews) {
|
|
13
|
+
this._expressWs = ews;
|
|
14
|
+
}
|
|
15
|
+
static get expressWs() {
|
|
16
|
+
return this._expressWs;
|
|
17
|
+
}
|
|
18
|
+
application = app; // TODO: Type this more specifically if application.js is also converted to TS
|
|
19
|
+
expressRouter = expressRouter; // express-ws adds a ws method
|
|
20
|
+
routes; // TODO: Type this more specifically
|
|
21
|
+
constructor(routes) {
|
|
22
|
+
super();
|
|
23
|
+
this.addRoutes(routes);
|
|
24
|
+
}
|
|
25
|
+
addRoutes(routes) {
|
|
26
|
+
this.routes = routes;
|
|
27
|
+
routes.forEach(this.addRoute.bind(this));
|
|
28
|
+
}
|
|
29
|
+
addRoute(route) {
|
|
30
|
+
const method = route[0].toLowerCase();
|
|
31
|
+
const path_matcher = route[1];
|
|
32
|
+
const consequences = route[2];
|
|
33
|
+
console.log("setting route for", method, path_matcher, consequences);
|
|
34
|
+
if (method.match(/ws/i)) {
|
|
35
|
+
this.expressRouter.ws(path_matcher, this.get_websocket_handler(consequences));
|
|
36
|
+
}
|
|
37
|
+
else if (consequences && consequences.public) {
|
|
38
|
+
this.expressRouter.use(path_matcher, expressStatic(this.application.get('public_root')));
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
this.expressRouter[method](path_matcher, this.get_express_route_handler(consequences));
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
routeWs(ws, request) {
|
|
45
|
+
console.log('\nSERVER_LOG:: WebSocket Router received request for: ', request.method, request.url, '\n');
|
|
46
|
+
const params = { ...request.params, ...request.query };
|
|
47
|
+
const controller = params._controller;
|
|
48
|
+
const action = params._action;
|
|
49
|
+
console.log('the action is:', controller, action, params);
|
|
50
|
+
console.log('the params are:', params);
|
|
51
|
+
console.log('ws router emitting:', 'dispatchTo:' + controller);
|
|
52
|
+
this.emit('dispatchTo:' + controller, { action, params, request, ws });
|
|
53
|
+
if (!request.handled_by_controller) {
|
|
54
|
+
params.error = { message: 'controller ' + controller + ' does not exist' };
|
|
55
|
+
console.log('closing websocket');
|
|
56
|
+
return ws.close(1003, "Action Not available: #" + action);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
route(request, response, next) {
|
|
60
|
+
console.log('\nSERVER_LOG:: Router recieved request for: ', request.method, request.url, '\n');
|
|
61
|
+
const params = { ...request.params, ...request.query };
|
|
62
|
+
const controller = params._controller;
|
|
63
|
+
const action = params._action;
|
|
64
|
+
console.log('the action is:', controller, action, params);
|
|
65
|
+
console.log('the params are:', params);
|
|
66
|
+
console.log('router emitting:', 'dispatchTo:' + controller);
|
|
67
|
+
this.emit('dispatchTo:' + controller, { action, params, request, response, next });
|
|
68
|
+
params.error = { message: 'controller ' + controller + ' does not exist' };
|
|
69
|
+
if (!request.handled_by_controller) {
|
|
70
|
+
this.emit('dispatchTo:application', { action: 404, params, request, response });
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
get_websocket_handler(routeOptions) {
|
|
74
|
+
const route_options = routeOptions || {};
|
|
75
|
+
return (ws, request) => {
|
|
76
|
+
console.log("handling a websocket request");
|
|
77
|
+
let controller = route_options.controller;
|
|
78
|
+
let action = route_options.action;
|
|
79
|
+
for (let i = 0; route_options[i]; i++) {
|
|
80
|
+
if (route_options[i] === 'action') {
|
|
81
|
+
action = action || request.params[i];
|
|
82
|
+
}
|
|
83
|
+
else if (route_options[i] === 'controller') {
|
|
84
|
+
controller = controller || request.params[i];
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
request.params[route_options[i]] = request.params[i];
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
request.params._action = action || request.params.action || "index";
|
|
91
|
+
request.params._controller =
|
|
92
|
+
controller || request.params.controller || "application";
|
|
93
|
+
this.routeWs(ws, request);
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
get_express_route_handler(route_options) {
|
|
97
|
+
route_options = route_options || {};
|
|
98
|
+
return async (request, response, next) => {
|
|
99
|
+
let controller = route_options['controller'];
|
|
100
|
+
let action = route_options['action'];
|
|
101
|
+
let json = !!route_options['json'];
|
|
102
|
+
let path = route_options['path'];
|
|
103
|
+
let async = route_options['async'];
|
|
104
|
+
let autorender = route_options['disable_autorender'];
|
|
105
|
+
for (let i = 0; route_options[i]; i++) {
|
|
106
|
+
if (route_options[i] === 'action') {
|
|
107
|
+
action = action || request.params[i];
|
|
108
|
+
}
|
|
109
|
+
else if (route_options[i] === 'controller') {
|
|
110
|
+
controller = controller || request.params[i];
|
|
111
|
+
}
|
|
112
|
+
else if (route_options[i] === 'json') {
|
|
113
|
+
json = json || request.params[i];
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
request.params[route_options[i]] = request.params[i];
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
request.params._action = action || request.params.action || "index";
|
|
120
|
+
request.params._controller =
|
|
121
|
+
controller || request.params.controller || "application";
|
|
122
|
+
request.params._json = json || !!request.params.json;
|
|
123
|
+
request.params._async = async == null || async == undefined ?
|
|
124
|
+
!!this.application.get('nails_config').config.ASYNC : async;
|
|
125
|
+
request.params._autorender = autorender ? !!autorender : true;
|
|
126
|
+
this.route(request, response, next);
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
export default NailsRouter;
|
|
131
|
+
//# sourceMappingURL=Router.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Router.js","sourceRoot":"","sources":["../lib/Router.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAI3C,OAAO,GAAG,EAAE,EAAC,aAAa,EAAiB,aAAa,EAAC,MAAM,kBAAkB,CAAC;AASlF,MAAM,WAAY,SAAQ,YAAY;IAC5B,MAAM,CAAC,kBAAkB,GAAY,IAAI,CAAC;IAC1C,MAAM,CAAC,UAAU,CAAgB;IAEzC,MAAM,KAAK,iBAAiB;QAC1B,OAAO,IAAI,CAAC,kBAAkB,CAAC;IACjC,CAAC;IAED,MAAM,KAAK,iBAAiB,CAAC,OAAgB;QAC3C,IAAI,CAAC,kBAAkB,GAAG,OAAO,CAAC;IACpC,CAAC;IAED,MAAM,KAAK,SAAS,CAAC,GAAkB;QACrC,IAAI,CAAC,UAAU,GAAG,GAAG,CAAC;IACxB,CAAC;IAED,MAAM,KAAK,SAAS;QAClB,OAAO,IAAI,CAAC,UAAU,CAAC;IACzB,CAAC;IAEO,WAAW,GAAkB,GAAG,CAAC,CAAC,8EAA8E;IACjH,aAAa,GAAa,aAAa,CAAC,CAAC,8BAA8B;IACvE,MAAM,CAAQ,CAAC,oCAAoC;IAE1D,YAAY,MAAyB;QACnC,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;IACzB,CAAC;IAED,SAAS,CAAC,MAAyB;QACjC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;IAC3C,CAAC;IAED,QAAQ,CAAC,KAAsB;QAC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BAAM,CAAC;oBACL,OAAO,CAAC,MAAc,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC,GAAI,OAAO,CAAC,MAAc,CAAC,CAAC,CAAC,CAAC;gBACzE,CAAC;YACH,CAAC;YAEA,OAAO,CAAC,MAAc,CAAC,OAAO,GAAG,MAAM,IAAK,OAAO,CAAC,MAAc,CAAC,MAAM,IAAI,OAAO,CAAC;YACrF,OAAO,CAAC,MAAc,CAAC,WAAW;gBACjC,UAAU,IAAK,OAAO,CAAC,MAAc,CAAC,UAAU,IAAI,aAAa,CAAC;YACnE,OAAO,CAAC,MAAc,CAAC,KAAK,GAAG,IAAI,IAAI,CAAC,CAAE,OAAO,CAAC,MAAc,CAAC,IAAI,CAAC;YACtE,OAAO,CAAC,MAAc,CAAC,MAAM,GAAG,KAAK,IAAI,IAAI,IAAI,KAAK,IAAI,SAAS,CAAC,CAAC;gBACpE,CAAC,CAAC,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC;YAC7D,OAAO,CAAC,MAAc,CAAC,WAAW,GAAG,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,IAAI,CAAC;YAEvE,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,QAAQ,EAAE,IAAI,CAAC,CAAC;QACtC,CAAC,CAAC;IACJ,CAAC;;AAGH,eAAe,WAAW,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
import expressWs from 'express-ws';
|
|
3
|
+
/**
|
|
4
|
+
* Singleton express application.
|
|
5
|
+
*/
|
|
6
|
+
declare const app: expressWs.Application;
|
|
7
|
+
export declare const ExpressRouter: typeof express.Router;
|
|
8
|
+
export declare const expressRouter: expressWs.Router;
|
|
9
|
+
export declare const expressStatic: import("serve-static").RequestHandlerConstructor<express.Response<any, Record<string, any>>>;
|
|
10
|
+
export default app;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// const bodyParser = require('body-parser');
|
|
2
|
+
import pkg from 'body-parser';
|
|
3
|
+
const { urlencoded, json } = pkg;
|
|
4
|
+
import express from 'express';
|
|
5
|
+
import expressWs from 'express-ws';
|
|
6
|
+
import cookieParser from 'cookie-parser';
|
|
7
|
+
/**
|
|
8
|
+
* Singleton express application.
|
|
9
|
+
*/
|
|
10
|
+
// var express = require('express');
|
|
11
|
+
const { app, applyTo } = expressWs(express());
|
|
12
|
+
app.use(cookieParser());
|
|
13
|
+
// TODO: this has to be done before routes in order to work. Can consider allowing the config to turn this off
|
|
14
|
+
// expressWs(app);
|
|
15
|
+
// Parse application/x-www-form-urlencoded
|
|
16
|
+
app.use(urlencoded({ limit: '2mb', extended: false }));
|
|
17
|
+
// Parse application/json
|
|
18
|
+
app.use(json({ limit: '2mb' }));
|
|
19
|
+
export const ExpressRouter = express.Router;
|
|
20
|
+
export const expressRouter = express.Router();
|
|
21
|
+
applyTo(expressRouter);
|
|
22
|
+
export const expressStatic = express.static;
|
|
23
|
+
// export ExpressRouter;
|
|
24
|
+
// export expressStatic;
|
|
25
|
+
export default app;
|
|
26
|
+
//# sourceMappingURL=application.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"application.js","sourceRoot":"","sources":["../lib/application.ts"],"names":[],"mappings":"AAAA,6CAA6C;AAC7C,OAAO,GAAG,MAAM,aAAa,CAAC;AAC9B,MAAM,EAAC,UAAU,EAAE,IAAI,EAAC,GAAG,GAAG,CAAC;AAC/B,OAAO,OAAO,MAAM,SAAS,CAAC;AAC9B,OAAO,SAAS,MAAM,YAAY,CAAC;AACnC,OAAO,YAAY,MAAM,eAAe,CAAC;AAEzC;;GAEG;AACH,oCAAoC;AACpC,MAAM,EAAC,GAAG,EAAE,OAAO,EAAC,GAAG,SAAS,CAAC,OAAO,EAAE,CAAC,CAAC;AAC5C,GAAG,CAAC,GAAG,CAAC,YAAY,EAAE,CAAC,CAAC;AACxB,8GAA8G;AAC9G,kBAAkB;AAClB,0CAA0C;AAC1C,GAAG,CAAC,GAAG,CAAC,UAAU,CAAC,EAAE,KAAK,EAAE,KAAK,EAAE,QAAQ,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC;AACvD,yBAAyB;AACzB,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,EAAC,KAAK,EAAE,KAAK,EAAC,CAAC,CAAC,CAAC;AAE9B,MAAM,CAAC,MAAM,aAAa,GAAG,OAAO,CAAC,MAAM,CAAC;AAC5C,MAAM,CAAC,MAAM,aAAa,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;AAC9C,OAAO,CAAC,aAAa,CAAC,CAAC;AACvB,MAAM,CAAC,MAAM,aAAa,GAAG,OAAO,CAAC,MAAM,CAAC;AAE5C,wBAAwB;AACxB,wBAAwB;AAExB,eAAe,GAAG,CAAC"}
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { type Options } from "sequelize";
|
|
2
|
+
import { type NextFunction, type Request, type Response } from "express";
|
|
3
|
+
export interface ControllerDoRouteParams {
|
|
4
|
+
action: string;
|
|
5
|
+
params: Record<string, any>;
|
|
6
|
+
request: Request;
|
|
7
|
+
response: Response;
|
|
8
|
+
ws?: WebSocket;
|
|
9
|
+
next: NextFunction;
|
|
10
|
+
}
|
|
11
|
+
interface ActionParams<T> {
|
|
12
|
+
params: T;
|
|
13
|
+
request: Request;
|
|
14
|
+
response: Response;
|
|
15
|
+
}
|
|
16
|
+
export interface RouteOptions {
|
|
17
|
+
controller?: string;
|
|
18
|
+
action?: string | (<T>(actionParams: ActionParams<T>) => any);
|
|
19
|
+
public?: boolean;
|
|
20
|
+
json?: boolean;
|
|
21
|
+
path?: string;
|
|
22
|
+
async?: boolean;
|
|
23
|
+
disable_autorender?: boolean;
|
|
24
|
+
[key: number]: string;
|
|
25
|
+
}
|
|
26
|
+
type RouteMatcher = string;
|
|
27
|
+
type HttpMethod = 'get' | 'post' | 'put' | 'delete' | 'ws';
|
|
28
|
+
export type RouteDefinition = [HttpMethod, RouteMatcher, RouteOptions?];
|
|
29
|
+
export interface RoutesConfig extends Array<RouteDefinition> {
|
|
30
|
+
}
|
|
31
|
+
interface MimesConfig {
|
|
32
|
+
[key: string]: any;
|
|
33
|
+
}
|
|
34
|
+
export interface DbConfig {
|
|
35
|
+
address: string;
|
|
36
|
+
username?: string;
|
|
37
|
+
password?: string;
|
|
38
|
+
options?: Options;
|
|
39
|
+
}
|
|
40
|
+
interface AppConfig {
|
|
41
|
+
APP_ROOT: string;
|
|
42
|
+
PUBLIC_ROOT: string;
|
|
43
|
+
CONTROLLERS_ROOT: string;
|
|
44
|
+
VIEWS_ROOT: string;
|
|
45
|
+
MODELS_ROOT: string;
|
|
46
|
+
SERVER_ROOT: string;
|
|
47
|
+
ENABLE_HTTP: boolean;
|
|
48
|
+
HOST: string;
|
|
49
|
+
PORT: number;
|
|
50
|
+
ENABLE_HTTPS: boolean;
|
|
51
|
+
SSL_PORT: number;
|
|
52
|
+
PRIVATE_KEY: Buffer;
|
|
53
|
+
CERTIFICATE: Buffer;
|
|
54
|
+
}
|
|
55
|
+
export interface Config {
|
|
56
|
+
service: AppConfig;
|
|
57
|
+
routes: RoutesConfig;
|
|
58
|
+
mimes: MimesConfig;
|
|
59
|
+
db: DbConfig;
|
|
60
|
+
}
|
|
61
|
+
export {};
|
package/dist/config.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../lib/config.ts"],"names":[],"mappings":""}
|