<commit_before>/* eslint-disable no-console */
import path from 'path'
import express from 'express'
import compress from 'compression'
import morgan from 'morgan'
import ejs from 'ejs'
import subdomain from 'express-subdomain'
import errorHandler from 'express-err'
import config from 'config'
import crashReporter from 'modules/crashReporter/crashReporter'
import csp from 'server/middlewares/csp'
import www from 'server/routes/www'
import api from 'server/routes/api'

const app = express()
app.disable('x-powered-by')
app.engine('html', ejs.renderFile)
app.set('trust proxy', 1)
app.set('views', path.join(__dirname, '..'))

app.use(crashReporter.requestHandler())

if (config.get('server.logFormat')) {
  app.use(morgan(config.get('server.logFormat')))
}

app.use(compress())
app.use(csp) // Content Security Policy

// Redirect from http to https
if (config.get('server.secure')) {
  app.use((req, res, next) => {
    if (req.headers['x-forwarded-proto'] !== 'https') {
      res.redirect(`${config.get('server.url')}${req.url}`)
    } else {
      next() /* Continue to other routes if we're not redirecting */
    }
  })
}

app.use(subdomain('www', www))
app.use(subdomain('api', api))

app.use(crashReporter.errorHandler())

// Log errors
if (config.get('env') !== 'test') {
  app.use((err, req, res, next) => {
    console.log(err, err.stack)
    next(err)
  })
}

// Display errors
app.use(errorHandler({
  exitOnUncaughtException: false,
  formatters: ['json'],
}))

export default app
<commit_msg>fix(redirect): fix redirect for api<commit_after>/* eslint-disable no-console */
import path from 'path'
import express from 'express'
import compress from 'compression'
import morgan from 'morgan'
import ejs from 'ejs'
import subdomain from 'express-subdomain'
import errorHandler from 'express-err'
import config from 'config'
import crashReporter from 'modules/crashReporter/crashReporter'
import csp from 'server/middlewares/csp'
import www from 'server/routes/www'
import api from 'server/routes/api'

const app = express()
app.disable('x-powered-by')
app.engine('html', ejs.renderFile)
app.set('trust proxy', 1)
app.set('views', path.join(__dirname, '..'))

app.use(crashReporter.requestHandler())

if (config.get('server.logFormat')) {
  app.use(morgan(config.get('server.logFormat')))
}

app.use(compress())
app.use(csp) // Content Security Policy

// Redirect from http to https
if (config.get('server.secure')) {
  app.use((req, res, next) => {
    if (req.headers['x-forwarded-proto'] !== 'https') {
      res.redirect(`https://${req.hostname}${req.url}`)
    } else {
      next() /* Continue to other routes if we're not redirecting */
    }
  })
}

app.use(subdomain('www', www))
app.use(subdomain('api', api))

app.use(crashReporter.errorHandler())

// Log errors
if (config.get('env') !== 'test') {
  app.use((err, req, res, next) => {
    console.log(err, err.stack)
    next(err)
  })
}

// Display errors
app.use(errorHandler({
  exitOnUncaughtException: false,
  formatters: ['json'],
}))

export default app

{'fraction_non_alphanumeric': 0.16940789473684212, 'fraction_numerical': 0.0006578947368421052, 'mean_word_length': 4.61070110701107, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 1, 'lorem ipsum': 0, 'www.': 8, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1041117', 'n_tokens_mistral': 1033, 'n_tokens_neox': 1029, 'n_words': 248}