{"size": 2521, "ext": "js", "max_stars_repo_path": "app.js", "max_stars_repo_name": "Tiss971/quizzyBackend", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "app.js", "max_issues_repo_name": "Tiss971/quizzyBackend", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "app.js", "max_forks_repo_name": "Tiss971/quizzyBackend", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 30.3734939759, "max_line_length": 106, "alphanum_fraction": 0.6902023007}
var port = process.env.PORT || 5000;

var express = require("express");
var http = require("http");
var path = require("path");
var bodyParser = require("body-parser");
var cookieParser = require("cookie-parser");
var logger = require("morgan");
const { Server } = require("socket.io");
const cors = require("cors");

// Router imports
var usersRouter = require("./routes/users");
var questionsRouter = require("./routes/questions");
var shopRouter = require("./routes/shop");
var friendsRouter = require("./routes/friends");

// express instance
var app = express();

// online controller classes
var PublicController = require('./controllers/common/publicController');

// Middleware imports
const { authWsToken } = require("./middleware/auth");

// http server instance to attach Socket.io handlers
const server = http.createServer(app);

// Use modules and folders
app.use(logger("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, "public")));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
var corsOptions = {
    origin: "*", // Reemplazar con dominio
    optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204
};
app.use(cors(corsOptions));

// Use routers
app.use("/user", usersRouter);
app.use("/questions", questionsRouter);
app.use("/shop", shopRouter);
app.use("/friends", friendsRouter);

const io = new Server(server, {
    cors: {
        origin: "*",
        methods: ["GET", "POST", "PUT", "DELETE"]
    }
});

let publicControllerInstance = new PublicController(io);

// Websocket handling imports
const registerChatHandlers = require("./controllers/ws/chatHandler");
const registerPublicHandlers = require("./controllers/ws/publicHandler");

const onConnection = (socket) => {
    // join common room
    socket.join('main');
    console.log('User with ID ' + socket.id + ' connected');
    socket.to('main').emit("otherConnect", {name : socket.user.name, systemMsg : 'connection'});

    socket.on('disconnect', () => {
        console.log('User with ID ' + socket.id + ' disconnected');
        socket.to('main').emit("otherDisconnect", {name : socket.user.name, systemMsg : 'disconnection'});
    })

    // Register handlers here
    registerChatHandlers(io, socket);
    registerPublicHandlers(socket, publicControllerInstance);
};

io.on('connection', onConnection);
io.use(authWsToken);

module.exports = { io, app, server };
