Browse files

Refactoring

  • Loading branch information...
1 parent a7bf6f7 commit a8bccb4d40b0a5008f9a7db2dcfd12e73159d88c @nicolasff committed Jan 1, 2011
Showing with 83 additions and 69 deletions.
  1. +1 −1 cmd.h
  2. +69 −0 server.c
  3. +9 −0 server.h
  4. +4 −68 webdis.c
View
2 cmd.h
@@ -4,8 +4,8 @@
#include <stdlib.h>
#include <hiredis/async.h>
#include <sys/queue.h>
-#include <evhttp.h>
#include <event.h>
+#include <evhttp.h>
struct evhttp_request;
struct server;
View
69 server.c
@@ -6,6 +6,20 @@
#include <hiredis/adapters/libevent.h>
#include <jansson.h>
+#include <signal.h>
+#include <string.h>
+
+struct server *
+server_new(const char *filename) {
+ struct server *s = calloc(1, sizeof(struct server));
+
+ s->cfg = conf_read(filename);
+ s->base = event_base_new();
+ s->http = evhttp_new(s->base);
+
+ return s;
+}
+
static void
connectCallback(const redisAsyncContext *c) {
((void)c);
@@ -79,3 +93,58 @@ server_copy(const struct server *s) {
return ret;
}
+void
+on_request(struct evhttp_request *rq, void *ctx) {
+
+ const char *uri = evhttp_request_uri(rq);
+ struct server *s = ctx;
+ int ret;
+
+ if(!s->ac) { /* redis is unavailable */
+ printf("503\n");
+ evhttp_send_reply(rq, 503, "Service Unavailable", NULL);
+ return;
+ }
+
+ /* check that the command can be executed */
+ switch(rq->type) {
+ case EVHTTP_REQ_GET:
+ ret = cmd_run(s, rq, 1+uri, strlen(uri)-1);
+ break;
+
+ case EVHTTP_REQ_POST:
+ ret = cmd_run(s, rq,
+ (const char*)EVBUFFER_DATA(rq->input_buffer),
+ EVBUFFER_LENGTH(rq->input_buffer));
+ break;
+
+ default:
+ printf("405\n");
+ evhttp_send_reply(rq, 405, "Method Not Allowed", NULL);
+ return;
+ }
+
+ if(ret < 0) {
+ evhttp_send_reply(rq, 403, "Forbidden", NULL);
+ }
+}
+
+void
+server_start(struct server *s) {
+
+ /* ignore sigpipe */
+#ifdef SIGPIPE
+ signal(SIGPIPE, SIG_IGN);
+#endif
+
+ /* start http server */
+ evhttp_bind_socket(s->http, s->cfg->http_host, s->cfg->http_port);
+ evhttp_set_gencb(s->http, on_request, s);
+
+ /* attach hiredis to libevent base */
+ webdis_connect(s);
+
+ /* loop */
+ event_base_dispatch(s->base);
+}
+
View
9 server.h
@@ -6,11 +6,14 @@
#include <sys/queue.h>
#include <event.h>
+struct evhttp;
+
struct server {
struct conf *cfg;
struct event_base *base;
redisAsyncContext *ac;
+ struct evhttp *http;
struct event ev_reconnect;
struct timeval tv_reconnect;
@@ -20,7 +23,13 @@ void
webdis_connect(struct server *s);
struct server *
+server_new(const char *filename);
+
+struct server *
server_copy(const struct server *s);
+void
+server_start(struct server *s);
+
#endif
View
72 webdis.c
@@ -1,82 +1,18 @@
#include <stdlib.h>
-#include <stdio.h>
-#include <sys/queue.h>
-#include <evhttp.h>
-#include <event.h>
-#include <string.h>
-#include <signal.h>
-
-#include <hiredis/hiredis.h>
-#include <hiredis/async.h>
-
#include "server.h"
-#include "conf.h"
-#include "cmd.h"
-
-
-void
-on_request(struct evhttp_request *rq, void *ctx) {
-
- const char *uri = evhttp_request_uri(rq);
- struct server *s = ctx;
- int ret;
-
- if(!s->ac) { /* redis is unavailable */
- printf("503\n");
- evhttp_send_reply(rq, 503, "Service Unavailable", NULL);
- return;
- }
-
- /* check that the command can be executed */
- switch(rq->type) {
- case EVHTTP_REQ_GET:
- ret = cmd_run(s, rq, 1+uri, strlen(uri)-1);
- break;
-
- case EVHTTP_REQ_POST:
- ret = cmd_run(s, rq,
- (const char*)EVBUFFER_DATA(rq->input_buffer),
- EVBUFFER_LENGTH(rq->input_buffer));
- break;
-
- default:
- printf("405\n");
- evhttp_send_reply(rq, 405, "Method Not Allowed", NULL);
- return;
- }
-
- if(ret < 0) {
- evhttp_send_reply(rq, 403, "Forbidden", NULL);
- }
-}
int
main(int argc, char *argv[]) {
- struct server *s = calloc(1, sizeof(struct server));
- s->base = event_base_new();
- struct evhttp *http = evhttp_new(s->base);
+ struct server *s;
if(argc > 1) {
- s->cfg = conf_read(argv[1]);
+ s = server_new(argv[1]);
} else {
- s->cfg = conf_read("webdis.json");
+ s = server_new("webdis.json");
}
- /* ignore sigpipe */
-#ifdef SIGPIPE
- signal(SIGPIPE, SIG_IGN);
-#endif
-
- /* start http server */
- evhttp_bind_socket(http, s->cfg->http_host, s->cfg->http_port);
- evhttp_set_gencb(http, on_request, s);
-
- /* attach hiredis to libevent base */
- webdis_connect(s);
-
- /* loop */
- event_base_dispatch(s->base);
+ server_start(s);
return EXIT_SUCCESS;
}

0 comments on commit a8bccb4

Please sign in to comment.