tstorch / mongoose

Automatically exported from code.google.com/p/mongoose
MIT License
0 stars 0 forks source link

can anybody give me an example by mongoose working with cgilua? (just a request) #305

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
mongoose is a nice ligthweight webserver,and cgilua has a simple template 
system. I just write a simple swig lua wrapper with mongoose. and try to make 
it work with cgilua,but failed. the cgilua has not good document to refer. 

does any one had good suggestion?

require "shttp"

function mg_callback(event,conn,info)

    match=string.match(info.uri,"%.lua$") or string.match(info.uri,"%.lp$")
    if match then
                -- how can i process request with cgilua?
                local f = assert(io.open("index.html", "r"))
        local t = f:read("*all")
        f:close()
                shttp.mg_write(conn,t)
        return "processed"
    else
        return nil
    end
end

svr=shttp.mg_start(mg_callback,{})
io.stdin:read('*l')

Original issue reported on code.google.com by xueyaos...@gmail.com on 3 Jan 2012 at 2:47

Attachments:

GoogleCodeExporter commented 9 years ago
I would recommend you start with a simple cgi script that can be executed in 
the Lua standalone interpreter 
(http://luabinaries.sourceforge.net/download.html, e.g., Lua5.1.exe from 
lua5_1_4_Win32_bin.zip). Make sure this executable is set as the CGI 
interpreter in the mongoose.conf file (Option: I Lua5.1) and all paths are set 
correctly. A short example script (lua.cgi) is below. This way you can 
establish and test a connection between mongoose an Lua without making changes 
in the source code of any of them. 
Maybe taking the script below as a starting point and replacing the explicit 
"print" calls by cgilua functions might help.

#!/usr/bin/lua5.1

-- First calculate response body, but do not send it yet

resp = [[
<http>
  <head>
    <title>Response of lua.cgi</title>
  </head>
  <body>
    <h1>Response of lua.cgi:</h1>
    <p>
]]

userName = os.getenv("REMOTE_USER")

if userName then 
   resp = resp .. "You are logged in as \"" .. userName .. "\".<br>";
else
   resp = resp .. "You are not logged in.<br>";   
end

resp = resp .. [[
    </p>
  </body>
</http>
]]

-- Now send a response head 

print "Connection: close"
print "Content-Type: text/html; charset=utf-8"
print "Cache-Control: no-cache"
print ("Content-Length: " .. resp:len())
print ""

-- Finally send the response body

print (resp)

Original comment by bel2...@gmail.com on 27 Feb 2012 at 3:00

GoogleCodeExporter commented 9 years ago
Thanks Bel!

Original comment by valenok on 22 Sep 2012 at 2:26