9 module:set_global(); |
9 module:set_global(); |
10 module:depends("http_errors"); |
10 module:depends("http_errors"); |
11 |
11 |
12 local moduleapi = require "core.moduleapi"; |
12 local moduleapi = require "core.moduleapi"; |
13 local url_parse = require "socket.url".parse; |
13 local url_parse = require "socket.url".parse; |
|
14 local url_build = require "socket.url".build; |
14 |
15 |
15 local server = require "net.http.server"; |
16 local server = require "net.http.server"; |
16 |
17 |
17 server.set_default_host(module:get_option_string("http_default_host")); |
18 server.set_default_host(module:get_option_string("http_default_host")); |
18 |
19 |
40 return normalize_path(host_module:get_option("http_paths", {})[app_name] -- Host |
41 return normalize_path(host_module:get_option("http_paths", {})[app_name] -- Host |
41 or module:get_option("http_paths", {})[app_name] -- Global |
42 or module:get_option("http_paths", {})[app_name] -- Global |
42 or default_app_path); -- Default |
43 or default_app_path); -- Default |
43 end |
44 end |
44 |
45 |
|
46 local ports_by_scheme = { http = 80, https = 443, }; |
|
47 |
45 -- Helper to deduce a module's external URL |
48 -- Helper to deduce a module's external URL |
46 function moduleapi.http_url(module, app_name, default_path) |
49 function moduleapi.http_url(module, app_name, default_path) |
47 app_name = app_name or (module.name:gsub("^http_", "")); |
50 app_name = app_name or (module.name:gsub("^http_", "")); |
48 local ext = url_parse(module:get_option_string("http_external_url")) or {}; |
51 local ext = url_parse(module:get_option_string("http_external_url")) or {}; |
49 local services = portmanager.get_active_services(); |
52 local services = portmanager.get_active_services(); |
50 local http_services = services:get("https") or services:get("http") or {}; |
53 local http_services = services:get("https") or services:get("http") or {}; |
51 for interface, ports in pairs(http_services) do |
54 for interface, ports in pairs(http_services) do |
52 for port, services in pairs(ports) do |
55 for port, services in pairs(ports) do |
53 local path = get_base_path(module, app_name, default_path or "/"..app_name); |
56 local url = { |
54 port = tonumber(ext.port) or port or 80; |
57 scheme = (ext.scheme or services[1].service.name); |
55 if port == 80 then port = ""; else port = ":"..port; end |
58 host = (ext.host or module.host); |
56 return (ext.scheme or services[1].service.name).."://" |
59 port = tonumber(ext.port) or port or 80; |
57 ..(ext.host or module.host)..port |
60 path = normalize_path(ext.path or "/").. |
58 ..normalize_path(ext.path or "/")..(path:sub(2)); |
61 (get_base_path(module, app_name, default_path or "/"..app_name):sub(2)); |
|
62 } |
|
63 if ports_by_scheme[url.scheme] == url.port then url.port = nil end |
|
64 return url_build(url); |
59 end |
65 end |
60 end |
66 end |
61 end |
67 end |
62 |
68 |
63 function module.add_host(module) |
69 function module.add_host(module) |