1
0
mirror of https://github.com/lxsang/antd-lua-plugin synced 2025-02-13 23:12:47 +01:00

using new plugin interface

This commit is contained in:
lxsang 2018-10-05 19:02:35 +02:00
parent 564afeb134
commit 06b2ec0e8f
3 changed files with 46 additions and 53 deletions

View File

@ -3,22 +3,24 @@ __ROOT__ = __api__.root
-- set require path -- set require path
package.path = __ROOT__ .. '/?.lua;'..__api__.apiroot..'/?.lua' package.path = __ROOT__ .. '/?.lua;'..__api__.apiroot..'/?.lua'
package.cpath = __api__.apiroot..'/?.llib' package.cpath = __api__.apiroot..'/?.llib'
require("std")
require("utils")
require("extra_mime")
-- set session -- set session
SESSION = {} SESSION = {}
if REQUEST.query ~= nil and REQUEST.query.cookie ~= nil then
SESSION = REQUEST.query.cookie REQUEST = HTTP_REQUEST.request.REQUEST_DATA
REQUEST.method = HTTP_REQUEST.request.METHOD
if HTTP_REQUEST.request.COOKIE then
SESSION = HTTP_REQUEST.request.COOKIE
end end
HEADER = REQUEST.query.__xheader__ HEADER = HTTP_REQUEST.request.REQUEST_HEADER
HEADER.mobile = false HEADER.mobile = false
if HEADER["User-Agent"] and HEADER["User-Agent"]:match("Mobi") then if HEADER["User-Agent"] and HEADER["User-Agent"]:match("Mobi") then
HEADER.mobile = true HEADER.mobile = true
end end
require("std")
require("utils")
require("extra_mime")
function has_module(m) function has_module(m)
if utils.file_exists(__ROOT__..'/'..m) then if utils.file_exists(__ROOT__..'/'..m) then
if m:find("%.ls$") then if m:find("%.ls$") then
@ -128,7 +130,7 @@ end
-- run the file -- run the file
local m, s, p = has_module(REQUEST.path) local m, s, p = has_module(HTTP_REQUEST.request.RESOURCE_PATH)
if m then if m then
-- run the correct module -- run the correct module
if s then if s then
@ -138,7 +140,7 @@ if m then
require(p) require(p)
end end
else else
unknow("Resource not found for request "..REQUEST.path) unknow("Resource not found for request "..HTTP_REQUEST.request.RESOURCE_PATH)
end end

View File

@ -2,13 +2,13 @@ std = modules.std()
bytes = modules.bytes() bytes = modules.bytes()
array = modules.array() array = modules.array()
function std.html() function std.html()
std._html(REQUEST.id) std._html(HTTP_REQUEST.id)
end end
function std.text() function std.text()
std._text(REQUEST.id) std._text(HTTP_REQUEST.id)
end end
function std.status(code, msg) function std.status(code, msg)
std._status(REQUEST.id, code, msg) std._status(HTTP_REQUEST.id, code, msg)
end end
function std.custom_header(k,v) function std.custom_header(k,v)
--print(k..":"..v) --print(k..":"..v)
@ -19,38 +19,38 @@ function std.header_flush()
end end
--_redirect --_redirect
function std.redirect(s) function std.redirect(s)
std._redirect(REQUEST.id,s) std._redirect(HTTP_REQUEST.id,s)
end end
function std.json() function std.json()
std._json(REQUEST.id) std._json(HTTP_REQUEST.id)
end end
function std.jpeg() function std.jpeg()
std._jpeg(REQUEST.id) std._jpeg(HTTP_REQUEST.id)
end end
function std.header(s) function std.header(s)
std._header(REQUEST.id,s) std._header(HTTP_REQUEST.id,s)
end end
function std.octstream(s) function std.octstream(s)
std._octstream(REQUEST.id,s) std._octstream(HTTP_REQUEST.id,s)
end end
function std.textstream() function std.textstream()
std._textstream(REQUEST.id) std._textstream(HTTP_REQUEST.id)
end end
function std.ti(v) function std.ti(v)
std._ti(REQUEST.id,v) std._ti(HTTP_REQUEST.id,v)
end end
function std.t(s) function std.t(s)
std._t(REQUEST.id,s) std._t(HTTP_REQUEST.id,s)
end end
function std.f(v) function std.f(v)
std._f(REQUEST.id,v) std._f(HTTP_REQUEST.id,v)
end end
function std.fb(v) function std.fb(v)
std._fb(REQUEST.id,v) std._fb(HTTP_REQUEST.id,v)
end end
function std.setCookie(t,v,p) function std.setCookie(t,v,p)
p = p or "" p = p or ""
std._setCookie(REQUEST.id,t,v,p) std._setCookie(HTTP_REQUEST.id,t,v,p)
end end
function std.cjson(v, p) function std.cjson(v, p)
@ -65,7 +65,7 @@ end
--_upload --_upload
--_route --_route
function std.unknow(s) function std.unknow(s)
std._unknow(REQUEST.id,s) std._unknow(HTTP_REQUEST.id,s)
end end
function std.readOnly(t) -- bugging function std.readOnly(t) -- bugging
@ -84,7 +84,7 @@ function std.readOnly(t) -- bugging
-- web socket -- web socket
std.ws = {} std.ws = {}
function std.ws.header() function std.ws.header()
local h = std.ws_header(REQUEST.id) local h = std.ws_header(HTTP_REQUEST.id)
if(h) then if(h) then
return h --std.readOnly(h) return h --std.readOnly(h)
else else
@ -93,22 +93,22 @@ function std.ws.header()
end end
function std.ws.read(h) function std.ws.read(h)
return std.ws_read(REQUEST.id,h) return std.ws_read(HTTP_REQUEST.id,h)
end end
function std.ws.swrite(s) function std.ws.swrite(s)
std.ws_t(REQUEST.id,s) std.ws_t(HTTP_REQUEST.id,s)
end end
function std.ws.fwrite(s) function std.ws.fwrite(s)
std.ws_f(REQUEST.id,s) std.ws_f(HTTP_REQUEST.id,s)
end end
function std.ws.write_bytes(arr) function std.ws.write_bytes(arr)
std.ws_b(REQUEST.id,arr) std.ws_b(HTTP_REQUEST.id,arr)
end end
function std.ws.enable() function std.ws.enable()
return REQUEST.query ~= nil and REQUEST.query["__web_socket__"] == "1" return HTTP_REQUEST.query ~= nil and HTTP_REQUEST.query["__web_socket__"] == "1"
end end
function std.ws.close(code) function std.ws.close(code)
std.ws_close(REQUEST.id,code) std.ws_close(HTTP_REQUEST.id,code)
end end
function std.basename(str) function std.basename(str)
local name = string.gsub(std.trim(str,"/"), "(.*/)(.*)", "%2") local name = string.gsub(std.trim(str,"/"), "(.*/)(.*)", "%2")

View File

@ -13,9 +13,6 @@ static const struct luaL_Reg modules [] = {
void init() void init()
{ {
//signal(SIGPIPE, SIG_IGN);
//signal(SIGABRT, SIG_IGN);
// init the plugin here
LOG("%s \n","INIT LUA HANDLER"); LOG("%s \n","INIT LUA HANDLER");
} }
/** /**
@ -33,7 +30,7 @@ static void push_dict_to_lua(lua_State* L, dictionary d)
{ {
lua_pushstring(L,as->key); lua_pushstring(L,as->key);
//printf("KEY %s\n", as->key); //printf("KEY %s\n", as->key);
if(EQU(as->key,"cookie") || EQU(as->key,"__xheader__")) if(EQU(as->key,"COOKIE") || EQU(as->key,"REQUEST_HEADER") || EQU(as->key,"REQUEST_DATA") )
push_dict_to_lua(L, (dictionary)as->value); push_dict_to_lua(L, (dictionary)as->value);
else else
{ {
@ -43,8 +40,10 @@ static void push_dict_to_lua(lua_State* L, dictionary d)
lua_settable(L, -3); lua_settable(L, -3);
} }
} }
void handle(void* client, const char* method, const char* path, dictionary rq) void* handle(void* data)
{ {
antd_request_t* rq = (antd_request_t*) data;
plugin_header_t* __plugin__ = meta();
lua_State* L = NULL; lua_State* L = NULL;
//char * index = __s("%s/%s",__plugin__.htdocs,"router.lua"); //char * index = __s("%s/%s",__plugin__.htdocs,"router.lua");
char* cnf = config_dir(); char* cnf = config_dir();
@ -58,11 +57,11 @@ void handle(void* client, const char* method, const char* path, dictionary rq)
// API header // API header
lua_newtable(L); lua_newtable(L);
lua_pushstring(L,"name"); lua_pushstring(L,"name");
lua_pushstring(L, __plugin__.name); lua_pushstring(L, __plugin__->name);
lua_settable(L,-3); lua_settable(L,-3);
lua_pushstring(L,"root"); lua_pushstring(L,"root");
lua_pushstring(L, __plugin__.htdocs); lua_pushstring(L, __plugin__->htdocs);
lua_settable(L,-3); lua_settable(L,-3);
lua_pushstring(L,"apiroot"); lua_pushstring(L,"apiroot");
@ -74,22 +73,13 @@ void handle(void* client, const char* method, const char* path, dictionary rq)
// Request // Request
lua_newtable(L); lua_newtable(L);
lua_pushstring(L,"id"); lua_pushstring(L,"id");
lua_pushlightuserdata(L, client); lua_pushlightuserdata(L, rq->client);
//lua_pushnumber(L,client); //lua_pushnumber(L,client);
lua_settable(L, -3); lua_settable(L, -3);
lua_pushstring(L,"request");
lua_pushstring(L,"method"); push_dict_to_lua(L,rq->request);
lua_pushstring(L,method);
lua_settable(L, -3); lua_settable(L, -3);
lua_setglobal(L, "HTTP_REQUEST");
lua_pushstring(L,"path");
lua_pushstring(L,path);
lua_settable(L, -3);
lua_pushstring(L,"query");
push_dict_to_lua(L,rq);
lua_settable(L, -3);
lua_setglobal(L, "REQUEST");
// load major apis // load major apis
if(is_file(apis)) if(is_file(apis))
@ -111,9 +101,10 @@ void handle(void* client, const char* method, const char* path, dictionary rq)
free(cnf); free(cnf);
if(apis) if(apis)
free(apis); free(apis);
return antd_create_task(NULL, (void*)rq, NULL);
//lua_close(L); //lua_close(L);
} }
void pexit() void destroy()
{ {
LOG("%s \n","Exit LUA Handler"); LOG("%s \n","Exit LUA Handle");
} }