Added simplistic config file.

master
Nicolas Favre-Felix 14 years ago
parent 5492922f37
commit 956a0e917c

@ -1,5 +1,5 @@
OUT=turnip
OBJS=turnip.o hiredis/hiredis.o hiredis/sds.o hiredis/net.o hiredis/async.o
OBJS=turnip.o conf.o hiredis/hiredis.o hiredis/sds.o hiredis/net.o hiredis/async.o
CFLAGS=-O0 -ggdb -Wall -Wextra -I.
LDFLAGS=-levent

@ -7,8 +7,11 @@ make clean all
./turnip &
curl http://127.0.0.1:7379/SET/hello/world
curl http://127.0.0.1:7379/GET/hello
→ “world”
curl -v "GET/hello" http://127.0.0.1:7379/
→ “world”
</pre>
# Ideas
@ -18,6 +21,6 @@ curl http://127.0.0.1:7379/GET/hello
* Support PUT, DELETE, HEAD?
* Add JSON output
* Add JSONP callbacks
* Add a config file
* Provide host, port, timeout
* Enrich config file
* Provide timeout
* Restrict commands by IP range

@ -0,0 +1,67 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "conf.h"
struct conf *
conf_read(const char *filename) {
struct conf *conf;
FILE *f = fopen(filename, "r");
if(!f) {
return NULL;
}
conf = calloc(1, sizeof(struct conf));
conf->redis_port = 6379;
conf->http_port = 7379;
while(!feof(f)) {
char buffer[100], *ret;
memset(buffer, 0, sizeof(buffer));
if(!(ret = fgets(buffer, sizeof(buffer)-1, f))) {
break;
}
if(*ret == '#') { /* comments */
continue;
}
if(*ret != 0) {
ret[strlen(ret)-1] = 0; /* remove new line */
}
if(strncmp(ret, "redis_host", 10) == 0) {
conf->redis_host = strdup(ret + 11);
} else if(strncmp(ret, "redis_port", 10) == 0) {
conf->redis_port = (short)atoi(ret + 10);
} else if(strncmp(ret, "http_host", 10) == 0) {
conf->http_host = strdup(ret + 11);
} else if(strncmp(ret, "http_port", 9) == 0) {
conf->http_port = (short)atoi(ret + 10);
}
}
fclose(f);
/* default values */
if(!conf->redis_host) {
conf->redis_host = strdup("127.0.0.1");
}
if(!conf->http_host) {
conf->http_host = strdup("0.0.0.0");
}
return conf;
}
void
conf_free(struct conf *conf) {
free(conf->redis_host);
free(conf->http_host);
free(conf);
}

@ -0,0 +1,20 @@
#ifndef CONF_H
#define CONF_H
struct conf {
char *redis_host;
short redis_port;
char *http_host;
short http_port;
};
struct conf *
conf_read(const char *filename);
void
conf_free(struct conf *conf);
#endif /* CONF_H */

@ -10,6 +10,8 @@
#include <hiredis/async.h>
#include <hiredis/adapters/libevent.h>
#include "conf.h"
static void
cmdCallback(redisAsyncContext *c, void *r, void *privdata) {
(void)c;
@ -152,7 +154,9 @@ main(int argc, char *argv[]) {
struct event_base *base = event_base_new();
struct evhttp *http = evhttp_new(base);
redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379);
struct conf *cfg = conf_read("turnip.conf");
redisAsyncContext *c = redisAsyncConnect(cfg->redis_host, cfg->redis_port);
if(c->err) {
/* Let *c leak for now... */
printf("Error: %s\n", c->errstr);
@ -160,7 +164,7 @@ main(int argc, char *argv[]) {
}
/* start http server */
evhttp_bind_socket(http, "0.0.0.0", 7379);
evhttp_bind_socket(http, cfg->http_host, cfg->http_port);
evhttp_set_gencb(http, on_request, c);
/* attach hiredis to libevent base */

@ -0,0 +1,5 @@
redis_host 127.0.0.1
redis_port 6379
http_host 0.0.0.0
http_port 7379
Loading…
Cancel
Save