bluefire
Version:
Lightweight tcp framework
80 lines (62 loc) • 2.87 kB
text/coffeescript
Async = require("async")
###
Class that calls controllers and their actions that were previously loaded by
given packet and condition. This class stores names of server packets if client,
else client packets (those which are received).
This may also load the paths and after that, needed controllers.
Gelidus
0.0.3a
###
module.exports = class Router
constructor: () ->
= { }
= { }
install: (, callback, controllersFolder = "#{global.CurrentWorkingDirectory}/controllers/") ->
if Object.keys(.data).length > 0 # cont initialize paths if no paths are in data
for code, path of .data
code, path.action, path.controller, path.policies
for code, path of
if [path.controller]?
continue # this controller already exists
# add controller - require and load
controller = require(controllersFolder + path.controller)
[path.controller] = Injector.create controller
callback(null, 3) if callback # only needed in async. This is synchronous
###
Creates new controller for the router
name [String] name of the controller to by stored by
module [Object|Constructor] already constructed object or constructor that will be injected
Creating new controller (all properties are injected inside constructor)
$router.controller "myController", class MyController
constructor: (MyModel) ->
###
controller: (name, module) ->
if typeof(module) is 'object' # accept object as a controller
[name] = module
else
[name] = Injector.create(module)
###
Adds the route by the opcode, controllers name and its action name
packetName [Object] opcode to handle the route by
controller [String] name of controller to bind action to
action [String] name of action for the controller
###
route: (packetName, action, controller = null, policies = []) ->
policyManager = Injector.getService("$policies")
[packetName] = { action: action, controller: controller, policies: policyManager.get(policies) }
call: (packetName, session, data) =>
if [packetName]?
path = [packetName]
controller = [path.controller] # get controller by given path
# check all policies here
Async.each path.policies, (policy, next) ->
policy(session, data, next)
, (err) ->
# all policies were successfully "nexted"
return if err? # something happened during policy checking
if not controller? # support for non-controller functions
path.action(session, data)
else
controller[path.action](session, data)
getController: (name) ->
return [name]