Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加redis支持密码授权和选择数据库索引配置功能 #215

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions bin/conf/zimg.lua
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ beansdb_port = 7900
ssdb_ip = '127.0.0.1'
--SSDB服务器端口
ssdb_port = 8888
--授权密码
ssdb_passwd = ''
--数据库索引
ssdb_index = 0

--lua conf functions
--部分与配置有关的函数在lua中实现,对性能影响不大
Expand Down
75 changes: 65 additions & 10 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ static int load_conf(const char *conf);
static void sighandler(int signal, siginfo_t *siginfo, void *arg);
void init_thread(evhtp_t *htp, evthr_t *thread, void *arg);
int main(int argc, char **argv);
redisContext * createRedisContext();

evbase_t *evbase;

Expand All @@ -69,6 +70,42 @@ extern const struct luaL_Reg loglib[];
const char *conf_file = NULL;
pthread_key_t gLuaStateKey = 0;

/**
* @brief support redis auth and select index
*/

redisContext * createRedisContext(){
redisContext* c = redisConnect(settings.ssdb_ip, settings.ssdb_port);
if (c->err) {
redisFree(c);
LOG_PRINT(LOG_DEBUG, "Connect to ssdb server faile");
} else {
LOG_PRINT(LOG_DEBUG, "Connect to ssdb server Success");
if (strcmp(settings.ssdb_passwd,"") > 0) {
redisReply *reply;
reply = redisCommand(c, "AUTH %s", settings.ssdb_passwd);
if (reply->type == REDIS_REPLY_ERROR) {
LOG_PRINT(LOG_ERROR, "ssdb auth faile");
}
else {
LOG_PRINT(LOG_DEBUG, "ssdb auth Success");
if (settings.ssdb_index > 0) {
reply = redisCommand(c, "SELECT %d", settings.ssdb_index);
if (reply->type == REDIS_REPLY_ERROR) {
LOG_PRINT(LOG_DEBUG, "ssdb select faile");
}
else {
LOG_PRINT(LOG_DEBUG, "ssdb select %d",settings.ssdb_index);
}
}
}
}
}
return c;
}



/**
* @brief close lua_State in thread local storage.
*/
Expand Down Expand Up @@ -132,6 +169,8 @@ static void settings_init(void) {
settings.beansdb_port = 7905;
str_lcpy(settings.ssdb_ip, "127.0.0.1", sizeof(settings.ssdb_ip));
settings.ssdb_port = 6379;
memset(settings.ssdb_passwd,'\0',sizeof(settings.ssdb_passwd));
settings.ssdb_index=0;
multipart_parser_settings *callbacks = (multipart_parser_settings *)malloc(sizeof(multipart_parser_settings));
memset(callbacks, 0, sizeof(multipart_parser_settings));
//callbacks->on_header_field = on_header_field;
Expand Down Expand Up @@ -349,6 +388,16 @@ static int load_conf(const char *conf) {
settings.ssdb_port = (int)lua_tonumber(L, -1);
lua_pop(L, 1);

lua_getglobal(L, "ssdb_passwd");
if (lua_isstring(L, -1))
str_lcpy(settings.ssdb_passwd, lua_tostring(L, -1), sizeof(settings.ssdb_passwd));
lua_pop(L, 1);

lua_getglobal(L, "ssdb_index");
if (lua_isnumber(L, -1))
settings.ssdb_index = (int)lua_tonumber(L, -1);
lua_pop(L, 1);

//settings.L = L;
lua_close(L);

Expand Down Expand Up @@ -419,7 +468,8 @@ void init_thread(evhtp_t *htp, evthr_t *thread, void *arg) {
memcached_server_list_free(servers);
} else if (settings.mode == 3) {
thr_args->beansdb_conn = NULL;
redisContext* c = redisConnect(settings.ssdb_ip, settings.ssdb_port);
redisContext* c = createRedisContext();
//redisContext* c = redisConnect(settings.ssdb_ip, settings.ssdb_port);
if (c->err) {
redisFree(c);
LOG_PRINT(LOG_DEBUG, "Connect to ssdb server faile");
Expand Down Expand Up @@ -565,15 +615,20 @@ int main(int argc, char **argv) {
}
memcached_free(beans);
} else if (settings.mode == 3) {
redisContext* c = redisConnect(settings.ssdb_ip, settings.ssdb_port);
if (c->err) {
redisFree(c);
LOG_PRINT(LOG_DEBUG, "Connect to ssdb server faile");
fprintf(stderr, "SSDB[%s:%d] Connect Failed!\n", settings.ssdb_ip, settings.ssdb_port);
return -1;
} else {
LOG_PRINT(LOG_DEBUG, "Connect to ssdb server Success");
}

redisContext* c = createRedisContext();
if (c == NULL || c->err) {
return -1;
}
// redisContext* c = redisConnect(settings.ssdb_ip, settings.ssdb_port);
// if (c->err) {
// redisFree(c);
// LOG_PRINT(LOG_DEBUG, "Connect to ssdb server faile");
// fprintf(stderr, "SSDB[%s:%d] Connect Failed!\n", settings.ssdb_ip, settings.ssdb_port);
// return -1;
// } else {
// LOG_PRINT(LOG_DEBUG, "Connect to ssdb server Success");
// }
}

//init magickwand
Expand Down
2 changes: 2 additions & 0 deletions src/zcommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ struct setting {
int beansdb_port;
char ssdb_ip[128];
int ssdb_port;
char ssdb_passwd[256];
int ssdb_index;
multipart_parser_settings *mp_set;
int (*get_img)(zimg_req_t *, evhtp_request_t *);
int (*info_img)(evhtp_request_t *, thr_arg_t *, char *);
Expand Down