29 end |
32 end |
30 return method:upper().." "..host..app_path..path; |
33 return method:upper().." "..host..app_path..path; |
31 end |
34 end |
32 |
35 |
33 local function get_base_path(host_module, app_name, default_app_path) |
36 local function get_base_path(host_module, app_name, default_app_path) |
34 return host_module:get_option("http_paths", {})[app_name] -- Host |
37 return normalize_path(host_module:get_option("http_paths", {})[app_name] -- Host |
35 or module:get_option("http_paths", {})[app_name] -- Global |
38 or module:get_option("http_paths", {})[app_name] -- Global |
36 or default_app_path; -- Default |
39 or default_app_path); -- Default |
|
40 end |
|
41 |
|
42 -- Helper to deduce a module's external URL |
|
43 function moduleapi.http_url(module, app_name, default_path) |
|
44 app_name = app_name or (module.name:gsub("^http_", "")); |
|
45 local ext = url_parse(module:get_option_string("http_external_url")) or {}; |
|
46 local services = portmanager.get_active_services(); |
|
47 local http_services = services:get("https") or services:get("http"); |
|
48 for interface, ports in pairs(http_services) do |
|
49 for port, services in pairs(ports) do |
|
50 local path = get_base_path(module, app_name, default_path or "/"..app_name); |
|
51 port = tonumber(ext.port) or port or 80; |
|
52 if port == 80 then port = ""; else port = ":"..port; end |
|
53 return (ext.scheme or services[1].service.name).."://" |
|
54 ..(ext.host or module.host)..port |
|
55 ..normalize_path(ext.path or "/")..(path:sub(2)); |
|
56 end |
|
57 end |
37 end |
58 end |
38 |
59 |
39 function module.add_host(module) |
60 function module.add_host(module) |
40 local host = module.host; |
61 local host = module.host; |
41 local apps = {}; |
62 local apps = {}; |
42 module.environment.apps = apps; |
63 module.environment.apps = apps; |
43 local function http_app_added(event) |
64 local function http_app_added(event) |
44 local app_name = event.item.name; |
65 local app_name = event.item.name; |
45 local default_app_path = event.item.default_path or "/"..app_name; |
66 local default_app_path = event.item.default_path or "/"..app_name; |
46 local app_path = normalize_path(get_base_path(module, app_name, default_app_path)); |
67 local app_path = get_base_path(module, app_name, default_app_path); |
47 if not app_name then |
68 if not app_name then |
48 -- TODO: Link to docs |
69 -- TODO: Link to docs |
49 module:log("error", "HTTP app has no 'name', add one or use module:provides('http', app)"); |
70 module:log("error", "HTTP app has no 'name', add one or use module:provides('http', app)"); |
50 return; |
71 return; |
51 end |
72 end |
52 apps[app_name] = apps[app_name] or {}; |
73 apps[app_name] = apps[app_name] or {}; |