|
| 1 | +local urilib = require("uri") |
| 2 | +local http_server = require('http.server') |
| 3 | + |
| 4 | +local M = { |
| 5 | + DEFAULT_SERVER_NAME = 'default', |
| 6 | +} |
| 7 | +local servers = {} |
| 8 | + |
| 9 | +local function parse_listen(listen) |
| 10 | + if listen == nil then |
| 11 | + return nil, nil, "must exist" |
| 12 | + end |
| 13 | + if type(listen) ~= "string" and type(listen) ~= "number" then |
| 14 | + return nil, nil, "must be a string or a number, got " .. type(listen) |
| 15 | + end |
| 16 | + |
| 17 | + local host |
| 18 | + local port |
| 19 | + if type(listen) == "string" then |
| 20 | + local uri, err = urilib.parse(listen) |
| 21 | + if err ~= nil then |
| 22 | + return nil, nil, "failed to parse URI: " .. err |
| 23 | + end |
| 24 | + |
| 25 | + if uri.scheme ~= nil then |
| 26 | + if uri.scheme == "unix" then |
| 27 | + uri.unix = uri.path |
| 28 | + else |
| 29 | + return nil, nil, "URI scheme is not supported" |
| 30 | + end |
| 31 | + end |
| 32 | + |
| 33 | + if uri.login ~= nil or uri.password then |
| 34 | + return nil, nil, "URI login and password are not supported" |
| 35 | + end |
| 36 | + |
| 37 | + if uri.query ~= nil then |
| 38 | + return nil, nil, "URI query component is not supported" |
| 39 | + end |
| 40 | + |
| 41 | + if uri.unix ~= nil then |
| 42 | + host = "unix/" |
| 43 | + port = uri.unix |
| 44 | + else |
| 45 | + if uri.service == nil then |
| 46 | + return nil, nil, "URI must contain a port" |
| 47 | + end |
| 48 | + |
| 49 | + port = tonumber(uri.service) |
| 50 | + if port == nil then |
| 51 | + return nil, nil, "URI port must be a number" |
| 52 | + end |
| 53 | + if uri.host ~= nil then |
| 54 | + host = uri.host |
| 55 | + elseif uri.ipv4 ~= nil then |
| 56 | + host = uri.ipv4 |
| 57 | + elseif uri.ipv6 ~= nil then |
| 58 | + host = uri.ipv6 |
| 59 | + else |
| 60 | + host = "0.0.0.0" |
| 61 | + end |
| 62 | + end |
| 63 | + elseif type(listen) == "number" then |
| 64 | + host = "0.0.0.0" |
| 65 | + port = listen |
| 66 | + end |
| 67 | + |
| 68 | + if type(port) == "number" and (port < 1 or port > 65535) then |
| 69 | + return nil, nil, "port must be in the range [1, 65535]" |
| 70 | + end |
| 71 | + return host, port, nil |
| 72 | +end |
| 73 | + |
| 74 | +local function apply_http(name, node) |
| 75 | + local host, port, err = parse_listen(node.listen) |
| 76 | + if err ~= nil then |
| 77 | + error("failed to parse URI: " .. err) |
| 78 | + end |
| 79 | + |
| 80 | + if servers[name] == nil then |
| 81 | + local httpd = http_server.new(host, port) |
| 82 | + httpd:start() |
| 83 | + servers[name] = { |
| 84 | + httpd = httpd, |
| 85 | + routes = {}, |
| 86 | + } |
| 87 | + end |
| 88 | +end |
| 89 | + |
| 90 | +M.validate = function(conf) |
| 91 | + if conf ~= nil and type(conf) ~= "table" then |
| 92 | + error("configuration must be a table, got " .. type(conf)) |
| 93 | + end |
| 94 | + conf = conf or {} |
| 95 | + |
| 96 | + for name, node in pairs(conf) do |
| 97 | + if type(name) ~= 'string' then |
| 98 | + error("name of the server must be a string") |
| 99 | + end |
| 100 | + |
| 101 | + local _, _, err = parse_listen(node.listen) |
| 102 | + if err ~= nil then |
| 103 | + error("failed to parse http 'listen' param: " .. err) |
| 104 | + end |
| 105 | + end |
| 106 | +end |
| 107 | + |
| 108 | +M.apply = function(conf) |
| 109 | + -- This should be called on the role's lifecycle, but it's better to give |
| 110 | + -- a meaningful error if something goes wrong. |
| 111 | + M.validate(conf) |
| 112 | + |
| 113 | + for name, node in pairs(conf or {}) do |
| 114 | + apply_http(name, node) |
| 115 | + end |
| 116 | +end |
| 117 | + |
| 118 | +M.stop = function() |
| 119 | + for _, server in pairs(servers) do |
| 120 | + server.httpd:stop() |
| 121 | + end |
| 122 | + servers = {} |
| 123 | +end |
| 124 | + |
| 125 | +M.get_server = function(name) |
| 126 | + checks('?string') |
| 127 | + |
| 128 | + name = name or M.DEFAULT_SERVER_NAME |
| 129 | + if type(name) == 'string' then |
| 130 | + return servers[name] |
| 131 | + end |
| 132 | +end |
| 133 | + |
| 134 | +return M |
0 commit comments