domo-io WIP: add nats broker. and setup the rest handler.
This commit is contained in:
137
src/main.c
137
src/main.c
@@ -1,83 +1,53 @@
|
||||
/*!
|
||||
* main.c
|
||||
*
|
||||
* Copyright (c) 2015-2019, NADAL Jean-Baptiste. All rights reserved.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301 USA
|
||||
*
|
||||
* @Author: NADAL Jean-Baptiste
|
||||
* @Date: 08/11/2019
|
||||
*
|
||||
*/
|
||||
* main.c
|
||||
*
|
||||
* Copyright (c) 2015-2019, NADAL Jean-Baptiste. All rights reserved.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301 USA
|
||||
*
|
||||
* @Author: NADAL Jean-Baptiste
|
||||
* @Date: 08/11/2019
|
||||
*
|
||||
*/
|
||||
|
||||
// This is an independent project of an individual developer. Dear PVS-Studio, please check it.
|
||||
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com
|
||||
|
||||
/*-------------------------------- INCLUDES ---------------------------------*/
|
||||
|
||||
#include <getopt.h>
|
||||
#include <event2/event.h>
|
||||
|
||||
#include <restd.h>
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
int my_http_get_handler(short event, restd_conn_t *conn, void *userdata)
|
||||
{
|
||||
if (event & RESTD_EVENT_READ)
|
||||
{
|
||||
if (restd_http_get_status(conn) == RESTD_HTTP_REQ_DONE)
|
||||
{
|
||||
restd_http_response(conn, 200, "application/json", "{\"status\": false}", 17);
|
||||
return restd_http_is_keepalive_request(conn) ? RESTD_DONE : RESTD_CLOSE;
|
||||
}
|
||||
}
|
||||
return RESTD_OK;
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
int my_error_handler(short event, restd_conn_t *conn, void *userdata)
|
||||
{
|
||||
int http_code = 500;
|
||||
if (event & RESTD_ERROR_METHOD_NOT_ALLOWED)
|
||||
{
|
||||
http_code = 405;
|
||||
}
|
||||
else if (event & RESTD_ERROR_PATH_NOT_FOUND)
|
||||
{
|
||||
http_code = 404;
|
||||
}
|
||||
|
||||
restd_http_response(conn, http_code, "application/json", "{\"status\":\"error\"}", 18);
|
||||
return RESTD_CLOSE; // Close connection.
|
||||
}
|
||||
#include "rest/rest_handler.h"
|
||||
#include "broker/nats_broker.h"
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
void usage(void)
|
||||
{
|
||||
printf("Usage : domo-iot [web server path] \n");
|
||||
printf("web server path: \t root path of the Web server.\n");
|
||||
printf("Usage : domo-iot [web server path] \n");
|
||||
printf("web server path: \t root path of the Web server.\n");
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
restd_server_t *server;
|
||||
struct event_base *ev_base;
|
||||
restd_server_t *rest_server;
|
||||
nats_broker_t *nats_broker;
|
||||
|
||||
if (argc < 2)
|
||||
{
|
||||
@@ -86,17 +56,42 @@ int main(int argc, char **argv)
|
||||
}
|
||||
|
||||
restd_log_level(RESTD_LOG_DEBUG);
|
||||
server = restd_server_new();
|
||||
restd_server_set_option(server, "server.port", "8888");
|
||||
restd_server_set_option(server, "server.root_path", argv[1]);
|
||||
|
||||
restd_server_register_request_handler(server, restd_rest_handler);
|
||||
restd_server_register_error_handler(server, my_error_handler);
|
||||
/* event loop */
|
||||
ev_base = event_base_new();
|
||||
if (!ev_base)
|
||||
{
|
||||
fprintf(stderr, "Failed to create a new event base.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
restd_server_register_hook(server, restd_http_handler, NULL);
|
||||
restd_server_register_hook_on_path(server, "GET", "/api/zob", my_http_get_handler, NULL);
|
||||
restd_server_register_hook_on_path(server, "GET", "/api/zob2", my_http_get_handler, NULL);
|
||||
restd_server_register_hook_on_path(server, "PUT", "/api/zob/25", my_http_get_handler, NULL);
|
||||
/* Create the rest Server. */
|
||||
rest_server = restd_server_new();
|
||||
if (!rest_server)
|
||||
{
|
||||
fprintf(stderr, "Failed to create a rest server.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return restd_server_start(server);
|
||||
restd_server_attach_event_loop(rest_server, ev_base);
|
||||
|
||||
if (setup_rest_server(rest_server, "8888", argv[1]) != 0)
|
||||
{
|
||||
fprintf(stderr, "Failed to setup rest Server\n.");
|
||||
restd_server_free(rest_server);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* create and setup the nats broker. */
|
||||
nats_broker = nats_broker_new();
|
||||
if (!nats_broker)
|
||||
{
|
||||
fprintf(stderr, "Failed to create a nats broker.\n");
|
||||
return -1;
|
||||
}
|
||||
nats_broker_setup(nats_broker, ev_base);
|
||||
nats_broker_connect(nats_broker, "foo");
|
||||
|
||||
/* start the rest server */
|
||||
return restd_server_start(rest_server);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user