diff options
Diffstat (limited to 'cethcan')
-rw-r--r-- | cethcan/Makefile | 2 | ||||
-rw-r--r-- | cethcan/cethcan.h | 1 | ||||
-rw-r--r-- | cethcan/http.c | 32 | ||||
-rw-r--r-- | cethcan/main.c | 2 |
4 files changed, 36 insertions, 1 deletions
diff --git a/cethcan/Makefile b/cethcan/Makefile index f0d5d4b..633c2f6 100644 --- a/cethcan/Makefile +++ b/cethcan/Makefile @@ -3,7 +3,7 @@ love: cethcan PKGS="libevent jansson" -cethcan: main.o can.o ether.o light.o +cethcan: main.o can.o ether.o light.o http.o gcc -g -o $@ `pkg-config --libs $(PKGS)` $^ clean: diff --git a/cethcan/cethcan.h b/cethcan/cethcan.h index 292b5c9..aa03d8b 100644 --- a/cethcan/cethcan.h +++ b/cethcan/cethcan.h @@ -69,5 +69,6 @@ extern void can_init(void); extern int ether_init(json_t *config); extern int light_init_conf(json_t *config); +extern void http_init(void); #endif /* _CETHCAN_H */ diff --git a/cethcan/http.c b/cethcan/http.c new file mode 100644 index 0000000..907ec08 --- /dev/null +++ b/cethcan/http.c @@ -0,0 +1,32 @@ +#include "cethcan.h" + +#include <event2/http.h> +#include <event2/buffer.h> + +static struct evhttp *evhttp; + +static int evb_json_add(const char *data, size_t size, void *arg) +{ + struct evbuffer *buf = arg; + return evbuffer_add(buf, data, size); +} + +static void http_json_basic(struct evhttp_request *req, void *arg) +{ + struct evkeyvalq *outhdr = evhttp_request_get_output_headers(req); + struct evbuffer *out = evbuffer_new(); + + evhttp_add_header(outhdr, "Content-Type", "text/plain; charset=utf-8"); + + json_t *jsout = json_object(); + json_dump_callback(jsout, evb_json_add, out, JSON_SORT_KEYS | JSON_INDENT(4)); + evhttp_send_reply(req, 200, "OK", out); + evbuffer_free(out); +} + +void http_init(void) +{ + evhttp = evhttp_new(ev_base); + evhttp_set_cb(evhttp, "/", http_json_basic, NULL); + evhttp_bind_socket(evhttp, "127.0.0.1", 34999); +} diff --git a/cethcan/main.c b/cethcan/main.c index 0794254..d6c5bbf 100644 --- a/cethcan/main.c +++ b/cethcan/main.c @@ -54,6 +54,8 @@ int main(int argc, char **argv) return 1; } + http_init(); + event_base_loop(ev_base, 0); return 0; } |