From 695d9abd8397b97f0a30b4dc5ab5c9c700a1268f Mon Sep 17 00:00:00 2001 From: simon987 Date: Tue, 21 Apr 2020 15:52:35 -0400 Subject: [PATCH] revert debug hard-coded listen address --- README.md | 2 +- src/cli.c | 14 ++++---------- src/cli.h | 3 +-- src/index/web.c | 8 +++++--- src/main.c | 5 ++--- src/web/serve.c | 15 +++++++-------- src/web/serve.h | 2 +- 7 files changed, 21 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index ead08fd..4f08971 100644 --- a/README.md +++ b/README.md @@ -119,7 +119,7 @@ binaries (GCC 7+ required). 1. Install compile-time dependencies ```bash - vcpkg install lmdb cjson glib libarchive[core,bzip2,libxml2,lz4,lzma,lzo] pthread tesseract libxml2 ffmpeg zstd + vcpkg install lmdb cjson glib libarchive[core,bzip2,libxml2,lz4,lzma,lzo] pthread tesseract libxml2 ffmpeg zstd gtest mongoose ``` 2. Build diff --git a/src/cli.c b/src/cli.c index ba73e47..b86ded4 100644 --- a/src/cli.c +++ b/src/cli.c @@ -11,8 +11,7 @@ #define DEFAULT_ES_URL "http://localhost:9200" #define DEFAULT_BATCH_SIZE 100 -#define DEFAULT_BIND_ADDR "localhost" -#define DEFAULT_PORT "4090" +#define DEFAULT_LISTEN_ADDRESS "localhost:4090" const char* TESS_DATAPATHS[] = { "/usr/share/tessdata/", @@ -276,12 +275,8 @@ int web_args_validate(web_args_t *args, int argc, const char **argv) { args->es_url = DEFAULT_ES_URL; } - if (args->bind == NULL) { - args->bind = DEFAULT_BIND_ADDR; - } - - if (args->port == NULL) { - args->port = DEFAULT_PORT; + if (args->listen_address == NULL) { + args->listen_address = DEFAULT_LISTEN_ADDRESS; } if (args->credentials != NULL) { @@ -316,8 +311,7 @@ int web_args_validate(web_args_t *args, int argc, const char **argv) { } LOG_DEBUGF("cli.c", "arg es_url=%s", args->es_url) - LOG_DEBUGF("cli.c", "arg bind=%s", args->bind) - LOG_DEBUGF("cli.c", "arg port=%s", args->port) + LOG_DEBUGF("cli.c", "arg listen=%s", args->listen_address) LOG_DEBUGF("cli.c", "arg credentials=%s", args->credentials) LOG_DEBUGF("cli.c", "arg auth_user=%s", args->auth_user) LOG_DEBUGF("cli.c", "arg auth_pass=%s", args->auth_pass) diff --git a/src/cli.h b/src/cli.h index 343c9ab..f7c9fb5 100644 --- a/src/cli.h +++ b/src/cli.h @@ -42,8 +42,7 @@ typedef struct index_args { typedef struct web_args { char *es_url; - char *bind; - char *port; + char *listen_address; char *credentials; char auth_user[256]; char auth_pass[256]; diff --git a/src/index/web.c b/src/index/web.c index d651213..8eaa16a 100644 --- a/src/index/web.c +++ b/src/index/web.c @@ -7,7 +7,9 @@ void free_response(response_t *resp) { - free(resp->body); + if (resp->body != NULL) { + free(resp->body); + } free(resp); } @@ -23,7 +25,7 @@ void http_req_ev(struct mg_connection *nc, int ev, void *ptr) { int connect_status = *(int *) ptr; if (connect_status != 0) { ev_data->done = TRUE; - //TODO: set error + ev_data->resp->status_code = 0; } break; } @@ -79,7 +81,7 @@ subreq_ctx_t *http_req(const char *url, const char *extra_headers, const char *p nc->user_data = &ctx->ev_data; mg_set_protocol_http_websocket(nc); - ctx->ev_data.resp = malloc(sizeof(response_t)); + ctx->ev_data.resp = calloc(1, sizeof(response_t)); ctx->ev_data.done = FALSE; mg_printf( diff --git a/src/main.c b/src/main.c index e877bf2..16795d5 100644 --- a/src/main.c +++ b/src/main.c @@ -302,7 +302,7 @@ void sist2_web(web_args_t *args) { free(abs_path); } - serve(args->bind, args->port); + serve(args->listen_address); } @@ -356,8 +356,7 @@ int main(int argc, const char *argv[]) { OPT_GROUP("Web options"), OPT_STRING(0, "es-url", &common_es_url, "Elasticsearch url. DEFAULT=http://localhost:9200"), - OPT_STRING(0, "bind", &web_args->bind, "Listen on this address. DEFAULT=localhost"), - OPT_STRING(0, "port", &web_args->port, "Listen on this port. DEFAULT=4090"), + OPT_STRING(0, "bind", &web_args->listen_address, "Listen on this address. DEFAULT=localhost:4090"), OPT_STRING(0, "auth", &web_args->credentials, "Basic auth in user:password format"), OPT_END(), diff --git a/src/web/serve.c b/src/web/serve.c index bac4593..6ee6d9c 100644 --- a/src/web/serve.c +++ b/src/web/serve.c @@ -407,8 +407,10 @@ static void ev_router(struct mg_connection *nc, int ev, void *p) { if (r->status_code == 200) { send_response_line(nc, 200, r->size, "Content-Type: application/json"); mg_send(nc, r->body, r->size); + } else if (r->status_code == 0) { + sist_log("serve.c", SIST_ERROR, "Could not connect to elasticsearch!"); } else { - sist_log("serve.c", SIST_WARNING, "ElasticSearch error during query"); + sist_logf("serve.c", SIST_WARNING, "ElasticSearch error during query (%d)", r->status_code); if (r->size != 0) { char *tmp = malloc(r->size + 1); memcpy(tmp, r->body, r->size); @@ -430,23 +432,20 @@ static void ev_router(struct mg_connection *nc, int ev, void *p) { } } -void serve(const char *hostname, const char *port) { +void serve(const char *listen_address) { - printf("Starting web server @ http://%s:%s\n", hostname, port); + printf("Starting web server @ http://%s\n", listen_address); struct mg_mgr mgr; mg_mgr_init(&mgr, NULL); - struct mg_connection *nc = mg_bind(&mgr, "0.0.0.0:8000", ev_router); + struct mg_connection *nc = mg_bind(&mgr, listen_address, ev_router); if (nc == NULL) { - printf("Failed to create listener\n"); - return; + LOG_FATALF("serve.c", "Couldn't bind web server on address %s", listen_address) } mg_set_protocol_http_websocket(nc); for (;;) { mg_mgr_poll(&mgr, 10); } - -// onion_set_root_handler(o, auth_basic(WebCtx.b64credentials, onion_url_to_handler(urls))); } diff --git a/src/web/serve.h b/src/web/serve.h index d1d30ff..d869eab 100644 --- a/src/web/serve.h +++ b/src/web/serve.h @@ -3,6 +3,6 @@ #include "src/sist.h" -void serve(const char *hostname, const char *port); +void serve(const char *listen_address); #endif