1 | require! {
|
2 | underscore: _
|
3 | fs
|
4 | path
|
5 | hacktiv
|
6 | \prelude-ls : {each, obj-to-pairs}
|
7 | \./Helpers/Debug
|
8 | }
|
9 |
|
10 | debug-nodulator = new Debug 'N::Core'
|
11 |
|
12 | class N
|
13 |
|
14 | resources: {}
|
15 | config: null
|
16 | consoleMode: false
|
17 | defaultConfig:
|
18 | db: type: \SqlMem
|
19 | cache: false
|
20 | flipDone: false
|
21 |
|
22 | ->
|
23 | @Init()
|
24 |
|
25 | Init: ->
|
26 |
|
27 | debug-nodulator.Log \Init
|
28 |
|
29 | @appRoot = path.resolve \.
|
30 |
|
31 | debug-nodulator.Log 'Init ended'
|
32 |
|
33 | Resource: (name, routes, config, _parent) ->
|
34 |
|
35 | getParentChain = ->
|
36 | | it?._parent? => " <= #{that.name}" + getParentChain that
|
37 | | _ => ''
|
38 |
|
39 | debug-nodulator.Log "Start creating resource: #{name + getParentChain @resources[name]}"
|
40 |
|
41 | resource = null
|
42 | name = name.toLowerCase()
|
43 | if @resources[name]?
|
44 | debug-nodulator.Log "Existing resource : #{name}"
|
45 | return @resources[name]
|
46 |
|
47 | if routes? and not routes.prototype
|
48 | config = routes
|
49 | routes = null
|
50 |
|
51 | @Config()
|
52 |
|
53 | if config?
|
54 | config.db = {} if not config.db?
|
55 |
|
56 | obj-to-pairs @config.db |> each ->
|
57 | | not config.db[it.0]? => config.db[it.0] = it.1
|
58 |
|
59 | config = {db: @config.db} if not config?
|
60 |
|
61 | if _parent?
|
62 | class ExtendedResource extends _parent
|
63 | Ntm: -> 0
|
64 |
|
65 | @resources[name] = resource := ExtendedResource
|
66 | @resources[name]._PrepareResource config, routes, name, _parent
|
67 | else
|
68 | @resources[name] = resource :=
|
69 | require(\./Resource/Resource) config, routes, name
|
70 |
|
71 | debug-nodulator.Log "Resource added : #{name + getParentChain @resources[name]}"
|
72 |
|
73 | resource
|
74 |
|
75 | Route: require \./Route/Route
|
76 |
|
77 | Config: (config) ->
|
78 | if @config?
|
79 | return
|
80 |
|
81 | debug-nodulator.Warn "Main config"
|
82 |
|
83 | @config = config || @defaultConfig
|
84 |
|
85 | @defaultConfig
|
86 | |> obj-to-pairs
|
87 | |> each ~> @config[it.0] = it.1 if not @config[it.0]?
|
88 |
|
89 | Use: (module) ->
|
90 | debug-nodulator.Log "Loading module"
|
91 | m = module @
|
92 | debug-nodulator.Log "Loaded module: #{m.name}"
|
93 | m
|
94 |
|
95 | Console: (@consoleMode = true) ->
|
96 |
|
97 | ExtendDefaultConfig: (config) ->
|
98 | @defaultConfig = _(@defaultConfig).extend config
|
99 |
|
100 | Bus: require \./Helpers/Bus
|
101 | bus: new @::Bus()
|
102 |
|
103 | Watch: Hacktiv
|
104 | DontWatch: Hacktiv.DontWatch
|
105 |
|
106 | Reset: (done) ->
|
107 | debug-nodulator.Warn "Reset"
|
108 |
|
109 | cache = require \./Resource/Cache
|
110 | if cache.client?
|
111 | cache.Reset!
|
112 |
|
113 | require \./Resource/Wrappers .Reset!
|
114 | require \./Resource/Connectors .Reset!
|
115 |
|
116 | @inited = {}
|
117 | @resources = {}
|
118 | @config = null
|
119 |
|
120 | if @server?
|
121 | @app = null
|
122 | @server.close()
|
123 | @server = null
|
124 |
|
125 | @Init()
|
126 |
|
127 | done() if done?
|
128 |
|
129 | _ListEndpoints: (done) ->
|
130 | endpoints = []
|
131 | for endpoint in @app._router.stack
|
132 | if endpoint.route?
|
133 | res = {}
|
134 | res[endpoint.route.path] = [key for key of endpoint.route.methods]
|
135 | endpoints.push res
|
136 | done(endpoints) if done?
|
137 |
|
138 | Nodulator = new N
|
139 |
|
140 | f = (...args) ->
|
141 |
|
142 | f.Config! if not f.config?
|
143 | f.Resource.apply f, args
|
144 |
|
145 | f = f <<<< Nodulator
|
146 |
|
147 | module.exports = f
|