-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmangos-api.lua
75 lines (60 loc) · 1.58 KB
/
cmangos-api.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
local cron = require("cron")
local http = require("http")
local mariadb = require("mariadb")
local realmstatus = require("scheduler.realmstatus")
local routes = require("routes")
local logger = require("logger")
local _M = {}
config = {}
jobs = {}
sql = {
mangosd = {}
}
function _M.configure(self, f)
fd = io.open(f, "r")
if fd == nil then
print(string.format("No such file or directory %s", f))
os.exit(2)
end
fd:close()
return dofile(f)
end
function _M.open_database(self, cfg)
local db, err = mariadb.open(
cfg.username, cfg.password,
cfg.address .. ":" .. cfg.port,
cfg.database)
if err ~= nil then
logger.error(err)
os.exit(3)
end
return db
end
function _M.main(self)
if #arg ~= 3 then self:print_usage() end
config = self:configure(arg[3])
logger.set_level(config.loglvl)
sql.api = self:open_database(config.db.api)
sql.realmd = self:open_database(config.db.realmd)
for k, v in pairs(config.db.mangosd) do
sql.mangosd[k] = {
chars = self:open_database(config.db.mangosd[k].chars),
world = self:open_database(config.db.mangosd[k].world)
}
end
jobs.realmstatus = cron.run(config.mangosd.check_interval, realmstatus.check)
local listen = config.address .. ":" .. tostring(config.port)
http.serve(listen, routes, nil)
sql.api.close()
sql.realmd.close()
for k, v in pairs(sql.mangosd) do
sql.mangosd[k].chars.close()
sql.mangosd[k].world.close()
end
jobs.realmstatus.stop()
end
function _M.print_usage(self)
print(string.format("USAGE: %s %s <config>", arg[1], arg[2]))
os.exit(1)
end
_M:main()