Merge branch 'slog'

master
Nicolas Favre-Felix 14 years ago
commit bfadb3a136

4
.gitignore vendored

@ -0,0 +1,4 @@
*.log
*.swp
*.o
webdis

@ -2,7 +2,8 @@ OUT=webdis
HIREDIS_OBJ=hiredis/hiredis.o hiredis/sds.o hiredis/net.o hiredis/async.o hiredis/dict.o
JANSSON_OBJ=jansson/src/dump.o jansson/src/error.o jansson/src/hashtable.o jansson/src/load.o jansson/src/strbuffer.o jansson/src/utf.o jansson/src/value.o jansson/src/variadic.o
FORMAT_OBJS=formats/json.o formats/raw.o formats/common.o formats/custom-type.o
OBJS=webdis.o conf.o $(FORMAT_OBJS) cmd.o server.o $(HIREDIS_OBJ) $(JANSSON_OBJ) libb64/cencode.o acl.o md5/md5.o
OBJS=webdis.o conf.o $(FORMAT_OBJS) cmd.o slog.o server.o $(HIREDIS_OBJ) $(JANSSON_OBJ) libb64/cencode.o acl.o md5/md5.o
CFLAGS=-O3 -Wall -Wextra -I. -Ijansson/src
LDFLAGS=-levent

@ -4,9 +4,12 @@ A very simple web server providing an HTTP interface to Redis. It uses [hiredis]
<pre>
make clean all
./webdis &
curl http://127.0.0.1:7379/SET/hello/world
→ {"SET":[true,"OK"]}
curl http://127.0.0.1:7379/GET/hello
→ {"GET":"world"}

@ -32,6 +32,8 @@ conf_read(const char *filename) {
conf->http_port = 7379;
conf->user = getuid();
conf->group = getgid();
conf->logfile = "webdis.log";
conf->verbosity = WEBDIS_NOTICE;
j = json_load_file(filename, 0, &error);
if(!j) {
@ -66,6 +68,13 @@ conf_read(const char *filename) {
if((g = getgrnam(json_string_value(jtmp)))) {
conf->group = g->gr_gid;
}
} else if(strcmp(json_object_iter_key(kv),"logfile") == 0 && json_typeof(jtmp) == JSON_STRING){
conf->logfile = strdup(json_string_value(jtmp));
} else if(strcmp(json_object_iter_key(kv),"verbosity") == 0 && json_typeof(jtmp) == JSON_INTEGER){
int tmp = json_integer_value(jtmp);
if(tmp < 0) conf->verbosity = WEBDIS_ERROR;
else if(tmp > (int)WEBDIS_DEBUG) conf->verbosity = WEBDIS_DEBUG;
else conf->verbosity = (log_level)tmp;
}
}

@ -2,6 +2,7 @@
#define CONF_H
#include <sys/types.h>
#include "slog.h"
struct conf {
@ -20,6 +21,10 @@ struct conf {
/* user/group */
uid_t user;
gid_t group;
/* Logging */
char *logfile;
log_level verbosity;
};
struct conf *

@ -1,6 +1,7 @@
#include "server.h"
#include "conf.h"
#include "cmd.h"
#include "slog.h"
#include <hiredis/hiredis.h>
#include <hiredis/adapters/libevent.h>
@ -61,6 +62,7 @@ on_timer_reconnect(int fd, short event, void *ctx) {
s->ac->data = s;
if(s->ac->err) {
slog(s, WEBDIS_ERROR, "Connection failed");
fprintf(stderr, "Error: %s\n", s->ac->errstr);
}
@ -116,7 +118,6 @@ on_flash_request(struct evhttp_request *rq, void *ctx) {
void
on_request(struct evhttp_request *rq, void *ctx) {
const char *uri = evhttp_request_uri(rq);
struct server *s = ctx;
int ret;
@ -126,19 +127,23 @@ on_request(struct evhttp_request *rq, void *ctx) {
return;
}
/* check that the command can be executed */
switch(rq->type) {
case EVHTTP_REQ_GET:
slog(s, WEBDIS_DEBUG, uri);
ret = cmd_run(s, rq, 1+uri, strlen(uri)-1);
break;
case EVHTTP_REQ_POST:
slog(s, WEBDIS_DEBUG, uri);
ret = cmd_run(s, rq,
(const char*)EVBUFFER_DATA(rq->input_buffer),
EVBUFFER_LENGTH(rq->input_buffer));
break;
default:
slog(s, WEBDIS_DEBUG, "405");
evhttp_send_reply(rq, 405, "Method Not Allowed", NULL);
return;
}
@ -157,11 +162,13 @@ server_start(struct server *s) {
#endif
/* start http server */
slog(s, WEBDIS_INFO, "Starting HTTP Server");
evhttp_bind_socket(s->http, s->cfg->http_host, s->cfg->http_port);
evhttp_set_cb(s->http, "/crossdomain.xml", on_flash_request, s);
evhttp_set_gencb(s->http, on_request, s);
/* drop privileges */
slog(s, WEBDIS_INFO, "Dropping Privileges");
setuid(s->cfg->user);
setgid(s->cfg->group);
@ -171,4 +178,3 @@ server_start(struct server *s) {
/* loop */
event_base_dispatch(s->base);
}

@ -31,5 +31,8 @@ server_copy(const struct server *s);
void
server_start(struct server *s);
void
webdis_log(struct server *s, int level, const char *body);
#endif

@ -0,0 +1,34 @@
/* A slog is a simple log. Basically extracted from antirez/redis. */
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <time.h>
#include <sys/types.h>
#include <unistd.h>
#include "slog.h"
#include "server.h"
#include "conf.h"
void slog(const struct server *s, log_level level, const char *body) {
const char *c = ".-*#";
time_t now = time(NULL);
static FILE *fp = NULL;
char buf[64];
char msg[124];
static pid_t self = 0;
if(!self) self = getpid();
if(level > s->cfg->verbosity) return; /* too verbose */
if(!fp) fp = (s->cfg->logfile == NULL) ? stdout : fopen(s->cfg->logfile, "a");
if(!fp) return;
/* limit message size */
snprintf(msg, sizeof(msg), "%s", body);
strftime(buf,sizeof(buf),"%d %b %H:%M:%S",localtime(&now));
fprintf(fp,"[%d] %s %c %s\n", (int)self, buf, c[level], msg);
fflush(fp);
}

@ -0,0 +1,16 @@
#ifndef SLOG_H
#define SLOG_H
typedef enum {
WEBDIS_ERROR = 0,
WEBDIS_WARNING,
WEBDIS_NOTICE,
WEBDIS_INFO,
WEBDIS_DEBUG
} log_level;
struct server;
void slog(const struct server *s, log_level level, const char *body);
#endif

@ -16,5 +16,8 @@
"http_basic_auth": "user:password",
"enabled": ["DEBUG"]
}
]
],
"verbosity": 3,
"logfile": "webdis.log"
}

Loading…
Cancel
Save