Commit 4b4bf01a authored by Marek Vavruša's avatar Marek Vavruša

daemon/lua: syntactic sugar for ‘net’

this allows list-like declaration of bound interfaces

e.g. net = { ‘127.0.0.1’, net.eth0 }
parent 8c8852c5
......@@ -112,16 +112,16 @@ The possible simple data types are strings, integers or floats and boolean.
Dynamic configuration
^^^^^^^^^^^^^^^^^^^^^
Knowing that the the configuration is a valid Lua script enables you to write dynamic rules, and also avoid
additional configuration templating. One example is to differentiate between internal and external
interfaces based on environment variable.
Knowing that the the configuration is a Lua in disguise enables you to write dynamic rules, and also avoid
repetition and templating. This is unavoidable with static configuration, e.g. when you want to configure
each node a little bit differently.
.. code-block:: lua
if hostname() == 'hidden' then
net.listen(net.eth0)
net.listen(net.eth0, 5353)
else
net.listen(net.eth1.addr[1])
net = { '127.0.0.1', net.eth1.addr[1] }
end
Another example would show how it is possible to bind to all interfaces, using iteration.
......@@ -221,6 +221,14 @@ Environment
Network configuration
^^^^^^^^^^^^^^^^^^^^^
For when listening on ``localhost`` just doesn't cut it.
.. tip:: Use declarative interface for network.
.. code-block:: lua
net = { '127.0.0.1', net.eth0, net.eth1.addr[1] }
.. function:: net.listen(address, [port = 53])
:return: boolean
......
......@@ -16,12 +16,19 @@ setmetatable(env, {
-- Quick access to interfaces
-- `net.<iface>` => `net.interfaces()[iface]`
-- `net = {addr1, ..}` => `net.listen(name, addr1)`
setmetatable(net, {
__index = function (t, k)
local v = rawget(t, k)
if v then return v
else return net.interfaces()[k]
end
end,
__newindex = function (t,k,v)
local iname = rawget(net.interfaces(), v)
if iname then t.listen(iname)
else t.listen(v)
end
end
})
......@@ -29,10 +36,11 @@ setmetatable(net, {
-- `modules.<name> = <config>`
setmetatable(modules, {
__newindex = function (t,k,v)
if type(k) == 'number' then k = v end
if not rawget(_G, k) then
modules.load(k)
local mod = rawget(_G, k)
if mod and mod['config'] then
if k ~= v and mod and mod['config'] then
mod['config'](v)
end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment