<commit_before>url = require 'url'
express = require 'express'

map = require './mapping'
Controller = require './controller'

cohers = (v) ->
  return Number(v) if not Number.isNaN(Number v)
  return true if v is "true"
  return false if v is "false"
  return new Date(v) if not Number.isNaN(Date.parse v)
  return v

resRouter = (getResHandler, req, res, next) -> getResHandler(req) res, next

exports.http = (ctrl, app) ->
  app ?= express()
  # Setup web server
  app.use express.bodyParser()

  app.put '/geo', resRouter.bind undefined, (req) ->
    ctrl.oneOrMoreGeos req.body
  app.put '/geojson', resRouter.bind undefined, (req) ->
    ctrl.oneOrMoreGeoJson req.body

  # Put using GET
  app.get '/put_geo', resRouter.bind undefined, (req) ->
    ctrl.oneOrMoreGeos JSON.parse url.parse(req.url, true).json
  app.get '/put_geojson', resRouter.bind undefined, (req) ->
    ctrl.oneOrMoreGeoJson JSON.parse url.parse(req.url, true).json
  app.get '/put_params', resRouter.bind undefined, (req) ->
    parts = url.parse req.url, true
    meta = {}
    meta[k] = cohers(v) for k, v of parts.query when not (k in ['lat','lng'])
    {lat, lng} = parts.query
    ctrl.newGeo.bind ctrl,
      lnglat: [parseFloat(lng), parseFloat(lat)]
      meta: meta

  # Get geo by id
  app.get '/geo/:id', ctrl.idGeo.bind ctrl, map.docToGeo
  app.get '/geojson/:id', ctrl.idGeo.bind ctrl, map.docToGeoJson

  # Get multiple geos by query
  app.get '/geo', ctrl.queryGeos.bind ctrl, (docs) ->
    map.docToGeo doc for doc in docs
  app.get '/geojson', ctrl.queryGeos.bind ctrl, map.docsToGeoJson
  return app

exports.socketio = (srvc, server) ->
  # Socket.io setup
  io = require('socket.io').listen server

  io.sockets.on 'connection', (socket) ->
    socket.on 'new geo', (geo) ->
      srvc.createGeo map.geoToDoc(geo), (err, doc) ->
        socket.broadcast.emit 'new geo', map.docToGeo(doc)
  return io
<commit_msg>Re-added geo_put API call for backward compatibility<commit_after>url = require 'url'
express = require 'express'

map = require './mapping'
Controller = require './controller'

cohers = (v) ->
  return Number(v) if not Number.isNaN(Number v)
  return true if v is "true"
  return false if v is "false"
  return new Date(v) if not Number.isNaN(Date.parse v)
  return v

resRouter = (getResHandler, req, res, next) -> getResHandler(req) res, next

exports.http = (ctrl, app) ->
  app ?= express()
  # Setup web server
  app.use express.bodyParser()

  app.put '/geo', resRouter.bind undefined, (req) ->
    ctrl.oneOrMoreGeos req.body
  app.put '/geojson', resRouter.bind undefined, (req) ->
    ctrl.oneOrMoreGeoJson req.body

  # Put using GET
  app.get '/put_geo', resRouter.bind undefined, (req) ->
    ctrl.oneOrMoreGeos JSON.parse url.parse(req.url, true).json
  app.get '/put_geojson', resRouter.bind undefined, (req) ->
    ctrl.oneOrMoreGeoJson JSON.parse url.parse(req.url, true).json
  putParams = resRouter.bind undefined, (req) ->
    parts = url.parse req.url, true
    meta = {}
    meta[k] = cohers(v) for k, v of parts.query when not (k in ['lat','lng'])
    {lat, lng} = parts.query
    ctrl.newGeo.bind ctrl,
      lnglat: [parseFloat(lng), parseFloat(lat)]
      meta: meta
  app.get '/put_params', putParams
  app.get '/geo_put', putParams # *depricated* legacy call

  # Get geo by id
  app.get '/geo/:id', ctrl.idGeo.bind ctrl, map.docToGeo
  app.get '/geojson/:id', ctrl.idGeo.bind ctrl, map.docToGeoJson

  # Get multiple geos by query
  app.get '/geo', ctrl.queryGeos.bind ctrl, (docs) ->
    map.docToGeo doc for doc in docs
  app.get '/geojson', ctrl.queryGeos.bind ctrl, map.docsToGeoJson
  return app

exports.socketio = (srvc, server) ->
  # Socket.io setup
  io = require('socket.io').listen server

  io.sockets.on 'connection', (socket) ->
    socket.on 'new geo', (geo) ->
      srvc.createGeo map.geoToDoc(geo), (err, doc) ->
        socket.broadcast.emit 'new geo', map.docToGeo(doc)
  return io

{'fraction_non_alphanumeric': 0.1399014778325123, 'fraction_numerical': 0.0, 'mean_word_length': 2.950389105058366, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, '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': '4782465', 'n_tokens_mistral': 1513, 'n_tokens_neox': 1402, 'n_words': 390}