import express from 'express';
import cors from 'cors';
import compression from 'compression';
import dotenv from 'dotenv';
// Configura dotenv

dotenv.config();
import v1 from './routes/v1.router.js';
const app = express();
app.use(compression());
app.use(express.json({ limit: '50mb' }));
app.use(express.urlencoded({ limit: '50mb', extended: true }));

//Middlewares
app.use((req, res, next) => {
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type,x-access-token');
    res.setHeader("Access-Control-Request-Method", "POST, GET, PUT, OPTIONS");
    res.setHeader('Access-Control-Allow-Credentials', true);
    next();
});

app.use(
    cors({
        origin: ['*'],
        methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH'],
        allowedHeaders: ['Content-Type', 'Authorization', 'x-access-token'],
    })
)


app.use('/v1', v1);

app.listen(process.env.PORT, () => {
    console.log('Servidor en funcionamiento en el puerto: ' + process.env.PORT);
});
