Lua binding: Optimize code
Signed-off-by: Jianhui Zhao <jianhuizhao329@gmail.com>
This commit is contained in:
1 parent
ddf6e8dd86
commit
deaec21076
2 files changed
+53
-44
No files matched your search
+23
-26
@@ -25,6 +25,24 @@ local uh = require "uhttpd"
|
||||
local verbose = true
|
||||
local port = 8914
|
||||
|
||||
uloop.init()
|
||||
|
||||
-- LOG_DEBUG LOG_INFO LOG_ERR
|
||||
if not verbose then
|
||||
uh.set_log_threshold(uh.LOG_ERR)
|
||||
end
|
||||
|
||||
uh.log(uh.LOG_INFO, "uhttpd version:" .. uh.VERSION)
|
||||
|
||||
local srv = uh.new(port)
|
||||
|
||||
-- srv:ssl_init("uhttpd.crt", "uhttpd.key")
|
||||
-- srv:set_options({docroot = "/home/zjh/www", index = "lua.html"})
|
||||
|
||||
uh.log(uh.LOG_INFO, "Listen on:" .. port)
|
||||
|
||||
|
||||
|
||||
local http_methods = {
|
||||
[uh.HTTP_METHOD_GET] = "GET",
|
||||
[uh.HTTP_METHOD_POST] = "POST",
|
||||
@@ -36,14 +54,15 @@ local http_version = {
|
||||
[uh.HTTP_VER_11] = "HTTP/1.1"
|
||||
}
|
||||
|
||||
local function on_error404(cl, path)
|
||||
srv:on_error404(function(cl, path)
|
||||
uh.send_header(cl, 200, "OK", -1)
|
||||
uh.header_end(cl)
|
||||
uh.chunk_send(cl, string.format("<h1>Libuhttpd-Lua: '%s' Not found</h1>", path))
|
||||
uh.request_done(cl)
|
||||
end
|
||||
end)
|
||||
|
||||
local function on_request(cl, path)
|
||||
|
||||
srv:on_request(function(cl, path)
|
||||
if path ~= "/hello" then
|
||||
return uh.REQUEST_CONTINUE
|
||||
end
|
||||
@@ -85,29 +104,7 @@ local function on_request(cl, path)
|
||||
uh.request_done(cl)
|
||||
|
||||
return uh.REQUEST_DONE
|
||||
end
|
||||
end)
|
||||
|
||||
|
||||
-- LOG_DEBUG LOG_INFO LOG_ERR
|
||||
if not verbose then
|
||||
uh.set_log_threshold(uh.LOG_ERR)
|
||||
end
|
||||
|
||||
uloop.init()
|
||||
|
||||
uh.log(uh.LOG_INFO, "uhttpd version:" .. uh.VERSION)
|
||||
|
||||
local srv = uh.new(port)
|
||||
|
||||
-- srv:ssl_init("uhttpd.crt", "uhttpd.key")
|
||||
|
||||
uh.log(uh.LOG_INFO, "Listen on:" .. port)
|
||||
|
||||
srv:set_options({
|
||||
docroot = "/home/zjh/www", -- Default is .
|
||||
index = "lua.html", -- Default is index.html
|
||||
on_error404 = on_error404,
|
||||
on_request = on_request
|
||||
})
|
||||
|
||||
uloop.run()
|
||||
Reference in new issue
Block a user