|
| 1 | +/*** |
| 2 | +Server-side SDK for LaunchDarkly Redis store. |
| 3 | +@module launchdarkly-server-sdk-redis |
| 4 | +*/ |
| 5 | + |
| 6 | +#include <lua.h> |
| 7 | +#include <lauxlib.h> |
| 8 | +#include <stdlib.h> |
| 9 | +#include <stdbool.h> |
| 10 | +#include <string.h> |
| 11 | + |
| 12 | +#include <launchdarkly/api.h> |
| 13 | +#include <launchdarkly/store/redis.h> |
| 14 | + |
| 15 | +/*** |
| 16 | +Initialize a store backend |
| 17 | +@function makeStore |
| 18 | +@tparam table fields list of configuration options |
| 19 | +@tparam[opt] string fields.host Hostname for Redis. |
| 20 | +@tparam[opt] int fields.port Port for Redis. |
| 21 | +@tparam[opt] string fields.prefix Redis key prefix for SDK values. |
| 22 | +@tparam[opt] int fields.poolSize Number of Redis connections to maintain. |
| 23 | +@return A fresh Redis store backend. |
| 24 | +*/ |
| 25 | +static int |
| 26 | +LuaLDRedisMakeStore(lua_State *const l) |
| 27 | +{ |
| 28 | + struct LDRedisConfig *config; |
| 29 | + struct LDStoreInterface *storeInterface; |
| 30 | + |
| 31 | + if (lua_gettop(l) != 1) { |
| 32 | + return luaL_error(l, "expecting exactly 1 argument"); |
| 33 | + } |
| 34 | + |
| 35 | + luaL_checktype(l, 1, LUA_TTABLE); |
| 36 | + |
| 37 | + config = LDRedisConfigNew(); |
| 38 | + |
| 39 | + lua_getfield(l, 1, "host"); |
| 40 | + |
| 41 | + if (lua_isstring(l, -1)) { |
| 42 | + LDRedisConfigSetHost(config, luaL_checkstring(l, -1)); |
| 43 | + } |
| 44 | + |
| 45 | + lua_getfield(l, 1, "prefix"); |
| 46 | + |
| 47 | + if (lua_isstring(l, -1)) { |
| 48 | + LDRedisConfigSetPrefix(config, luaL_checkstring(l, -1)); |
| 49 | + } |
| 50 | + |
| 51 | + lua_getfield(l, 1, "port"); |
| 52 | + |
| 53 | + if (lua_isnumber(l, -1)) { |
| 54 | + LDRedisConfigSetPort(config, luaL_checkinteger(l, -1)); |
| 55 | + } |
| 56 | + |
| 57 | + lua_getfield(l, 1, "poolSize"); |
| 58 | + |
| 59 | + if (lua_isnumber(l, -1)) { |
| 60 | + LDRedisConfigSetPoolSize(config, luaL_checkinteger(l, -1)); |
| 61 | + } |
| 62 | + |
| 63 | + storeInterface = LDStoreInterfaceRedisNew(config); |
| 64 | + |
| 65 | + struct LDStoreInterface **i = |
| 66 | + (struct LDStoreInterface **)lua_newuserdata(l, sizeof(storeInterface)); |
| 67 | + |
| 68 | + *i = storeInterface; |
| 69 | + |
| 70 | + luaL_getmetatable(l, "LaunchDarklyStoreInterface"); |
| 71 | + lua_setmetatable(l, -2); |
| 72 | + |
| 73 | + return 1; |
| 74 | +} |
| 75 | + |
| 76 | +static const struct luaL_Reg launchdarkly_functions[] = { |
| 77 | + { "makeStore", LuaLDRedisMakeStore }, |
| 78 | + { NULL, NULL } |
| 79 | +}; |
| 80 | + |
| 81 | +#if !defined LUA_VERSION_NUM || LUA_VERSION_NUM==501 |
| 82 | +/* |
| 83 | +** Adapted from Lua 5.2.0 |
| 84 | +*/ |
| 85 | +static void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) { |
| 86 | + luaL_checkstack(L, nup+1, "too many upvalues"); |
| 87 | + for (; l->name != NULL; l++) { /* fill the table with given functions */ |
| 88 | + int i; |
| 89 | + lua_pushstring(L, l->name); |
| 90 | + for (i = 0; i < nup; i++) /* copy upvalues to the top */ |
| 91 | + lua_pushvalue(L, -(nup+1)); |
| 92 | + lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */ |
| 93 | + lua_settable(L, -(nup + 3)); |
| 94 | + } |
| 95 | + lua_pop(L, nup); /* remove upvalues */ |
| 96 | +} |
| 97 | +#endif |
| 98 | + |
| 99 | +int |
| 100 | +luaopen_launchdarkly_server_sdk_redis(lua_State *const l) |
| 101 | +{ |
| 102 | + #if LUA_VERSION_NUM == 503 || LUA_VERSION_NUM == 502 |
| 103 | + luaL_newlib(l, launchdarkly_functions); |
| 104 | + #else |
| 105 | + luaL_register(l, "launchdarkly-server-sdk-redis", |
| 106 | + launchdarkly_functions); |
| 107 | + #endif |
| 108 | + |
| 109 | + return 1; |
| 110 | +} |
0 commit comments