Browse Source

[Minor] Add function to load redis server config in C using lua_redis

tags/1.8.3
Vsevolod Stakhov 5 years ago
parent
commit
ee8a7f23d9
2 changed files with 71 additions and 0 deletions
  1. 61
    0
      src/lua/lua_common.c
  2. 10
    0
      src/lua/lua_common.h

+ 61
- 0
src/lua/lua_common.c View File

@@ -2343,3 +2343,64 @@ rspamd_lua_require_function (lua_State *L, const gchar *modname,

return FALSE;
}


gboolean
rspamd_lua_try_load_redis (lua_State *L, const ucl_object_t *obj,
struct rspamd_config *cfg, gint *ref_id)
{
gint res_pos, err_idx;
struct rspamd_config **pcfg;

/* Create results table */
lua_createtable (L, 0, 0);
res_pos = lua_gettop (L);
lua_pushcfunction (L, &rspamd_lua_traceback);
err_idx = lua_gettop (L);

/* Obtain function */
if (!rspamd_lua_require_function (L, "lua_redis", "try_load_redis_servers")) {
msg_err_config ("cannot require lua_redis");
lua_pop (L, 2);

return FALSE;
}

/* Function arguments */
ucl_object_push_lua (L, obj, true);
pcfg = lua_newuserdata (L, sizeof (*pcfg));
rspamd_lua_setclass (L, "rspamd{config}", -1);
*pcfg = cfg;
lua_pushvalue (L, res_pos);

if (lua_pcall (L, 0, 1, err_idx) != 0) {
GString *tb;

tb = lua_touserdata (L, -1);
msg_err_config ("cannot call lua try_load_redis_servers script: %s", tb->str);
g_string_free (tb, TRUE);
lua_settop (L, 0);

return FALSE;
}

if (lua_toboolean (L, -1)) {
if (ref_id) {
/* Ref table */
lua_pushvalue (L, res_pos);
*ref_id = luaL_ref (L, LUA_REGISTRYINDEX);
lua_settop (L, 0);
}
else {
/* Leave it on the stack */
lua_settop (L, res_pos);
}

return TRUE;
}
else {
lua_settop (L, 0);
}

return FALSE;
}

+ 10
- 0
src/lua/lua_common.h View File

@@ -423,6 +423,16 @@ void rspamd_lua_add_ref_dtor (lua_State *L, rspamd_mempool_t *pool,
gboolean rspamd_lua_require_function (lua_State *L, const gchar *modname,
const gchar *funcname);

/**
* Tries to load redis server definition from ucl object specified
* @param L
* @param obj
* @param cfg
* @return
*/
gboolean rspamd_lua_try_load_redis (lua_State *L, const ucl_object_t *obj,
struct rspamd_config *cfg, gint *ref_id);

/* Paths defs */
#define RSPAMD_CONFDIR_INDEX "CONFDIR"
#define RSPAMD_LOCAL_CONFDIR_INDEX "LOCAL_CONFDIR"

Loading…
Cancel
Save