static int handle_request(struct mg_connection *conn) { static const char *PONG_STR = "{\"version\" : \"brubeck %s\", \"pid\" : %d, \"status\" : \"%s\"}\n"; if (!strcmp(conn->request_method, "GET")) { if (!strcmp(conn->uri, "/ping")) { struct brubeck_server *brubeck = conn->server_param; const char *status = "OK"; if (brubeck->at_capacity) status = "CAPACITY"; if (!brubeck->running) status = "SHUTDOWN"; send_headers(conn); mg_printf_data(conn, PONG_STR, GIT_SHA, getpid(), status); return MG_TRUE; } if (!strcmp(conn->uri, "/stats")) return send_stats(conn); if (starts_with(conn->uri, "/metric/")) return send_metric(conn); } if (!strcmp(conn->request_method, "POST")) { if (starts_with(conn->uri, "/expire/")) return expire_metric(conn); } return MG_FALSE; }
static int handle_request(void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls) { int ret; struct MHD_Response *response = NULL; struct brubeck_server *brubeck = cls; if (!strcmp(method, "GET")) { if (!strcmp(url, "/ping")) { char *jsonr; json_t *pong = json_pack("{s:s, s:i, s:s}", "version", "brubeck " GIT_SHA, "pid", (int)getpid(), "status", "OK"); jsonr = json_dumps(pong, JSON_PRESERVE_ORDER); response = MHD_create_response_from_data(strlen(jsonr), jsonr, 1, 0); json_decref(pong); } else if (!strcmp(url, "/stats")) response = send_stats(brubeck); else if (starts_with(url, "/metric/")) response = send_metric(brubeck, url); } else if (!strcmp(method, "POST")) { if (starts_with(url, "/expire/")) response = expire_metric(brubeck, url); } if (!response) { static const char *NOT_FOUND = "404 not found"; response = MHD_create_response_from_data( strlen(NOT_FOUND), (void *)NOT_FOUND, 0, 0); MHD_add_response_header(response, "Connection", "close"); ret = MHD_queue_response(connection, 404, response); } else { MHD_add_response_header(response, "Connection", "close"); MHD_add_response_header(response, "Content-Type", "application/json"); ret = MHD_queue_response(connection, 200, response); } MHD_destroy_response(response); return ret; }