80 lines
2.1 KiB
TypeScript
80 lines
2.1 KiB
TypeScript
![]() |
import express from "express";
|
||
|
import { Routes } from "../interfaces/router";
|
||
|
import cors from "cors";
|
||
|
import locator from "../di/register_di";
|
||
|
import { DevEnv, UnitTestEnv } from "../di/env";
|
||
|
import mongoose from "mongoose";
|
||
|
import http from "http";
|
||
|
import { Server } from "socket.io";
|
||
|
|
||
|
export class App {
|
||
|
public app: express.Application;
|
||
|
public port: number;
|
||
|
public env: string;
|
||
|
public computedFolder: string;
|
||
|
// public io:
|
||
|
constructor(routes: Routes[], computedFolder: string) {
|
||
|
this.app = express();
|
||
|
this.port = Number(process.env.PORT) || 3000;
|
||
|
this.env = "dev";
|
||
|
this.initializeMiddlewares();
|
||
|
this.initializeRoutes(routes);
|
||
|
this.loadAppDependencies();
|
||
|
this.computedFolder = computedFolder;
|
||
|
}
|
||
|
|
||
|
public listen() {
|
||
|
const httpServer = new http.Server(this.app);
|
||
|
// const io = new Server(httpServer);
|
||
|
|
||
|
httpServer.listen(this.port, () => {
|
||
|
console.info(`=================================`);
|
||
|
console.info(`======= ENV: ${this.env} =======`);
|
||
|
console.info(`🚀 HTTP http://localhost:${this.port}`);
|
||
|
console.info(`🚀 WS ws://localhost:${this.port}`);
|
||
|
console.info(`=================================`);
|
||
|
});
|
||
|
// io.on("connection", (socket) => {
|
||
|
// socket.on("disconnect", function (msg) {
|
||
|
// console.log("Disconnected");
|
||
|
// });
|
||
|
// });
|
||
|
|
||
|
// setInterval(function () {
|
||
|
// io.emit("goodbye");
|
||
|
// console.log(200);
|
||
|
// }, 1000);
|
||
|
|
||
|
}
|
||
|
|
||
|
public getServer() {
|
||
|
return this.app;
|
||
|
}
|
||
|
|
||
|
private initializeMiddlewares() {
|
||
|
this.app.use(cors());
|
||
|
this.app.use(express.json());
|
||
|
this.app.use(express.urlencoded({ extended: true }));
|
||
|
}
|
||
|
|
||
|
private initializeRoutes(routes: Routes[]) {
|
||
|
routes.forEach((route) => {
|
||
|
this.app.use("/", route.router);
|
||
|
});
|
||
|
}
|
||
|
loadAppDependencies() {
|
||
|
locator(
|
||
|
this.env == "development"
|
||
|
? new DevEnv(this.computedFolder)
|
||
|
: new UnitTestEnv(this.computedFolder)
|
||
|
);
|
||
|
|
||
|
mongoose
|
||
|
.connect("mongodb://127.0.0.1:27017/test")
|
||
|
.then(() => console.log("Connected!"))
|
||
|
.catch((e) => {
|
||
|
console.log("ERROR:", e);
|
||
|
});
|
||
|
}
|
||
|
}
|