const path = require('path');
const { createProxyMiddleware } = require('http-proxy-middleware');
const cors = require('cors');

const express = require('express');
const app = express();

const server = require('http').Server(app);
const router = require('express').Router()

const options = {
    target: 'https://book.ogoun.name/', // target host
    changeOrigin: true, // needed for virtual hosted sites
    ws: true, // proxy websockets
    logLevel: 'debug',
    secure: false,
    cookieDomainRewrite: {
        '*': 'localhost',
    },
    onProxyRes(proxyRes, req, _res) {
        if (proxyRes.headers['set-cookie'] !== undefined) {
            req.cookie = proxyRes.headers['set-cookie'];
        }
    },
    onProxyReq(proxyReq, req, _res) {
        if (req && req.cookie !== undefined) {
            proxyReq.setHeader('Cookie', req.cookie[0]);
        }
    },
};

const myLogger = function (req, res, next) {
    next()
}

app.use(myLogger)

app.use(express.static(__dirname));
app.use(express.static(__dirname + '/js'));
app.use(express.static(__dirname + '/css'));
app.use(express.static(__dirname + '/constants'));

// app.get('/login', function (req, res) {
//     res.sendFile(path.join(__dirname, '/login.html'));
// });

app.use('/', cors({
    credentials: true,
    origin: 'http://localhost:3000',
}), createProxyMiddleware(options));

server.listen(3001);