-
Notifications
You must be signed in to change notification settings - Fork 8
/
cryptofu.lua
48 lines (42 loc) · 1.4 KB
/
cryptofu.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
local log = require 'tools.logger' ()
local dispatcher = require 'dispatcher'
local config = "config.lua"
local dump = require 'pl.pretty'.dump
local loadmodule = function (name)
return pcall (function ()
local sandbox = {}
local mod_table = setfenv (assert (loadfile (name)), sandbox) ()
setmetatable (sandbox, { __index = function (t, v)
t[v] = v == "_G" and sandbox or _G[v]
return rawget (t, v)
end })
assert (mod_table.main)
dispatcher.addtask (mod_table.main, mod_table.interval)
if mod_table.startup then
table.insert (dispatcher.startup_tasks, mod_table.startup)
end
if mod_table.shutdown then
table.insert (dispatcher.shutdown_tasks, mod_table.shutdown)
end
end)
end
local loadconfig = function (cfg_file)
local cfg = {}
local noerr, errmsg = pcall (function ()
log ("Reading ", cfg_file)
setfenv (assert (loadfile (cfg_file)), cfg) ()
package.preload.apikeys = function () return cfg.apikeys end
end)
log._if (not noerr, errmsg)
if not noerr then return end
for _, mod in ipairs (cfg.modules) do
log ("Loading ", mod)
local noerr, errmsg = loadmodule (mod)
log._if (not noerr, errmsg)
end
end
print "CryptoFu Trader"
loadconfig (config)
dispatcher.startup()
dispatcher.mainloop()
dispatcher.shutdown()