2019-11-19 17:13:39 +01:00
|
|
|
#define PLUGIN_IMPLEMENT 1
|
2020-01-01 21:17:46 +01:00
|
|
|
#include <dlfcn.h>
|
2020-01-02 17:51:08 +01:00
|
|
|
#include <antd/plugin.h>
|
2020-08-25 17:09:04 +02:00
|
|
|
#include <antd/scheduler.h>
|
|
|
|
#include <antd/dbhelper.h>
|
|
|
|
#include <sys/stat.h>
|
2020-01-02 17:51:08 +01:00
|
|
|
#include "lib/lualib.h"
|
2018-09-19 15:08:49 +02:00
|
|
|
|
2020-01-01 21:17:46 +01:00
|
|
|
static void* core_handle = NULL;
|
|
|
|
|
2018-09-19 15:08:49 +02:00
|
|
|
|
|
|
|
void init()
|
|
|
|
{
|
2020-01-01 21:17:46 +01:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
free(path);
|
2018-09-19 15:08:49 +02:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Plugin handler, reads request from the server and processes it
|
|
|
|
*
|
|
|
|
*/
|
2019-12-15 12:08:35 +01:00
|
|
|
static void push_dict_to_lua(lua_State* L, dictionary_t d)
|
2018-09-19 15:08:49 +02:00
|
|
|
{
|
|
|
|
lua_newtable(L);
|
|
|
|
|
2019-12-15 12:08:35 +01:00
|
|
|
chain_t as;
|
2018-09-19 15:08:49 +02:00
|
|
|
if(d)
|
|
|
|
for_each_assoc(as, d)
|
|
|
|
{
|
|
|
|
lua_pushstring(L,as->key);
|
|
|
|
//printf("KEY %s\n", as->key);
|
2018-10-05 19:02:35 +02:00
|
|
|
if(EQU(as->key,"COOKIE") || EQU(as->key,"REQUEST_HEADER") || EQU(as->key,"REQUEST_DATA") )
|
2019-12-15 12:08:35 +01:00
|
|
|
push_dict_to_lua(L, (dictionary_t)as->value);
|
2018-09-19 15:08:49 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
lua_pushstring(L,as->value);
|
|
|
|
//printf("VALUE : %s\n",as->value );
|
|
|
|
}
|
|
|
|
lua_settable(L, -3);
|
|
|
|
}
|
|
|
|
}
|
2018-10-05 19:02:35 +02:00
|
|
|
void* handle(void* data)
|
2018-09-19 15:08:49 +02:00
|
|
|
{
|
2018-10-05 19:02:35 +02:00
|
|
|
antd_request_t* rq = (antd_request_t*) data;
|
2020-01-08 22:13:41 +01:00
|
|
|
char buf[BUFFLEN];
|
2018-10-05 19:02:35 +02:00
|
|
|
plugin_header_t* __plugin__ = meta();
|
2018-09-19 15:08:49 +02:00
|
|
|
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
|
2020-01-02 17:51:08 +01:00
|
|
|
//luaL_newlib(L, modules);
|
|
|
|
//lua_setglobal(L, "modules");
|
2018-09-19 15:08:49 +02:00
|
|
|
// set up global variable
|
|
|
|
// API header
|
|
|
|
lua_newtable(L);
|
|
|
|
lua_pushstring(L,"name");
|
2018-10-05 19:02:35 +02:00
|
|
|
lua_pushstring(L, __plugin__->name);
|
2018-09-19 15:08:49 +02:00
|
|
|
lua_settable(L,-3);
|
|
|
|
|
|
|
|
lua_pushstring(L,"root");
|
2020-01-08 22:13:41 +01:00
|
|
|
htdocs(rq, buf);
|
|
|
|
lua_pushstring(L, buf);
|
2018-09-19 15:08:49 +02:00
|
|
|
lua_settable(L,-3);
|
|
|
|
|
|
|
|
lua_pushstring(L,"apiroot");
|
|
|
|
lua_pushstring(L, cnf);
|
|
|
|
lua_settable(L,-3);
|
2019-12-20 19:29:23 +01:00
|
|
|
|
|
|
|
lua_pushstring(L,"tmpdir");
|
2020-01-08 22:13:41 +01:00
|
|
|
tmpdir(buf);
|
|
|
|
lua_pushstring(L, buf);
|
2019-12-20 19:29:23 +01:00
|
|
|
lua_settable(L,-3);
|
2020-01-02 17:51:08 +01:00
|
|
|
|
|
|
|
lua_pushstring(L,"dbpath");
|
|
|
|
lua_pushstring(L, __plugin__->dbpath);
|
|
|
|
lua_settable(L,-3);
|
2018-09-19 15:08:49 +02:00
|
|
|
|
|
|
|
lua_setglobal(L, "__api__");
|
|
|
|
|
|
|
|
// Request
|
|
|
|
lua_newtable(L);
|
|
|
|
lua_pushstring(L,"id");
|
2018-10-05 19:02:35 +02:00
|
|
|
lua_pushlightuserdata(L, rq->client);
|
2018-09-19 15:08:49 +02:00
|
|
|
//lua_pushnumber(L,client);
|
|
|
|
lua_settable(L, -3);
|
2018-10-05 19:02:35 +02:00
|
|
|
lua_pushstring(L,"request");
|
|
|
|
push_dict_to_lua(L,rq->request);
|
2018-09-19 15:08:49 +02:00
|
|
|
lua_settable(L, -3);
|
2018-10-05 19:02:35 +02:00
|
|
|
lua_setglobal(L, "HTTP_REQUEST");
|
2018-09-19 15:08:49 +02:00
|
|
|
|
|
|
|
// load major apis
|
|
|
|
if(is_file(apis))
|
|
|
|
if (luaL_loadfile(L, apis) || lua_pcall(L, 0, 0, 0))
|
|
|
|
{
|
2020-01-02 17:51:08 +01:00
|
|
|
ERROR( "cannot start API file: [%s] %s\n", apis, lua_tostring(L, -1));
|
2018-09-19 15:08:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*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);
|
2019-12-15 16:56:16 +01:00
|
|
|
return antd_create_task(NULL, (void*)rq, NULL,rq->client->last_io);
|
2018-09-19 15:08:49 +02:00
|
|
|
//lua_close(L);
|
|
|
|
}
|
2018-10-05 19:02:35 +02:00
|
|
|
void destroy()
|
2018-09-19 15:08:49 +02:00
|
|
|
{
|
2020-01-01 21:17:46 +01:00
|
|
|
if(core_handle)
|
|
|
|
dlclose(core_handle);
|
|
|
|
LOG("Exit LUA Handle");
|
2018-09-19 15:08:49 +02:00
|
|
|
}
|