domo-io WIP: add nats broker. and setup the rest handler.
This commit is contained in:
64
src/rest/rest_handler.c
Normal file
64
src/rest/rest_handler.c
Normal file
@@ -0,0 +1,64 @@
|
||||
/*!
|
||||
* rest_handler.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: 26/12/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 "rest_handler.h"
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
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.
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
int setup_rest_server(restd_server_t *rest_server, const char *port, const char *root_path)
|
||||
{
|
||||
restd_server_set_option(rest_server, "server.port", port);
|
||||
restd_server_set_option(rest_server, "server.root_path",root_path);
|
||||
|
||||
restd_server_register_request_handler(rest_server, restd_rest_handler);
|
||||
restd_server_register_error_handler(rest_server, my_error_handler);
|
||||
|
||||
restd_server_register_hook(rest_server, restd_http_handler, NULL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
35
src/rest/rest_handler.h
Normal file
35
src/rest/rest_handler.h
Normal file
@@ -0,0 +1,35 @@
|
||||
/*!
|
||||
* rest_handler.h
|
||||
*
|
||||
* 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: 26/12/2019
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef _REST_HANDLER_H
|
||||
#define _REST_HANDLER_H
|
||||
|
||||
/*------------------------------- INCLUDES ----------------------------------*/
|
||||
|
||||
#include <restd.h>
|
||||
|
||||
extern int setup_rest_server(restd_server_t *rest_server, const char *port, const char *root_path);
|
||||
|
||||
#endif /*_REST_HANDLER_H */
|
||||
Reference in New Issue
Block a user