This repository has been archived on 2022-05-17. You can view files and clone it, but cannot push or open issues or pull requests.
brainlet/index.js

40 lines
1.2 KiB
JavaScript
Executable file

const config = require("./config");
const apiRoute = require("./api/v1");
const { GatewayServer, GatewayHandler } = require("./api/v2/gateway/index");
const express = require("express");
const cookieParser = require("cookie-parser");
const cors = require("cors");
const http = require("http");
const app = express();
const httpServer = http.createServer(app);
const gatewayServer = new GatewayServer(httpServer);
gatewayServer.bindHandler(GatewayHandler);
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use(cookieParser());
app.use(cors({
origin: function (origin, callback) {
if (config.corsAllowList.indexOf(origin) !== -1 || !origin) {
callback(null, true);
} else {
callback(new Error("Not allowed by CORS"));
}
},
credentials: true,
optionsSuccessStatus: 200
}));
app.use("/api/v1", apiRoute);
app.use(express.static("app"));
app.use((err, req, res, next) => {
console.error("error: server: internal server error", err);
res.status(500).json({ error: true, status: 500, message: "ERROR_INTERNAL_SERVER_ERROR" });
});
httpServer.listen(config.ports.mainServerPort, () => {
console.log(`server: listening on port ${config.ports.mainServerPort}`);
});