Switched conf file to JSON.

master
Nicolas Favre-Felix 14 years ago
parent 3d849e57dc
commit 9833000353

@ -3,65 +3,53 @@
#include <string.h> #include <string.h>
#include <ctype.h> #include <ctype.h>
#include <jansson.h>
#include "conf.h" #include "conf.h"
static char *
skipspaces(char *p) {
while(isspace(*p)) p++;
return p;
}
struct conf * struct conf *
conf_read(const char *filename) { conf_read(const char *filename) {
json_t *j, *jtmp;
json_error_t error;
struct conf *conf; struct conf *conf;
FILE *f = fopen(filename, "r"); /* defaults */
if(!f) {
return NULL;
}
conf = calloc(1, sizeof(struct conf)); conf = calloc(1, sizeof(struct conf));
conf->redis_host = strdup("127.0.0.1");
conf->redis_port = 6379; conf->redis_port = 6379;
conf->http_host = strdup("0.0.0.0");
conf->http_port = 7379; conf->http_port = 7379;
while(!feof(f)) { j = json_load_file(filename, 0, &error);
char buffer[100], *ret; if(!j) {
memset(buffer, 0, sizeof(buffer)); fprintf(stderr, "Error: %s (line %d)\n", error.text, error.line);
if(!(ret = fgets(buffer, sizeof(buffer)-1, f))) { return conf;
break; }
}
if(*ret == '#') { /* comments */
continue;
}
if(*ret != 0) { jtmp = json_object_get(j, "redis_host");
ret[strlen(ret)-1] = 0; /* remove new line */ if(jtmp && json_typeof(jtmp) == JSON_STRING) {
} free(conf->redis_host);
conf->redis_host = strdup(json_string_value(jtmp));
}
if(strncmp(ret, "redis_host", 10) == 0) { jtmp = json_object_get(j, "redis_port");
conf->redis_host = strdup(skipspaces(ret + 11)); if(jtmp && json_typeof(jtmp) == JSON_INTEGER) {
} else if(strncmp(ret, "redis_port", 10) == 0) { conf->redis_port = json_integer_value(jtmp);
conf->redis_port = (short)atoi(skipspaces(ret + 10));
} else if(strncmp(ret, "http_host", 10) == 0) {
conf->http_host = strdup(skipspaces(ret + 11));
} else if(strncmp(ret, "http_port", 9) == 0) {
conf->http_port = (short)atoi(skipspaces(ret + 10));
}
} }
fclose(f);
/* default values */ jtmp = json_object_get(j, "http_host");
if(!conf->redis_host) { if(jtmp && json_typeof(jtmp) == JSON_STRING) {
conf->redis_host = strdup("127.0.0.1"); free(conf->http_host);
conf->http_host = strdup(json_string_value(jtmp));
} }
if(!conf->http_host) {
conf->http_host = strdup("0.0.0.0"); jtmp = json_object_get(j, "http_port");
if(jtmp && json_typeof(jtmp) == JSON_INTEGER) {
conf->http_port = json_integer_value(jtmp);
} }
json_decref(j);
return conf; return conf;
} }

@ -50,7 +50,7 @@ main(int argc, char *argv[]) {
s->base = event_base_new(); s->base = event_base_new();
struct evhttp *http = evhttp_new(s->base); struct evhttp *http = evhttp_new(s->base);
s->cfg = conf_read("turnip.conf"); s->cfg = conf_read("turnip.json");
/* ignore sigpipe */ /* ignore sigpipe */
#ifdef SIGPIPE #ifdef SIGPIPE

@ -0,0 +1,11 @@
{
"redis_host": "127.0.0.1",
"redis_port": 6379,
"http_host": "0.0.0.0",
"http_port": 7379,
"disable": {
"255.255.255.255/32": ["DEBUG", "FLUSHDB", "FLUSHALL"]
}
}
Loading…
Cancel
Save