2023-10-26 17:44:54 +03:00
|
|
|
import express from "express";
|
|
|
|
import { Routes } from "../interfaces/router";
|
|
|
|
import cors from "cors";
|
2023-11-10 12:06:40 +03:00
|
|
|
import { Server } from "socket.io";
|
|
|
|
import { createServer } from "http";
|
|
|
|
import { SocketSubscriber } from "./socket_controller";
|
2023-11-14 20:44:06 +03:00
|
|
|
import { dirname } from "path";
|
2023-11-20 00:48:40 +03:00
|
|
|
import fileUpload from "express-fileupload";
|
|
|
|
import { SetLastActivePipelineToRealTimeServiceUseCase } from "../usecases/set_active_pipeline_to_realtime_service_usecase";
|
|
|
|
import { CheckAndCreateStaticFilesFolderUseCase } from "../usecases/check_and_create_static_files_folder_usecase";
|
|
|
|
import { DataBaseConnectUseCase } from "../usecases/database_connect_usecase";
|
2023-10-26 17:44:54 +03:00
|
|
|
|
|
|
|
export class App {
|
|
|
|
public app: express.Application;
|
|
|
|
public port: number;
|
|
|
|
public env: string;
|
2023-11-10 12:06:40 +03:00
|
|
|
public socketSubscribers: SocketSubscriber<any>[];
|
|
|
|
public io: Server;
|
|
|
|
|
|
|
|
constructor(routes: Routes[], socketSubscribers: SocketSubscriber<any>[]) {
|
|
|
|
this.port = 4001;
|
|
|
|
this.socketSubscribers = socketSubscribers;
|
2023-10-26 17:44:54 +03:00
|
|
|
this.env = "dev";
|
2023-11-01 18:24:43 +03:00
|
|
|
this.app = express();
|
2023-10-27 21:22:48 +03:00
|
|
|
this.loadAppDependencies().then(() => {
|
|
|
|
this.initializeMiddlewares();
|
|
|
|
this.initializeRoutes(routes);
|
|
|
|
});
|
2023-10-26 17:44:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public listen() {
|
2023-11-10 12:06:40 +03:00
|
|
|
const httpServer = createServer(this.app);
|
|
|
|
const io = new Server(httpServer, {
|
|
|
|
cors: { origin: "*" },
|
|
|
|
});
|
|
|
|
|
|
|
|
io.on("connection", (socket) => {
|
|
|
|
this.socketSubscribers.map((el) => {
|
|
|
|
el.emitter.on((e) => {
|
|
|
|
socket.emit(el.event, e);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2023-10-26 17:44:54 +03:00
|
|
|
|
2023-11-10 12:06:40 +03:00
|
|
|
httpServer.listen(this.port, () => {
|
2023-10-26 17:44:54 +03:00
|
|
|
console.info(`=================================`);
|
|
|
|
console.info(`======= ENV: ${this.env} =======`);
|
|
|
|
console.info(`🚀 HTTP http://localhost:${this.port}`);
|
|
|
|
console.info(`🚀 WS ws://localhost:${this.port}`);
|
|
|
|
console.info(`=================================`);
|
|
|
|
});
|
2023-11-10 12:06:40 +03:00
|
|
|
this.io = io;
|
2023-10-26 17:44:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public getServer() {
|
|
|
|
return this.app;
|
|
|
|
}
|
|
|
|
|
|
|
|
private initializeMiddlewares() {
|
|
|
|
this.app.use(cors());
|
|
|
|
this.app.use(express.json());
|
|
|
|
this.app.use(express.urlencoded({ extended: true }));
|
2023-11-14 20:44:06 +03:00
|
|
|
this.app.use(express.static("public"));
|
2023-11-20 00:48:40 +03:00
|
|
|
this.app.use(
|
|
|
|
fileUpload({
|
|
|
|
createParentPath: true,
|
|
|
|
})
|
|
|
|
);
|
2023-10-26 17:44:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private initializeRoutes(routes: Routes[]) {
|
|
|
|
routes.forEach((route) => {
|
|
|
|
this.app.use("/", route.router);
|
|
|
|
});
|
|
|
|
}
|
2023-10-27 21:22:48 +03:00
|
|
|
async loadAppDependencies() {
|
2023-11-20 00:48:40 +03:00
|
|
|
await new DataBaseConnectUseCase().call();
|
|
|
|
await new CheckAndCreateStaticFilesFolderUseCase().call();
|
|
|
|
await new SetLastActivePipelineToRealTimeServiceUseCase().call();
|
2023-10-26 17:44:54 +03:00
|
|
|
}
|
2023-11-14 20:44:06 +03:00
|
|
|
|
|
|
|
static staticFilesStoreDir = () => {
|
|
|
|
const dir = dirname(__filename);
|
|
|
|
const rootDir = dir.slice(0, dir.length - 20);
|
|
|
|
return rootDir + "public/";
|
|
|
|
};
|
2023-10-26 17:44:54 +03:00
|
|
|
}
|