Skip to content

Commit

Permalink
enhance config include
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudwu committed Mar 21, 2017
1 parent 67b0550 commit 3a4a673
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
10 changes: 3 additions & 7 deletions examples/config
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
root = "./"
include "config.path"

-- preload = "./examples/preload.lua" -- run preload.lua before every lua service run
thread = 8
logger = nil
logpath = "."
Expand All @@ -8,12 +10,6 @@ master = "127.0.0.1:2013"
start = "main" -- main script
bootstrap = "snlua bootstrap" -- The service for bootstrap
standalone = "0.0.0.0:2013"
luaservice = root.."service/?.lua;"..root.."test/?.lua;"..root.."examples/?.lua"
lualoader = root .. "lualib/loader.lua"
lua_path = root.."lualib/?.lua;"..root.."lualib/?/init.lua"
lua_cpath = root .. "luaclib/?.so"
-- preload = "./examples/preload.lua" -- run preload.lua before every lua service run
snax = root.."examples/?.lua;"..root.."test/?.lua"
-- snax_interface_g = "snax_g"
cpath = root.."cservice/?.so"
-- daemon = "./skynet.pid"
6 changes: 6 additions & 0 deletions examples/config.path
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
root = "./"
luaservice = root.."service/?.lua;"..root.."test/?.lua;"..root.."examples/?.lua"
lualoader = root .. "lualib/loader.lua"
lua_path = root.."lualib/?.lua;"..root.."lualib/?/init.lua"
lua_cpath = root .. "luaclib/?.so"
snax = root.."examples/?.lua;"..root.."test/?.lua"
30 changes: 22 additions & 8 deletions skynet-src/skynet_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,32 @@ int sigign() {
}

static const char * load_config = "\
local config_name = ...\n\
local f = assert(io.open(config_name))\n\
local code = assert(f:read \'*a\')\n\
local function getenv(name) return assert(os.getenv(name), \'os.getenv() failed: \' .. name) end\n\
code = string.gsub(code, \'%$([%w_%d]+)\', getenv)\n\
f:close()\n\
local result = {}\n\
local function getenv(name) return assert(os.getenv(name), [[os.getenv() failed: ]] .. name) end\n\
local sep = package.config:sub(1,1)\n\
local current_path = [[.]]..sep\n\
local function include(filename)\n\
assert(loadfile(filename, \'t\', result))()\n\
local last_path = current_path\n\
local path, name = filename:match([[(.*]]..sep..[[)(.*)$]])\n\
if path then\n\
if path:sub(1,1) == sep then -- root\n\
current_path = path\n\
else\n\
current_path = current_path .. path\n\
end\n\
else\n\
name = filename\n\
end\n\
local f = assert(io.open(current_path .. name))\n\
local code = assert(f:read [[*a]])\n\
code = string.gsub(code, [[%$([%w_%d]+)]], getenv)\n\
f:close()\n\
assert(load(code,[[@]]..filename,[[t]],result))()\n\
current_path = last_path\n\
end\n\
setmetatable(result, { __index = { include = include } })\n\
assert(load(code,\'=(load)\',\'t\',result))()\n\
local config_name = ...\n\
include(config_name)\n\
setmetatable(result, nil)\n\
return result\n\
";
Expand Down

0 comments on commit 3a4a673

Please sign in to comment.