1
0
mirror of https://github.com/lxsang/antd-lua-plugin synced 2024-12-26 01:18:22 +01:00

improvement module loader

This commit is contained in:
lxsang 2021-01-01 15:22:25 +01:00
parent 7bc0a89ef5
commit 807e56e331
4 changed files with 150 additions and 107 deletions

BIN
dist/lua-0.5.2b.tar.gz vendored

Binary file not shown.

View File

@ -2,6 +2,10 @@ lib_LTLIBRARIES = ulib.la
ulib_la_LDFLAGS = -module -avoid-version -shared
ulib_la_SOURCES = 3rd/zip/zip.c ulib.c
lib_LTLIBRARIES += handle.la
handle_la_LDFLAGS = -module -avoid-version -shared
handle_la_SOURCES = handle.c
lib_LTLIBRARIES += antd.la
antd_la_LDFLAGS = -module -avoid-version -shared
antd_la_SOURCES = antd.c

106
lib/asl/handle.c Normal file
View File

@ -0,0 +1,106 @@
#include <antd/plugin.h>
#include <antd/scheduler.h>
#include <antd/dbhelper.h>
#include <sys/stat.h>
#include "../lualib.h"
/**
* convert antd dictionary to lua table
*
*/
static void push_dict_to_lua(lua_State* L, dictionary_t d)
{
lua_newtable(L);
chain_t as;
if(d)
for_each_assoc(as, d)
{
lua_pushstring(L,as->key);
//printf("KEY %s\n", as->key);
if(EQU(as->key,"COOKIE") || EQU(as->key,"REQUEST_HEADER") || EQU(as->key,"REQUEST_DATA") )
push_dict_to_lua(L, (dictionary_t)as->value);
else
{
lua_pushstring(L,as->value);
//printf("VALUE : %s\n",as->value );
}
lua_settable(L, -3);
}
}
void* lua_handle(void* data, void* meta)
{
antd_request_t* rq = (antd_request_t*) data;
char buf[BUFFLEN];
plugin_header_t* __plugin__ = meta;
lua_State* L = NULL;
//char * index = __s("%s/%s",__plugin__.htdocs,"router.lua");
char* cnf = __s("%s%s%s", __plugin__->pdir,DIR_SEP, __plugin__->name);
char * apis = __s("%s/%s",cnf,"api.lua");
L = luaL_newstate();
luaL_openlibs(L);
//module loader
//luaL_newlib(L, modules);
//lua_setglobal(L, "modules");
// set up global variable
// API header
lua_newtable(L);
lua_pushstring(L,"name");
lua_pushstring(L, __plugin__->name);
lua_settable(L,-3);
lua_pushstring(L,"root");
htdocs(rq, buf);
lua_pushstring(L, buf);
lua_settable(L,-3);
lua_pushstring(L,"apiroot");
lua_pushstring(L, cnf);
lua_settable(L,-3);
lua_pushstring(L,"tmpdir");
tmpdir(buf);
lua_pushstring(L, buf);
lua_settable(L,-3);
lua_pushstring(L,"dbpath");
lua_pushstring(L, __plugin__->dbpath);
lua_settable(L,-3);
lua_setglobal(L, "__api__");
// Request
lua_newtable(L);
lua_pushstring(L,"id");
lua_pushlightuserdata(L, rq->client);
//lua_pushnumber(L,client);
lua_settable(L, -3);
lua_pushstring(L,"request");
push_dict_to_lua(L,rq->request);
lua_settable(L, -3);
lua_setglobal(L, "HTTP_REQUEST");
// load major apis
if(is_file(apis))
if (luaL_loadfile(L, apis) || lua_pcall(L, 0, 0, 0))
{
ERROR( "cannot start API file: [%s] %s\n", apis, lua_tostring(L, -1));
}
/*if (luaL_loadfile(L, index) || lua_pcall(L, 0, 0, 0))
{
text(client);
__t(client, "Cannot run router: %s", lua_tostring(L, -1));
}
free(index);*/
// clear request
if(L)
lua_close(L);
if(cnf)
free(cnf);
if(apis)
free(apis);
return antd_create_task(NULL, (void*)rq, NULL,rq->client->last_io);
//lua_close(L);
}

147
lua-api.c
View File

@ -6,126 +6,59 @@
#include <sys/stat.h>
#include "lib/lualib.h"
static void* core_handle = NULL;
#define LUA_HDL_FN "lua_handle"
static void* core = NULL;
static void* lua_handle = NULL;
static void *(*handle_fn)(void *, void*);
void init()
{
char* error;
char* path = __s("%s/lua/core.so", __plugin__.pdir);
core_handle = dlopen(path, RTLD_NOW| RTLD_GLOBAL);
if(!core_handle)
{
ERROR("Cannot load Lua core; %s", dlerror());
}
else
{
LOG("Lua core loaded");
}
core = dlopen(path, RTLD_NOW| RTLD_GLOBAL);
free(path);
if(!core)
{
ERROR("Cannot load Lua core: %s", dlerror());
return;
}
LOG("Lua core loaded");
// now load the handle
path = __s("%s/lua/handle.so", __plugin__.pdir);
lua_handle = dlopen(path, RTLD_LAZY);
free(path);
if(!lua_handle)
{
ERROR("Cannot load lua_handle: %s", dlerror());
return;
}
// find the fn
handle_fn = (void *(*)(void *))dlsym(lua_handle, LUA_HDL_FN);
if ((error = dlerror()) != NULL)
{
ERROR("Problem when finding %s method from handle : %s", LUA_HDL_FN, error);
handle_fn = NULL;
return;
}
}
/**
* Plugin handler, reads request from the server and processes it
*
*/
static void push_dict_to_lua(lua_State* L, dictionary_t d)
{
lua_newtable(L);
chain_t as;
if(d)
for_each_assoc(as, d)
{
lua_pushstring(L,as->key);
//printf("KEY %s\n", as->key);
if(EQU(as->key,"COOKIE") || EQU(as->key,"REQUEST_HEADER") || EQU(as->key,"REQUEST_DATA") )
push_dict_to_lua(L, (dictionary_t)as->value);
else
{
lua_pushstring(L,as->value);
//printf("VALUE : %s\n",as->value );
}
lua_settable(L, -3);
}
}
void* handle(void* data)
{
antd_request_t* rq = (antd_request_t*) data;
char buf[BUFFLEN];
plugin_header_t* __plugin__ = meta();
lua_State* L = NULL;
//char * index = __s("%s/%s",__plugin__.htdocs,"router.lua");
char* cnf = config_dir();
char * apis = __s("%s/%s",cnf,"api.lua");
L = luaL_newstate();
luaL_openlibs(L);
//module loader
//luaL_newlib(L, modules);
//lua_setglobal(L, "modules");
// set up global variable
// API header
lua_newtable(L);
lua_pushstring(L,"name");
lua_pushstring(L, __plugin__->name);
lua_settable(L,-3);
lua_pushstring(L,"root");
htdocs(rq, buf);
lua_pushstring(L, buf);
lua_settable(L,-3);
lua_pushstring(L,"apiroot");
lua_pushstring(L, cnf);
lua_settable(L,-3);
lua_pushstring(L,"tmpdir");
tmpdir(buf);
lua_pushstring(L, buf);
lua_settable(L,-3);
lua_pushstring(L,"dbpath");
lua_pushstring(L, __plugin__->dbpath);
lua_settable(L,-3);
lua_setglobal(L, "__api__");
// Request
lua_newtable(L);
lua_pushstring(L,"id");
lua_pushlightuserdata(L, rq->client);
//lua_pushnumber(L,client);
lua_settable(L, -3);
lua_pushstring(L,"request");
push_dict_to_lua(L,rq->request);
lua_settable(L, -3);
lua_setglobal(L, "HTTP_REQUEST");
// load major apis
if(is_file(apis))
if (luaL_loadfile(L, apis) || lua_pcall(L, 0, 0, 0))
{
ERROR( "cannot start API file: [%s] %s\n", apis, lua_tostring(L, -1));
}
/*if (luaL_loadfile(L, index) || lua_pcall(L, 0, 0, 0))
plugin_header_t* meta_ptr = (void*)meta();
antd_request_t *rq = (antd_request_t *)data;
// find the handle function and execute it
if(!handle_fn)
{
text(client);
__t(client, "Cannot run router: %s", lua_tostring(L, -1));
antd_error(rq->client, 503, "Requested service not found");
return antd_create_task(NULL, (void *)rq, NULL, rq->client->last_io);
}
free(index);*/
// clear request
if(L)
lua_close(L);
if(cnf)
free(cnf);
if(apis)
free(apis);
return antd_create_task(NULL, (void*)rq, NULL,rq->client->last_io);
//lua_close(L);
return handle_fn(data, meta_ptr);
}
void destroy()
{
if(core_handle)
dlclose(core_handle);
if(core)
(void)dlclose(core);
if(lua_handle)
(void)dlclose(lua_handle);
LOG("Exit LUA Handle");
}