<commit_before>_         = require 'underscore'
model     = require './model'
utils     = require './utils'

# 路由器初始化
# params
#   server object restify.createServer()
#   controller ./controller
#   defaults 默认控制器方法
module.exports = (server, ctls, defaults, opts = {}) ->

  apis = []

  # 判断是否需要提供apis的查询接口
  if opts.apis
    server.get opts.apis, (req, res, next) ->
      res.send apis
      next()

  register = (verb, routePath, ctlAct) ->

    # 暂存起来，提供给apis接口来用
    # apis接口用来返回当前 services 提供的可用的 api
    apis.push "[#{verb.toUpperCase()}] #{routePath}"

    [ctl, action] = ctlAct.split('#')

    # 如果定义的对应的控制器，也有对应的方法则使用该方法
    actions = ctls[ctl][action] if ctls[ctl] and ctls[ctl][action]

    # 反之则使用默认的方法来处理
    actions = defaults[action](model ctl) unless actions

    # 如果都没有则抛出异常
    throw Error "控制器缺少route指定的方法" unless actions

    # 如果actions是数组，则把数组弄成一维的
    actions = _.flatten actions if _.isArray actions

    # 强制把actions处理成一个数组
    actions = [actions] unless _.isArray actions

    # 过滤掉空
    actions = _.filter(actions, (x) -> x)

    # 将每一个action都用try catch处理
    actions = _.map(actions, (action) ->
      (req, res, next) ->
        try
          action(req, res, next)
        catch e
          console.error e
          console.error e.stack
          next(e)
    )
    server[verb].apply server, [routePath].concat actions

  router = {}
  _.each(['get', 'post', 'put', 'patch', 'del'], (verb) ->
    router[verb] = (routePath, ctlAct) ->
      register verb, routePath, ctlAct
  )

  ###
  controller 为可选参数，如果不填写则控制器名称直接就是 res ，方法为 list,add
  如果设置了controller 则控制器为 controller，方法为 #{res}s, add{Res}
  ###
  router.collection = (res, routePath, controller) ->
    unless routePath
      if controller
        routePath = "/#{controller}s/:#{controller}Id/#{res}s"
      else
        routePath = "/#{res}s"
    if controller
      register 'get', routePath, "#{controller}##{res}s"
      register 'post', routePath, "#{controller}#add#{utils.ucwords(res)}"
    else<commit_msg>为了配合 socket 做消息推送，对 router 做一些小改动，route.name 是 ctl_action 生成<commit_after>_         = require 'underscore'
model     = require './model'
utils     = require './utils'

# 路由器初始化
# params
#   server object restify.createServer()
#   controller ./controller
#   defaults 默认控制器方法
module.exports = (server, ctls, defaults, opts = {}) ->

  apis = []

  # 判断是否需要提供apis的查询接口
  if opts.apis
    server.get opts.apis, (req, res, next) ->
      res.send apis
      next()

  register = (verb, routePath, ctlAct) ->

    # 暂存起来，提供给apis接口来用
    # apis接口用来返回当前 services 提供的可用的 api
    apis.push "[#{verb.toUpperCase()}] #{routePath}"

    [ctl, action] = ctlAct.split('#')
    evtName = "#{ctl}_#{action}"

    # 如果定义的对应的控制器，也有对应的方法则使用该方法
    actions = ctls[ctl][action] if ctls[ctl] and ctls[ctl][action]

    # 反之则使用默认的方法来处理
    actions = defaults[action](model ctl) unless actions

    # 如果都没有则抛出异常
    throw Error "控制器缺少route指定的方法" unless actions

    # 如果actions是数组，则把数组弄成一维的
    actions = _.flatten actions if _.isArray actions

    # 强制把actions处理成一个数组
    actions = [actions] unless _.isArray actions

    # 过滤掉空
    actions = _.filter(actions, (x) -> x)

    # 将每一个action都用try catch处理
    actions = _.map(actions, (action) ->
      (req, res, next) ->
        req.route.evtName = evtName
        try
          action(req, res, next)
        catch e
          console.error e
          console.error e.stack
          next(e)
    )
    server[verb].apply server, [routePath].concat actions

  router = {}
  _.each(['get', 'post', 'put', 'patch', 'del'], (verb) ->
    router[verb] = (routePath, ctlAct) ->
      register verb, routePath, ctlAct
  )

  ###
  controller 为可选参数，如果不填写则控制器名称直接就是 res ，方法为 list,add
  如果设置了controller 则控制器为 controller，方法为 #{res}s, add{Res}
  ###
  router.collection = (res, routePath, controller) ->
    unless routePath
      if controller
        routePath = "/#{controller}s/:#{controller}Id/#{res}s"
      else
        routePath = "/#{res}s"
    if controller
      register 'get', routePath, "#{controller}##{res}s"
      register 'post', routePath, "#{controller}#add#{utils.ucwords(res)}"
    else
{'fraction_non_alphanumeric': 0.1372216844143272, 'fraction_numerical': 0.0, 'mean_word_length': 2.802207911683533, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, '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': '7415349', 'n_tokens_mistral': 1615, 'n_tokens_neox': 1659, 'n_words': 326}