<commit_before>const express = require('express')
const app = express()
const mongoose = require('mongoose')
const autoIncrement = require('mongoose-auto-increment')

const logger = require('./infrastructure/logger')

const DEFAULT_APPLICATION_PORT = 5000

// set up the database
const url = require('./config').url
mongoose.connect(url)
const db = mongoose.connection

// auto increment ID
autoIncrement.initialize(db)

db.on('error', (error) => logger.error(`connection error: ${error}`))
db.once('open', () => {
  logger.info('Connected to MongoDB')
})

const Url = require('./infrastructure/models/Url')

app.set('port', process.env.PORT || DEFAULT_APPLICATION_PORT)
app.set('view engine', 'ejs')

// may not need this after all.
app.use((req, res, next) => {
  req.shortTemplate = `${req.protocol}://${req.get('host')}/`
  next()
})

app.get('/list/', (req, res) => {
  Url.find({}, (err, urls) => {
    if (err) {
      logger.error(err.message, err.stack)
      return res.status(500).end()
    }
    res.render('pages/list', { urls: urls, shortTemplate: req.shortTemplate })
  })
})

app.get('/', (req, res) => {
  res.render('pages/index')
})

app.get('/:hash', (req, res) => {
  const hash = req.params.hash
  const idOfHash = parseInt(hash, 36)

  Url.findOne({ id: idOfHash }, (err, url) => {
    if (err || url === null) {
      logger.error(err)
      return res.status(500).end()
    }
    res.redirect(url.link)
  })
})

app.get('/new/:url(*)', (req, res) => {
  const url = req.params.url
  const query = { link: url }

  Url.findOne(query, (err, url) => {
    if (err) {
      logger.error(err + '[Url.findOne]')
      return res.status(500).end()
    }

    if (url) {
      return res.json({
        original_url: url.link,
        short_url: req.shortTemplate + url.hash()
      })
    }

    const newUrl = new Url(query)
    newUrl.save((err, newUrl) => {
      if (err) {
        logger.error(err.message, err.stack)
        return res.status(500).end()
      }
      res.json({
        original_url: newUrl.link,
        short_url: req.shortTemplate + newUrl.hash()
      })
    })
  })
})

app.listen(app.get('port'), () => {
  logger.info('short-url app listening on port: ' + app.get('port'))
})<commit_msg>callback to async/await<commit_after>const express = require('express')
const app = express()
const mongoose = require('mongoose')
const autoIncrement = require('mongoose-auto-increment')

const logger = require('./infrastructure/logger')

const DEFAULT_APPLICATION_PORT = 5000

// set up the database
const url = require('./config').url
mongoose.connect(url)
const db = mongoose.connection

// auto increment ID
autoIncrement.initialize(db)

db.on('error', (error) => logger.error(`connection error: ${error}`))
db.once('open', () => {
  logger.info('Connected to MongoDB')
})

const Url = require('./infrastructure/models/Url')

app.set('port', process.env.PORT || DEFAULT_APPLICATION_PORT)
app.set('view engine', 'ejs')

// may not need this after all.
app.use((req, res, next) => {
  req.shortTemplate = `${req.protocol}://${req.get('host')}/`
  next()
})

app.get('/list/', async (req, res) => {
  try {
    const urls = await Url.find({})
    res.render('pages/list', { urls: urls, shortTemplate: req.shortTemplate })
  } catch (err) {
    logger.error(err.message, err.stack)
    return res.status(500).end()
  }
})

app.get('/', (req, res) => {
  res.render('pages/index')
})

app.get('/:hash', async (req, res) => {
  const hash = req.params.hash
  const idOfHash = parseInt(hash, 36)

  try {
    const url = await Url.findOne({ id: idOfHash })
    if (!url) {
      return res.status(404).end()
    }
    res.redirect(url.link)
  } catch (err) {
    logger.error(err)
    return res.status(500).end()
  }
})

app.get('/new/:url(*)', async (req, res) => {
  const url = req.params.url

  const existingUrl = await Url.findOne({
    link: url
  })

  if (existingUrl) {
    return res.json({
      original_url: existingUrl.link,
      short_url: req.shortTemplate + existingUrl.hash()
    })
  }

  const newUrl = new Url({
    link: url
  })

  try {
    const createdUrl = await newUrl.save()
    res.json({
      original_url: createdUrl.link,
      short_url: req.shortTemplate + createdUrl.hash()
    })
  } catch (err) {
    logger.error(err.message, err.stack)
    return res.status(500).end()
  }
})

app.listen(app.get('port'), () => {
  logger.info('short-url app listening on port: ' + app.get('port'))
})
{'fraction_non_alphanumeric': 0.1862811028917283, 'fraction_numerical': 0.008069939475453935, 'mean_word_length': 3.241444866920152, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '426622', 'n_tokens_mistral': 1580, 'n_tokens_neox': 1554, 'n_words': 329}