|
2 | 2 | local log = require('log').new("http_api") |
3 | 3 | local schema = require('experimental.config.utils.schema') |
4 | 4 |
|
5 | | -local scheme_node = schema.enum({ 'http', 'https' }) |
6 | | -local host_node = schema.scalar({ type = 'string' }) |
7 | | -local port_node = schema.scalar({ type = 'integer' }) |
8 | | - |
9 | | -local scheme_from_env = schema.fromenv('HTTP_SCHEME', os.getenv('HTTP_SCHEME'), scheme_node) |
10 | | -local host_from_env = schema.fromenv('HTTP_HOST', os.getenv('HTTP_HOST'), host_node) |
11 | | -local port_from_env = schema.fromenv('HTTP_PORT', os.getenv('HTTP_PORT'), port_node) |
12 | | - |
13 | 5 | local listen_address_schema = schema.new('listen_address', schema.record({ |
14 | | - scheme = scheme_node, |
15 | | - host = host_node, |
16 | | - port = port_node |
| 6 | + scheme = schema.enum({ 'http', 'https' }, { env = 'HTTP_SCHEME' }), |
| 7 | + host = schema.scalar({ type = 'string', env = 'HTTP_HOST' }), |
| 8 | + port = schema.scalar({ type = 'integer', env = 'HTTP_PORT' }) |
17 | 9 | })) |
18 | 10 |
|
| 11 | +local function collect_env_cfg() |
| 12 | + local res = {} |
| 13 | + for _, w in listen_address_schema:pairs() do |
| 14 | + local env_var = w.schema.env |
| 15 | + if env_var ~= nil then |
| 16 | + local value = schema.fromenv(env_var, os.getenv(env_var), w.schema) |
| 17 | + listen_address_schema:set(res, w.path, value) |
| 18 | + end |
| 19 | + end |
| 20 | + return res |
| 21 | +end |
| 22 | + |
19 | 23 | local function validate(cfg) |
20 | | - listen_address_schema:set(cfg, 'scheme', scheme_from_env) |
21 | | - listen_address_schema:set(cfg, 'host', host_from_env) |
22 | | - listen_address_schema:set(cfg, 'port', port_from_env) |
23 | | - listen_address_schema:validate(cfg) |
| 24 | + local env_cfg = collect_env_cfg() |
| 25 | + local result_cfg = listen_address_schema:merge(cfg, env_cfg) |
| 26 | + listen_address_schema:validate(result_cfg) |
24 | 27 | end |
25 | 28 |
|
26 | 29 | local function apply(cfg) |
27 | | - local scheme = listen_address_schema:get(cfg, 'scheme') |
28 | | - local host = listen_address_schema:get(cfg, 'host') |
29 | | - local port = listen_address_schema:get(cfg, 'port') |
30 | | - log.info("HTTP API endpoint: %s://%s:%d", scheme, host, port) |
| 30 | + local env_cfg = collect_env_cfg() |
| 31 | + local result_cfg = listen_address_schema:merge(cfg, env_cfg) |
| 32 | + log.info("HTTP API endpoint: %s://%s:%d", result_cfg.scheme, result_cfg.host, result_cfg.port) |
31 | 33 | end |
32 | 34 |
|
33 | 35 | local function stop() |
|
0 commit comments