import express from "express";
import mongoose from "mongoose";
import config from "config";
import cors from "cors";
import errorMiddleware from "./middlewares/errorMiddleware.js";
import cookieParser from "cookie-parser";

//routers
import authRouter from "./routers/auth.router.js"  

const PORT = config.get('serverPort');

const app = express();

app.use(express.json());
app.use(cookieParser())
app.use(cors({origin:['http://www.localhost:3000', 'http://localhost:3000', 'https://chess.beknazaryanstudio.ru', 'https://www.chess.beknazaryanstudio.ru'], credentials: true}));
app.use('/api/auth', authRouter);
app.use(errorMiddleware);

const start = async () => {
    try {
        await mongoose.connect(config.get('dbUrl', {
            useNewUrlParser: true,
            useUnifieldTopology: true
        }));

        app.listen(PORT, () => {
            console.log(`Сервер успешно запущен на порту ${PORT}`);
        })
    } catch (e) {
        console.log(e);
    }
}

start();