From ce152b978d489ac32c27c0877bda794107942bba Mon Sep 17 00:00:00 2001 From: eeff <33566201+eeff@users.noreply.github.com> Date: Wed, 1 Jun 2022 14:19:18 +0800 Subject: [PATCH] feat(restful): add get version api --- CMakeLists.txt | 1 + plugins/restful/handle.c | 10 +++++++ plugins/restful/version_handle.c | 47 ++++++++++++++++++++++++++++++++ plugins/restful/version_handle.h | 27 ++++++++++++++++++ 4 files changed, 85 insertions(+) create mode 100644 plugins/restful/version_handle.c create mode 100644 plugins/restful/version_handle.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 7193c69d..b26c417b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -124,6 +124,7 @@ set(NEURON_SOURCES plugins/restful/datatag_handle.c plugins/restful/group_config_handle.c plugins/restful/plugin_handle.c + plugins/restful/version_handle.c plugins/restful/rest.c plugins/restful/http.c plugins/dummy/dummy.c diff --git a/plugins/restful/handle.c b/plugins/restful/handle.c index bbd00552..b8146bda 100644 --- a/plugins/restful/handle.c +++ b/plugins/restful/handle.c @@ -36,6 +36,7 @@ #include "normal_handle.h" #include "plugin_handle.h" #include "rw_handle.h" +#include "version_handle.h" #include "handle.h" @@ -105,6 +106,9 @@ struct neu_rest_handler cors_handler[] = { { .url = "/api/v2/license", }, + { + .url = "/api/v2/version", + }, }; struct neu_rest_handler api_handlers[] = { @@ -284,6 +288,12 @@ struct neu_rest_handler api_handlers[] = { .url = "/api/v2/license", .value.handler = handle_get_license, }, + { + .method = NEU_REST_METHOD_GET, + .type = NEU_REST_HANDLER_FUNCTION, + .url = "/api/v2/version", + .value.handler = handle_get_version, + }, }; void neu_rest_web_handler(const struct neu_rest_handler **handlers, diff --git a/plugins/restful/version_handle.c b/plugins/restful/version_handle.c new file mode 100644 index 00000000..9e70432f --- /dev/null +++ b/plugins/restful/version_handle.c @@ -0,0 +1,47 @@ +/** + * NEURON IIoT System for Industry 4.0 + * Copyright (C) 2020-2022 EMQ Technologies Co., Ltd All rights reserved. + * + * This program 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 3 of the License, or (at your option) any later version. + * + * This program 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 program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + **/ + +#include "neuron/version.h" +#include "utils/log.h" +#include "json/neu_json_error.h" +#include "json/neu_json_fn.h" + +#include "handle.h" +#include "http.h" + +void handle_get_version(nng_aio *aio) +{ + char result[128] = { 0 }; + + VALIDATE_JWT(aio); + + int ret = snprintf( + result, sizeof(result), + "{\"version\":\"%s\", \"revision\":\"%s\", \"build_date\":\"%s\"}", + NEURON_VERSION, NEURON_GIT_REV NEURON_GIT_DIFF, NEURON_BUILD_DATE); + + if ((size_t) ret >= sizeof(result)) { + NEU_JSON_RESPONSE_ERROR(NEU_ERR_EINTERNAL, { + http_response(aio, error_code.error, result_error); + }); + return; + } + + http_response(aio, 0, result); +} diff --git a/plugins/restful/version_handle.h b/plugins/restful/version_handle.h new file mode 100644 index 00000000..c74dcb87 --- /dev/null +++ b/plugins/restful/version_handle.h @@ -0,0 +1,27 @@ +/** + * NEURON IIoT System for Industry 4.0 + * Copyright (C) 2020-2022 EMQ Technologies Co., Ltd All rights reserved. + * + * This program 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 3 of the License, or (at your option) any later version. + * + * This program 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 program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + **/ + +#ifndef _NEU_VERSION_HANDLE_H_ +#define _NEU_VERSION_HANDLE_H_ + +#include + +void handle_get_version(nng_aio *aio); + +#endif