#!/usr/bin/env node
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.server = void 0;
const cors = require("cors");
const express = require("express");
const createError = require("http-errors");
const os = require("os");
const path = require("path");
const path_1 = require("../path");
const port_1 = require("./port");
const compression = require("compression");
process.on('uncaughtException', function (err) {
    console.error(new Date().toUTCString() + ' uncaughtException:', err.message);
    console.error(err.stack);
    process.exit(1);
});
const LOCAL_IP_ADDRESS = Object.values(os.networkInterfaces()).reduce((r, list) => r.concat(list.reduce((rr, i) => rr.concat((i.family === 'IPv4' && !i.internal && i.address) || []), [])), [])[0];
exports.server = null;
const app = express();
app.use((req, res, next) => {
    const { url, method } = req;
    console.log(method, url);
    next();
});
const corsOptions = {
    origin: '*',
    credentials: true,
    optionsSuccessStatus: 200, // some legacy browsers (IE11, various SmartTVs) choke on 204
};
app.use(cors(corsOptions));
app.use(compression());
app.use(express.static(path_1.PATH_PUBLIC));
const PUBLIC_PATH = path.join(__dirname, '../..', 'public');
app.use(express.static(PUBLIC_PATH));
app.use((req, res, next) => {
    console.log(req.path);
    res.sendStatus(404);
});
exports.server = app.listen(port_1.PORT, onListening);
app.set('port', port_1.PORT);
exports.server.on('error', onError);
function onError(error) {
    if (error.syscall !== 'listen') {
        throw error;
    }
    const bind = typeof port_1.PORT === 'string' ? 'Pipe ' + port_1.PORT : 'Port ' + port_1.PORT;
    switch (error.code) {
        case 'EACCES':
            console.error(bind + ' requires elevated privileges');
            process.exit(1);
            break;
        case 'EADDRINUSE':
            console.error(bind + ' is already in use');
            process.exit(1);
            break;
        default:
            throw error;
    }
}
function onListening() {
    console.log(`http://localhost:${port_1.PORT}`);
    console.log(`http://${LOCAL_IP_ADDRESS}:${port_1.PORT}`);
}
app.use(function (req, res, next) {
    next(createError(404));
});
//# sourceMappingURL=index.js.map