]> Untitled Git - public.git/blob - lua/publish.lua
c0a4ff79347da9cb1fe0a92999a0dd9c63a27a81
[public.git] / lua / publish.lua
1 require "apache2"
2
3 function publish(dir, pattern)
4     local cmd = ("%s/publish.sh %s"):format(dir, pattern)
5     local handle = io.popen(cmd)
6     local output = handle:read('*a')
7     handle:close()
8
9     return output
10 end
11
12 function get_form(dir)
13     local handle = io.open(dir .. '/submit.html')
14     local output = handle:read('*a')
15     handle:close()
16
17     return output;
18 end
19
20 function handle(r)
21     r.content_type = "text/html"
22
23     local args = r:parseargs()
24     local pattern = args.pattern 
25
26     if pattern then
27         local nonce = publish(r.context_document_root, pattern)
28         if nonce == "" then
29             r:puts( "No links created" )
30         else
31             -- if FQDN override is set,
32             -- use override instead of my own server name and port number
33             local fqdn = os.getenv('PUBLIC_FQDN_OVERRIDE')
34             fqdn = fqdn or
35                 (r.is_https and "https" or "http") ..
36                 "://" .. r.server_name .. ":" ..r.port
37
38             local uri = ("%s/%s/"):format(fqdn, nonce)
39             r:puts( ([[<a href="%s">%s</a>]]):format(uri, uri) )
40         end
41     else
42         local template = get_form(r.context_document_root)
43         r:puts( template )
44     end
45
46     return apache2.OK
47 end