<commit_before>const bodyParser = require("body-parser");
const compression = require("compression");
const cors = require("cors");
const express = require("express");
const { HttpError, ObjectNotExistError } = require("./libs/errors");
const expressMongoDb = require("./middlewares/express_mongo_db");
const expressRedisDb = require("./middlewares/express_redis_db");
const logger = require("morgan");
const winston = require("winston");
const passport = require("passport");
const passportStrategies = require("./libs/passport-strategies");
const { jwtStrategy } = require("./utils/passport-strategies");

const ModelManager = require("./models/manager");
const routes = require("./routes");
const schema = require("./schema");
const setupGraphql = require("./utils/setup_graphql");

const { REDIS_URL, CORS_ANY } = process.env;

const app = express();

// We are behind the proxy
app.set("trust proxy", 1);

// Enable compress the traffic
if (app.get("env") === "production") {
    app.use(compression());
}

// winston logging setup
if (app.get("env") === "test" || app.get("env") === "developement") {
    winston.remove(winston.transports.Console);
}

if (app.get("env") !== "test") {
    app.use(logger("dev"));
}
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(expressMongoDb());
app.use(expressRedisDb(REDIS_URL));
app.use((req, res, next) => {
    req.manager = new ModelManager(req.db);
    next();
});

if (CORS_ANY === "TRUE") {
    app.use(cors());
} else {
    app.use(
        cors({
            origin: [/\.goodjob\.life$/],
        })
    );
}

app.use((req, res, next) => {
    winston.info(req.originalUrl, {
        ip: req.ip,
        ips: req.ips,
        query: req.query,
    });
    next();
});

app.use(passport.initialize());
passport.use(passportStrategies.legacyFacebookTokenStrategy());
passport.use(jwtStrategy());
app.use((req, res, next) => {
    passport.authenticate("jwt", { session: false }, (err, user) => {
        if (user) {
            req.user = user;
        }
        next();
    })(req, res, next);
});
app.use("/", routes);

setupGraphql(app, {
    schema,
    context: ({ req }) => ({
        db: req.db,
        manager: req.manager,
        user: req.user,
    }),
});

// catch 404 and forward to error handler
app.use((req, res, next) => {
    next(new HttpError("Not Found", 404));
});

// error handlers

// logging any error except HttpError
app.use((err, req, res, next) => {
    if (err instanceof ObjectNotExistError) {
        next(new HttpError("Not Found", 404));
        return;
    }

    if (err instanceof HttpError) {
        next(err);<commit_msg>Feature: Logging graphql error (#582)<commit_after>const bodyParser = require("body-parser");
const compression = require("compression");
const cors = require("cors");
const express = require("express");
const { HttpError, ObjectNotExistError } = require("./libs/errors");
const expressMongoDb = require("./middlewares/express_mongo_db");
const expressRedisDb = require("./middlewares/express_redis_db");
const logger = require("morgan");
const winston = require("winston");
const passport = require("passport");
const passportStrategies = require("./libs/passport-strategies");
const { jwtStrategy } = require("./utils/passport-strategies");

const ModelManager = require("./models/manager");
const routes = require("./routes");
const schema = require("./schema");
const setupGraphql = require("./utils/setup_graphql");

const { REDIS_URL, CORS_ANY } = process.env;

const app = express();

// We are behind the proxy
app.set("trust proxy", 1);

// Enable compress the traffic
if (app.get("env") === "production") {
    app.use(compression());
}

// winston logging setup
if (app.get("env") === "test" || app.get("env") === "developement") {
    winston.remove(winston.transports.Console);
}

if (app.get("env") !== "test") {
    app.use(logger("dev"));
}
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(expressMongoDb());
app.use(expressRedisDb(REDIS_URL));
app.use((req, res, next) => {
    req.manager = new ModelManager(req.db);
    next();
});

if (CORS_ANY === "TRUE") {
    app.use(cors());
} else {
    app.use(
        cors({
            origin: [/\.goodjob\.life$/],
        })
    );
}

app.use((req, res, next) => {
    winston.info(req.originalUrl, {
        ip: req.ip,
        ips: req.ips,
        query: req.query,
    });
    next();
});

app.use(passport.initialize());
passport.use(passportStrategies.legacyFacebookTokenStrategy());
passport.use(jwtStrategy());
app.use((req, res, next) => {
    passport.authenticate("jwt", { session: false }, (err, user) => {
        if (user) {
            req.user = user;
        }
        next();
    })(req, res, next);
});
app.use("/", routes);

setupGraphql(app, {
    schema,
    context: ({ req }) => ({
        db: req.db,
        manager: req.manager,
        user: req.user,
    }),
    formatError: err => {
        winston.warn("", {
            message: err.message,
            error: err,
        });
        return err;
    },
});

// catch 404 and forward to error handler
app.use((req, res, next) => {
    next(new HttpError("Not Found", 404));
});

// error handlers

// logging any error except HttpError
app.use((err, req, res, next) => {
    if (err instanceof ObjectNotExistError) {
        next(new HttpError("Not Found", 404));
        return;
    }

    if (err instanceof HttpError) {
        next(err);
{'fraction_non_alphanumeric': 0.1875683558148013, 'fraction_numerical': 0.004192489974480496, 'mean_word_length': 3.3651551312649164, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4060214', 'n_tokens_mistral': 1821, 'n_tokens_neox': 1771, 'n_words': 388}