@ccci/micro-server 1.0.46 → 1.0.47
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/index.js +31 -29
- package/dist/utils/RouterFactory.d.ts +2 -2
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -87637,32 +87637,35 @@ var ISSUER = process.env.JWT_ISSUER ? process.env.JWT_ISSUER : "*.service";
|
|
|
87637
87637
|
var UNSECURED_DIR = ["dev", "public"];
|
|
87638
87638
|
|
|
87639
87639
|
class RouterFactory {
|
|
87640
|
-
|
|
87641
|
-
|
|
87642
|
-
|
|
87643
|
-
|
|
87644
|
-
|
|
87645
|
-
|
|
87646
|
-
|
|
87647
|
-
|
|
87648
|
-
|
|
87649
|
-
|
|
87650
|
-
|
|
87651
|
-
|
|
87652
|
-
|
|
87653
|
-
|
|
87654
|
-
|
|
87655
|
-
|
|
87656
|
-
|
|
87657
|
-
|
|
87640
|
+
static async init(app, pathName, folderName = "routes", context = "api") {
|
|
87641
|
+
return new Promise((resolve, reject) => {
|
|
87642
|
+
try {
|
|
87643
|
+
Logger.info("Create API Routes...\n");
|
|
87644
|
+
fs2.readdirSync(folderName).forEach((dir) => {
|
|
87645
|
+
let isUnsecure = UNSECURED_DIR.findIndex((x) => x === dir) > -1;
|
|
87646
|
+
var routerDir = path.join(folderName, dir);
|
|
87647
|
+
fs2.readdirSync(routerDir).forEach((file) => {
|
|
87648
|
+
let fullName = path.join(routerDir, file);
|
|
87649
|
+
if (file.toLowerCase().indexOf(".js") || file.toLowerCase().indexOf(".ts")) {
|
|
87650
|
+
let fileName = fullName.replace(folderName, context).replace(".js", "").replace(".ts", "").split("\\").join("/");
|
|
87651
|
+
let endpoint = `/${fileName}`;
|
|
87652
|
+
let routeFile = `${pathName}/${fullName}`;
|
|
87653
|
+
import(routeFile).then((routerClass) => {
|
|
87654
|
+
let router = new routerClass.default;
|
|
87655
|
+
app.use(endpoint, (req, res, next) => RouterFactory.authenticate(req, res, next, isUnsecure || router.grantPublicAccess), router.getRoutes());
|
|
87656
|
+
Logger.info("Initializing endpoint:", endpoint);
|
|
87657
|
+
});
|
|
87658
|
+
}
|
|
87659
|
+
});
|
|
87660
|
+
resolve();
|
|
87658
87661
|
});
|
|
87659
|
-
})
|
|
87660
|
-
|
|
87661
|
-
|
|
87662
|
-
|
|
87663
|
-
}
|
|
87662
|
+
} catch (error) {
|
|
87663
|
+
Logger.error(error);
|
|
87664
|
+
reject(error);
|
|
87665
|
+
}
|
|
87666
|
+
});
|
|
87664
87667
|
}
|
|
87665
|
-
authenticate(req, res, next, grantPublicAccess = false) {
|
|
87668
|
+
static authenticate(req, res, next, grantPublicAccess = false) {
|
|
87666
87669
|
try {
|
|
87667
87670
|
let bearerToken = req.headers.authorization;
|
|
87668
87671
|
if (bearerToken && bearerToken != null && bearerToken != "null") {
|
|
@@ -87741,7 +87744,6 @@ class ApplicationServer {
|
|
|
87741
87744
|
static app;
|
|
87742
87745
|
static async bootstrap(port, options) {
|
|
87743
87746
|
ApplicationServer.app = import_express.default();
|
|
87744
|
-
Logger.info("Setting up application middlewares...\n");
|
|
87745
87747
|
ApplicationServer.app.use(import_cors.default());
|
|
87746
87748
|
ApplicationServer.app.use(helmet());
|
|
87747
87749
|
ApplicationServer.app.use(import_express.default.json());
|
|
@@ -87752,13 +87754,13 @@ class ApplicationServer {
|
|
|
87752
87754
|
ApplicationServer.app.get("/", (req, resp) => {
|
|
87753
87755
|
return resp.sendFile(path3.resolve("index.html"));
|
|
87754
87756
|
});
|
|
87755
|
-
|
|
87757
|
+
RouterFactory.init(ApplicationServer.app, process.cwd(), "routes");
|
|
87756
87758
|
const server = ApplicationServer.app.listen(port, () => {
|
|
87757
87759
|
Logger.info(`API Sever ready at: http://127.0.0.1:${port}\n`);
|
|
87760
|
+
if (options?.enableWebSocket) {
|
|
87761
|
+
new WebSocketServer(options?.websocketPort, process.cwd(), options?.websocketDir);
|
|
87762
|
+
}
|
|
87758
87763
|
});
|
|
87759
|
-
if (options?.enableWebSocket) {
|
|
87760
|
-
new WebSocketServer(options?.websocketPort, process.cwd(), options?.websocketDir);
|
|
87761
|
-
}
|
|
87762
87764
|
}
|
|
87763
87765
|
getInstance() {
|
|
87764
87766
|
return ApplicationServer.app;
|
|
@@ -6,7 +6,7 @@ export default class RouterFactory {
|
|
|
6
6
|
* @param pathName root path of the application
|
|
7
7
|
* @param folderName path of the routers (default: routes)
|
|
8
8
|
*/
|
|
9
|
-
|
|
10
|
-
authenticate(req: Request, res: Response, next: NextFunction, grantPublicAccess?: boolean): Response<any, Record<string, any>> | undefined;
|
|
9
|
+
static init(app: Application, pathName: string, folderName?: string, context?: string): Promise<void>;
|
|
10
|
+
static authenticate(req: Request, res: Response, next: NextFunction, grantPublicAccess?: boolean): Response<any, Record<string, any>> | undefined;
|
|
11
11
|
}
|
|
12
12
|
//# sourceMappingURL=RouterFactory.d.ts.map
|