Merge pull request #882 from fengzeroz/main

feat(rest): change the api port to 7000
This commit is contained in:
fengzeroz
2022-11-03 16:20:17 +08:00
committed by GitHub
6 changed files with 48 additions and 85 deletions
+1 -1
View File
@@ -1,5 +1,5 @@
*** Settings ***
Library REST http://127.0.0.1:7001
Library REST http://127.0.0.1:7000
Resource common.resource
*** Variables ***
+1 -1
View File
@@ -1,7 +1,7 @@
*** Settings ***
Library Collections
Library Keyword.MQTT.Client reconnect_retries=5 reconnect_delay=0
Library REST http://127.0.0.1:7001
Library REST http://127.0.0.1:7000
Resource common.resource
*** Keywords ***
+1 -1
View File
@@ -34,7 +34,7 @@ static nng_http_server *server_init()
{
nng_url * url = NULL;
nng_http_server *server = NULL;
const char * host_port = "http://0.0.0.0:7001";
const char * host_port = "http://0.0.0.0:7000";
nlog_info("monitor bind url: %s", host_port);
+17 -31
View File
@@ -42,15 +42,6 @@ struct neu_rest_handle_ctx {
static struct neu_rest_handle_ctx *rest_ctx = NULL;
static struct neu_rest_handler web_handlers[] = {
{
.method = NEU_REST_METHOD_GET,
.type = NEU_REST_HANDLER_DIRECTORY,
.url = "",
.value.path = "./dist",
},
};
static struct neu_rest_handler cors_handler[] = {
{
.url = "/api/v2/ping",
@@ -108,22 +99,25 @@ static struct neu_rest_handler cors_handler[] = {
},
};
static struct neu_rest_handler api_handlers[] = {
static struct neu_rest_handler rest_handlers[] = {
{
.method = NEU_REST_METHOD_GET,
.type = NEU_REST_HANDLER_DIRECTORY,
.url = "/web",
.value.path = "./dist",
},
{
.method = NEU_REST_METHOD_UNDEFINE,
.type = NEU_REST_HANDLER_REDIRECT,
.url = "/",
.value.path = "/web",
},
{
.method = NEU_REST_METHOD_UNDEFINE,
.type = NEU_REST_HANDLER_PROXY,
.url = "/api/v2/ekuiper",
.value.dst_url = "http://127.0.0.1:9081",
},
{
.type = NEU_REST_HANDLER_PROXY,
.url = "/api/v2/hwtoken",
.value.dst_url = "http://127.0.0.1:7003/api/v2/hwtoken",
},
{
.type = NEU_REST_HANDLER_PROXY,
.url = "/api/v2/license",
.value.dst_url = "http://127.0.0.1:7003/api/v2/license",
},
{
.method = NEU_REST_METHOD_POST,
.type = NEU_REST_HANDLER_FUNCTION,
@@ -308,18 +302,10 @@ static struct neu_rest_handler api_handlers[] = {
},
};
void neu_rest_web_handler(const struct neu_rest_handler **handlers,
uint32_t * size)
void neu_rest_handler(const struct neu_rest_handler **handlers, uint32_t *size)
{
*handlers = web_handlers;
*size = sizeof(web_handlers) / sizeof(struct neu_rest_handler);
}
void neu_rest_api_handler(const struct neu_rest_handler **handlers,
uint32_t * size)
{
*handlers = api_handlers;
*size = sizeof(api_handlers) / sizeof(struct neu_rest_handler);
*handlers = rest_handlers;
*size = sizeof(rest_handlers) / sizeof(struct neu_rest_handler);
}
void neu_rest_api_cors_handler(const struct neu_rest_handler **handlers,
+4 -5
View File
@@ -80,7 +80,8 @@
}
enum neu_rest_method {
NEU_REST_METHOD_GET = 0x0,
NEU_REST_METHOD_UNDEFINE = 0x0,
NEU_REST_METHOD_GET,
NEU_REST_METHOD_POST,
NEU_REST_METHOD_PUT,
NEU_REST_METHOD_DELETE,
@@ -90,6 +91,7 @@ enum neu_rest_handler_type {
NEU_REST_HANDLER_FUNCTION = 0x0,
NEU_REST_HANDLER_DIRECTORY,
NEU_REST_HANDLER_PROXY,
NEU_REST_HANDLER_REDIRECT,
};
struct neu_rest_handler {
@@ -111,10 +113,7 @@ void * neu_rest_get_plugin();
int neu_rest_add_handler(nng_http_server * server,
const struct neu_rest_handler *rest_handler);
void neu_rest_web_handler(const struct neu_rest_handler **handlers,
uint32_t * size);
void neu_rest_api_handler(const struct neu_rest_handler **handlers,
uint32_t * size);
void neu_rest_handler(const struct neu_rest_handler **handlers, uint32_t *size);
void neu_rest_api_cors_handler(const struct neu_rest_handler **handlers,
uint32_t * size);
void neu_rest_handle_cors(nng_aio *aio);
+24 -46
View File
@@ -43,8 +43,7 @@
struct neu_plugin {
neu_plugin_common_t common;
nng_http_server * api_server;
nng_http_server * web_server;
nng_http_server * server;
neu_rest_handle_ctx_t *handle_ctx;
};
@@ -66,7 +65,11 @@ int neu_rest_add_handler(nng_http_server * server,
break;
case NEU_REST_HANDLER_PROXY:
ret = rest_proxy_handler(rest_handler, &handler);
goto end;
break;
case NEU_REST_HANDLER_REDIRECT:
ret = nng_http_handler_alloc_redirect(&handler, rest_handler->url, 0,
rest_handler->value.path);
break;
}
if (ret != 0) {
@@ -94,12 +97,10 @@ int neu_rest_add_handler(nng_http_server * server,
ret = nng_http_handler_set_method(handler, "OPTIONS");
strncpy(method, "OPTIONS", sizeof(method));
break;
default:
break;
}
end:
if (ret != 0) {
return -1;
}
assert(ret == 0);
ret = nng_http_server_add_handler(server, handler);
nlog_info("rest add handler, method: %s, url: %s, ret: %d", method,
@@ -108,18 +109,14 @@ end:
return ret;
}
static nng_http_server *server_init(char *type)
static nng_http_server *server_init()
{
nng_url * url;
char host_port[24] = { 0 };
nng_http_server *server;
if (strncmp(type, "api", strlen("api")) == 0) {
snprintf(host_port, sizeof(host_port), "http://0.0.0.0:7001");
} else {
snprintf(host_port, sizeof(host_port), "http://0.0.0.0:7000");
}
nlog_info("%s bind url: %s", type, host_port);
snprintf(host_port, sizeof(host_port), "http://0.0.0.0:7000");
nlog_info("bind url: %s", host_port);
int ret = nng_url_parse(&url, host_port);
if (ret != 0) {
@@ -149,49 +146,32 @@ static neu_plugin_t *dashb_plugin_open(void)
plugin->handle_ctx = neu_rest_init_ctx(plugin);
plugin->web_server = server_init("web");
plugin->api_server = server_init("api");
plugin->server = server_init();
if (plugin->web_server == NULL || plugin->api_server == NULL) {
nlog_error("Failed to create web server and api server");
if (plugin->server == NULL) {
nlog_error("Failed to create RESTful server");
goto server_init_fail;
}
neu_rest_api_handler(&rest_handlers, &n_handler);
neu_rest_handler(&rest_handlers, &n_handler);
for (uint32_t i = 0; i < n_handler; i++) {
neu_rest_add_handler(plugin->api_server, &rest_handlers[i]);
neu_rest_add_handler(plugin->server, &rest_handlers[i]);
}
neu_rest_api_cors_handler(&cors, &n_handler);
for (uint32_t i = 0; i < n_handler; i++) {
neu_rest_add_handler(plugin->api_server, &cors[i]);
neu_rest_add_handler(plugin->server, &cors[i]);
}
if ((rv = nng_http_server_start(plugin->api_server)) != 0) {
if ((rv = nng_http_server_start(plugin->server)) != 0) {
nlog_error("Failed to start api server, error=%d", rv);
goto api_server_start_fail;
}
neu_rest_web_handler(&rest_handlers, &n_handler);
for (uint32_t i = 0; i < n_handler; i++) {
neu_rest_add_handler(plugin->web_server, &rest_handlers[i]);
}
if ((rv = nng_http_server_start(plugin->web_server)) != 0) {
nlog_error("Failed to start web server, error=%d", rv);
goto web_server_start_fail;
goto server_init_fail;
}
nlog_info("Success to create plugin: %s", neu_plugin_module.module_name);
return plugin;
web_server_start_fail:
nng_http_server_stop(plugin->api_server);
api_server_start_fail:
server_init_fail:
if (plugin->web_server != NULL) {
nng_http_server_release(plugin->web_server);
}
if (plugin->api_server != NULL) {
nng_http_server_release(plugin->api_server);
if (plugin->server != NULL) {
nng_http_server_release(plugin->server);
}
neu_rest_free_ctx(plugin->handle_ctx);
free(plugin);
@@ -204,10 +184,8 @@ static int dashb_plugin_close(neu_plugin_t *plugin)
nlog_info("Success to before free plugin: %s",
neu_plugin_module.module_name);
nng_http_server_stop(plugin->api_server);
nng_http_server_release(plugin->api_server);
nng_http_server_stop(plugin->web_server);
nng_http_server_release(plugin->web_server);
nng_http_server_stop(plugin->server);
nng_http_server_release(plugin->server);
free(plugin);
nlog_info("Success to free plugin: %s", neu_plugin_module.module_name);