<commit_before>_ = require 'lodash'

express = require 'express'

{albums, albumsUserVisible} = require './db'

respondJSON = (res, code, data) ->
  res.contentType 'application/json'
  res.send code, JSON.stringify data

respondFromDb = (req, res, collection, query, projection) ->
  collection.findOne query, projection, (err, result) ->
    if result?
      respondJSON res, 200, _.omit(result, '_id')
    else
      respond404 req, res

respond404 = (req, res) ->
  if req.accepts 'json'
    respondJSON res, 404,
      error: 404
      message: 'Not found'

  else
    res.send 404, 'Not found'

albumQuery = (path) ->
  $or: [
    { path },
    { 'pictures.path': path }
  ]

exports.app = app = express()
app.use app.router
app.use respond404

app.get /^\/v2(\/[\/a-zA-Z0-9-\/]*)$/, (req, res) ->
  path = req.params[0]
  console.log 'album', path
  respondFromDb req, res, albums, albumQuery path, albumsUserVisible

if require.main is module
  app.listen 3000<commit_msg>serve static resources<commit_after>path = require 'path'

_ = require 'lodash'

express = require 'express'

{albums, albumsUserVisible} = require './db'

staticPath = path.resolve path.dirname(module.filename), '..', 'public'

respondJSON = (res, code, data) ->
  res.contentType 'application/json'
  res.send code, JSON.stringify data

respondFromDb = (req, res, collection, query, projection) ->
  collection.findOne query, projection, (err, result) ->
    if result?
      respondJSON res, 200, _.omit(result, '_id')
    else
      respond404 req, res

respond404 = (req, res) ->
  if req.accepts 'json'
    respondJSON res, 404,
      error: 404
      message: 'Not found'

  else
    res.send 404, 'Not found'

albumQuery = (path) ->
  $or: [
    { path },
    { 'pictures.path': path }
  ]

exports.app = app = express()
app.use app.router
app.use express.static staticPath
app.use respond404

app.get /^\/v2(\/[\/a-zA-Z0-9-\/]*)$/, (req, res) ->
  path = req.params[0]
  console.log 'album', path
  respondFromDb req, res, albums, albumQuery path, albumsUserVisible

if require.main is module
  app.listen 3000
{'fraction_non_alphanumeric': 0.14122486288848263, 'fraction_numerical': 0.026508226691042046, 'mean_word_length': 2.7227891156462585, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '2841180', 'n_tokens_mistral': 827, 'n_tokens_neox': 716, 'n_words': 202}