diff --git a/plugins/restful/adapter_handle.c b/plugins/restful/adapter_handle.c index 60552507..0ee0cc54 100644 --- a/plugins/restful/adapter_handle.c +++ b/plugins/restful/adapter_handle.c @@ -5,7 +5,7 @@ * 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. + * version 3 of the License, or (at your) 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 @@ -31,6 +31,11 @@ #include "adapter_handle.h" +/** + * 处理添加适配器的HTTP请求 + * + * @param aio NNG异步I/O对象,包含HTTP请求和响应信息 + */ void handle_add_adapter(nng_aio *aio) { neu_plugin_t *plugin = neu_rest_get_plugin(); @@ -62,6 +67,13 @@ void handle_add_adapter(nng_aio *aio) }) } +/** + * 发送节点更新请求 + * + * @param aio NNG异步I/O对象,包含HTTP请求和响应信息 + * @param req 更新节点的请求数据 + * @return 错误码,0表示成功,其他表示错误 + */ static inline int send_node_update_req(nng_aio * aio, neu_json_update_node_req_t *req) { @@ -95,6 +107,11 @@ static inline int send_node_update_req(nng_aio * aio, return 0; } +/** + * 处理更新适配器的HTTP请求 + * + * @param aio NNG异步I/O对象,包含HTTP请求和响应信息 + */ void handle_update_adapter(nng_aio *aio) { NEU_PROCESS_HTTP_REQUEST_VALIDATE_JWT( @@ -107,6 +124,11 @@ void handle_update_adapter(nng_aio *aio) }) } +/** + * 处理删除适配器的HTTP请求 + * + * @param aio NNG异步I/O对象,包含HTTP请求和响应信息 + */ void handle_del_adapter(nng_aio *aio) { neu_plugin_t *plugin = neu_rest_get_plugin(); @@ -129,6 +151,11 @@ void handle_del_adapter(nng_aio *aio) }) } +/** + * 处理获取适配器的HTTP请求 + * + * @param aio NNG异步I/O对象,包含HTTP请求和响应信息 + */ void handle_get_adapter(nng_aio *aio) { int ret = 0; @@ -170,6 +197,12 @@ void handle_get_adapter(nng_aio *aio) } } +/** + * 处理获取适配器的响应 + * + * @param aio NNG异步I/O对象,包含HTTP请求和响应信息 + * @param nodes 节点信息的数组 + */ void handle_get_adapter_resp(nng_aio *aio, neu_resp_get_node_t *nodes) { neu_json_get_nodes_resp_t nodes_res = { 0 }; @@ -196,6 +229,11 @@ void handle_get_adapter_resp(nng_aio *aio, neu_resp_get_node_t *nodes) utarray_free(nodes->nodes); } +/** + * 处理设置节点配置的HTTP请求 + * + * @param aio NNG异步I/O对象,包含HTTP请求和响应信息 + */ void handle_set_node_setting(nng_aio *aio) { neu_plugin_t *plugin = neu_rest_get_plugin(); @@ -220,6 +258,11 @@ void handle_set_node_setting(nng_aio *aio) }) } +/** + * 处理获取节点配置的HTTP请求 + * + * @param aio NNG异步I/O对象,包含HTTP请求和响应信息 + */ void handle_get_node_setting(nng_aio *aio) { neu_plugin_t * plugin = neu_rest_get_plugin(); @@ -250,6 +293,12 @@ void handle_get_node_setting(nng_aio *aio) } } +/** + * 处理获取节点配置的响应 + * + * @param aio NNG异步I/O对象,包含HTTP请求和响应信息 + * @param setting 节点配置信息 + */ void handle_get_node_setting_resp(nng_aio * aio, neu_resp_get_node_setting_t *setting) { @@ -273,6 +322,11 @@ void handle_get_node_setting_resp(nng_aio * aio, free(json_str); } +/** + * 处理节点控制的HTTP请求 + * + * @param aio NNG异步I/O对象,包含HTTP请求和响应信息 + */ void handle_node_ctl(nng_aio *aio) { neu_plugin_t *plugin = neu_rest_get_plugin(); @@ -297,6 +351,11 @@ void handle_node_ctl(nng_aio *aio) }) } +/** + * 处理获取节点状态的HTTP请求 + * + * @param aio NNG异步I/O对象,包含HTTP请求和响应信息 + */ void handle_get_node_state(nng_aio *aio) { neu_plugin_t * plugin = neu_rest_get_plugin(); @@ -325,6 +384,12 @@ void handle_get_node_state(nng_aio *aio) } } +/** + * 处理获取单个节点状态的响应 + * + * @param aio NNG异步I/O对象,包含HTTP请求和响应信息 + * @param state 节点状态信息 + */ void handle_get_node_state_resp(nng_aio *aio, neu_resp_get_node_state_t *state) { neu_json_get_node_state_resp_t res = { 0 }; @@ -342,6 +407,12 @@ void handle_get_node_state_resp(nng_aio *aio, neu_resp_get_node_state_t *state) free(result); } +/** + * 处理获取多个节点状态的响应 + * + * @param aio NNG异步I/O对象,包含HTTP请求和响应信息 + * @param states 多个节点状态信息 + */ void handle_get_nodes_state_resp(nng_aio * aio, neu_resp_get_nodes_state_t *states) { diff --git a/plugins/restful/datatag_handle.c b/plugins/restful/datatag_handle.c index 7f83d74f..1f4de820 100644 --- a/plugins/restful/datatag_handle.c +++ b/plugins/restful/datatag_handle.c @@ -30,6 +30,11 @@ #include "datatag_handle.h" +/** + * 处理添加标签的HTTP请求 + * + * @param aio NNG异步I/O对象,包含HTTP请求和响应信息 + */ void handle_add_tags(nng_aio *aio) { neu_plugin_t *plugin = neu_rest_get_plugin(); @@ -82,6 +87,12 @@ void handle_add_tags(nng_aio *aio) }) } +/** + * 处理添加标签的响应 + * + * @param aio NNG异步I/O对象,包含HTTP请求和响应信息 + * @param resp 添加标签的响应数据 + */ void handle_add_tags_resp(nng_aio *aio, neu_resp_add_tag_t *resp) { neu_json_add_tag_res_t res = { 0 }; @@ -96,6 +107,11 @@ void handle_add_tags_resp(nng_aio *aio, neu_resp_add_tag_t *resp) free(result); } +/** + * 处理删除标签的HTTP请求 + * + * @param aio NNG异步I/O对象,包含HTTP请求和响应信息 + */ void handle_del_tags(nng_aio *aio) { neu_plugin_t *plugin = neu_rest_get_plugin(); @@ -126,6 +142,11 @@ void handle_del_tags(nng_aio *aio) }) } +/** + * 处理更新标签的HTTP请求 + * + * @param aio NNG异步I/O对象,包含HTTP请求和响应信息 + */ void handle_update_tags(nng_aio *aio) { neu_plugin_t *plugin = neu_rest_get_plugin(); @@ -170,11 +191,22 @@ void handle_update_tags(nng_aio *aio) }) } +/** + * 处理更新标签的响应 + * + * @param aio NNG异步I/O对象,包含HTTP请求和响应信息 + * @param resp 更新标签的响应数据 + */ void handle_update_tags_resp(nng_aio *aio, neu_resp_update_tag_t *resp) { handle_add_tags_resp(aio, resp); } +/** + * 处理获取标签的HTTP请求 + * + * @param aio NNG异步I/O对象,包含HTTP请求和响应信息 + */ void handle_get_tags(nng_aio *aio) { neu_plugin_t * plugin = neu_rest_get_plugin(); @@ -218,6 +250,12 @@ void handle_get_tags(nng_aio *aio) } } +/** + * 处理获取标签的响应 + * + * @param aio NNG异步I/O对象,包含HTTP请求和响应信息 + * @param tags 标签信息的数组 + */ void handle_get_tags_resp(nng_aio *aio, neu_resp_get_tag_t *tags) { neu_json_get_tags_resp_t tags_res = { 0 }; diff --git a/plugins/restful/global_config_handle.c b/plugins/restful/global_config_handle.c index d41ce0d1..a2d59cda 100644 --- a/plugins/restful/global_config_handle.c +++ b/plugins/restful/global_config_handle.c @@ -80,27 +80,160 @@ typedef struct { }; } context_t; +/** + * 获取节点列表 + * + * @param ctx 上下文结构指针 + * @param type 节点类型 + * @return 成功返回0,失败返回错误码 + */ static int get_nodes(context_t *ctx, neu_node_type_e type); + +/** + * 处理获取节点列表的响应 + * + * @param ctx 上下文结构指针 + * @param nodes 节点列表响应结构 + * @return 成功返回0,失败返回错误码 + */ static int get_nodes_resp(context_t *ctx, neu_resp_get_node_t *nodes); + +/** + * 获取所有驱动的组配置 + * + * @param ctx 上下文结构指针 + * @param unused 未使用的参数 + * @return 成功返回0,失败返回错误码 + */ static int get_groups(context_t *ctx, int unused); + +/** + * 处理获取组配置的响应 + * + * @param ctx 上下文结构指针 + * @param groups 组配置响应结构 + * @return 成功返回0,失败返回错误码 + */ static int get_groups_resp(context_t *ctx, neu_resp_get_driver_group_t *groups); + +/** + * 获取驱动组内的标签列表 + * + * @param ctx 上下文结构指针 + * @param info 驱动组信息结构 + * @return 成功返回0,失败返回错误码 + */ static int get_tags(context_t *ctx, neu_resp_driver_group_info_t *info); + +/** + * 处理获取标签列表的响应 + * + * @param ctx 上下文结构指针 + * @param tags 标签列表响应结构 + * @return 成功返回0,失败返回错误码 + */ static int get_tags_resp(context_t *ctx, neu_resp_get_tag_t *tags); + +/** + * 获取应用的订阅组列表 + * + * @param ctx 上下文结构指针 + * @param info 节点信息结构 + * @return 成功返回0,失败返回错误码 + */ static int get_subscriptions(context_t *ctx, neu_resp_node_info_t *info); + +/** + * 处理获取订阅组列表的响应 + * + * @param ctx 上下文结构指针 + * @param groups 订阅组列表响应结构 + * @return 成功返回0,失败返回错误码 + */ static int get_subscriptions_resp(context_t * ctx, neu_resp_get_subscribe_group_t *groups); + +/** + * 获取节点配置 + * + * @param ctx 上下文结构指针 + * @param info 节点信息结构 + * @return 成功返回0,失败返回错误码 + */ static int get_setting(context_t *ctx, neu_resp_node_info_t *info); + +/** + * 处理获取节点配置的响应 + * + * @param ctx 上下文结构指针 + * @param setting 节点配置响应结构 + * @return 成功返回0,失败返回错误码 + */ static int get_setting_resp(context_t * ctx, neu_resp_get_node_setting_t *setting); +/** + * 删除节点 + * + * @param ctx 上下文结构指针 + * @param info 节点信息结构 + * @return 成功返回0,失败返回错误码 + */ static int del_node(context_t *ctx, neu_resp_node_info_t *info); + +/** + * 添加节点 + * + * @param ctx 上下文结构指针 + * @param req 添加节点请求结构 + * @return 成功返回0,失败返回错误码 + */ static int add_node(context_t *ctx, neu_json_get_nodes_resp_node_t *req); + +/** + * 添加组 + * + * @param ctx 上下文结构指针 + * @param data 添加组请求数据 + * @return 成功返回0,失败返回错误码 + */ static int add_group(context_t * ctx, neu_json_get_driver_group_resp_group_t *data); + +/** + * 添加标签 + * + * @param ctx 上下文结构指针 + * @param data 添加标签请求数据 + * @return 成功返回0,失败返回错误码 + */ static int add_tag(context_t *ctx, neu_json_add_tags_req_t *data); + +/** + * 添加订阅 + * + * @param ctx 上下文结构指针 + * @param data 添加订阅请求数据 + * @return 成功返回0,失败返回错误码 + */ static int add_subscription(context_t *ctx, neu_json_subscribe_req_t *data); + +/** + * 添加节点配置 + * + * @param ctx 上下文结构指针 + * @param data 添加节点配置请求数据 + * @return 成功返回0,失败返回错误码 + */ static int add_setting(context_t *ctx, neu_json_node_setting_req_t *data); +/** + * 判断节点是否为静态节点 + * + * @param name 节点名称 + * @param plugin 插件名称 + * @return 是静态节点返回true,否则返回false + */ static inline bool is_static_node(const char *name, const char *plugin) { (void) name; @@ -109,6 +242,13 @@ static inline bool is_static_node(const char *name, const char *plugin) // return 0 == strcmp("monitor", name) && 0 == strcmp("Monitor", plugin); } +/** + * 创建上下文结构 + * + * @param aio NNG异步I/O对象 + * @param get 是否为获取操作 + * @return 创建的上下文结构指针,失败返回NULL + */ static context_t *context_new(nng_aio *aio, bool get) { context_t *ctx = calloc(1, sizeof(*ctx)); @@ -128,6 +268,11 @@ static context_t *context_new(nng_aio *aio, bool get) return ctx; } +/** + * 释放上下文结构及其资源 + * + * @param ctx 上下文结构指针 + */ static void context_free(context_t *ctx) { if (NULL == ctx) { @@ -165,6 +310,13 @@ static void context_free(context_t *ctx) } \ } +/** + * 处理GET请求的状态机推进 + * + * @param ctx 上下文结构指针 + * @param type 请求响应类型 + * @param data 响应数据 + */ static void get_context_next(context_t *ctx, neu_reqresp_type_e type, void *data) { @@ -264,6 +416,13 @@ static void get_context_next(context_t *ctx, neu_reqresp_type_e type, } } +/** + * 处理PUT请求的状态机推进 + * + * @param ctx 上下文结构指针 + * @param type 请求响应类型 + * @param data 响应数据 + */ static void put_context_next(context_t *ctx, neu_reqresp_type_e type, void *data) { @@ -455,6 +614,13 @@ static void put_context_next(context_t *ctx, neu_reqresp_type_e type, } } +/** + * 根据请求类型选择合适的状态机推进函数 + * + * @param ctx 上下文结构指针 + * @param type 请求响应类型 + * @param data 响应数据 + */ static void context_next(context_t *ctx, neu_reqresp_type_e type, void *data) { if (ctx->get) { @@ -976,6 +1142,11 @@ static int add_setting(context_t *ctx, neu_json_node_setting_req_t *data) return 0; } +/** + * 处理获取全局配置的HTTP GET请求 + * + * @param aio NNG异步I/O对象 + */ void handle_get_global_config(nng_aio *aio) { NEU_VALIDATE_JWT(aio); @@ -992,6 +1163,11 @@ void handle_get_global_config(nng_aio *aio) context_next(ctx, NEU_REQ_GET_NODE, NULL); } +/** + * 处理更新全局配置的HTTP PUT请求 + * + * @param aio NNG异步I/O对象 + */ void handle_put_global_config(nng_aio *aio) { NEU_PROCESS_HTTP_REQUEST_VALIDATE_JWT( @@ -1010,9 +1186,16 @@ void handle_put_global_config(nng_aio *aio) }); } +/** + * 处理全局配置请求的响应 + * + * @param aio NNG异步I/O对象 + * @param type 请求响应类型 + * @param data 响应数据 + */ void handle_global_config_resp(nng_aio *aio, neu_reqresp_type_e type, void *data) { context_t *ctx = nng_aio_get_input(aio, 3); context_next(ctx, type, data); -} +} \ No newline at end of file diff --git a/plugins/restful/group_config_handle.c b/plugins/restful/group_config_handle.c index 052b481f..65dcb78a 100644 --- a/plugins/restful/group_config_handle.c +++ b/plugins/restful/group_config_handle.c @@ -29,6 +29,11 @@ #include "group_config_handle.h" +/** + * 处理添加组配置的HTTP请求 + * + * @param aio NNG异步I/O对象,包含HTTP请求和响应信息 + */ void handle_add_group_config(nng_aio *aio) { neu_plugin_t *plugin = neu_rest_get_plugin(); @@ -61,6 +66,13 @@ void handle_add_group_config(nng_aio *aio) }) } +/** + * 发送更新组配置请求 + * + * @param aio NNG异步I/O对象,包含HTTP请求和响应信息 + * @param req 更新组配置的请求数据 + * @return 错误码,0表示成功,其他表示错误 + */ static inline int send_update_group(nng_aio * aio, neu_json_update_group_config_req_t *req) { @@ -114,6 +126,11 @@ static inline int send_update_group(nng_aio * aio, return 0; } +/** + * 处理更新组配置的HTTP请求 + * + * @param aio NNG异步I/O对象,包含HTTP请求和响应信息 + */ void handle_update_group(nng_aio *aio) { NEU_PROCESS_HTTP_REQUEST_VALIDATE_JWT( @@ -127,6 +144,11 @@ void handle_update_group(nng_aio *aio) }) } +/** + * 处理删除组配置的HTTP请求 + * + * @param aio NNG异步I/O对象,包含HTTP请求和响应信息 + */ void handle_del_group_config(nng_aio *aio) { neu_plugin_t *plugin = neu_rest_get_plugin(); @@ -151,6 +173,11 @@ void handle_del_group_config(nng_aio *aio) }) } +/** + * 处理获取组配置的HTTP请求 + * + * @param aio NNG异步I/O对象,包含HTTP请求和响应信息 + */ void handle_get_group_config(nng_aio *aio) { neu_plugin_t * plugin = neu_rest_get_plugin(); @@ -178,6 +205,12 @@ void handle_get_group_config(nng_aio *aio) } } +/** + * 处理获取组配置的响应 + * + * @param aio NNG异步I/O对象,包含HTTP请求和响应信息 + * @param groups 组配置信息的数组 + */ void handle_get_group_resp(nng_aio *aio, neu_resp_get_group_t *groups) { neu_json_get_group_config_resp_t gconfig_res = { 0 }; @@ -206,6 +239,12 @@ void handle_get_group_resp(nng_aio *aio, neu_resp_get_group_t *groups) utarray_free(groups->groups); } +/** + * 处理获取驱动组配置的响应 + * + * @param aio NNG异步I/O对象,包含HTTP请求和响应信息 + * @param groups 驱动组配置信息的数组 + */ void handle_get_driver_group_resp(nng_aio * aio, neu_resp_get_driver_group_t *groups) { @@ -235,6 +274,14 @@ void handle_get_driver_group_resp(nng_aio * aio, utarray_free(groups->groups); } +/** + * 发送订阅请求 + * + * @param aio NNG异步I/O对象,包含HTTP请求和响应信息 + * @param type 请求类型 + * @param req 订阅请求数据 + * @return 错误码,0表示成功,其他表示错误 + */ static inline int send_subscribe(nng_aio *aio, neu_reqresp_type_e type, neu_json_subscribe_req_t *req) { @@ -259,6 +306,11 @@ static inline int send_subscribe(nng_aio *aio, neu_reqresp_type_e type, return 0; } +/** + * 处理组订阅的HTTP请求 + * + * @param aio NNG异步I/O对象,包含HTTP请求和响应信息 + */ void handle_grp_subscribe(nng_aio *aio) { NEU_PROCESS_HTTP_REQUEST_VALIDATE_JWT( @@ -271,6 +323,11 @@ void handle_grp_subscribe(nng_aio *aio) }) } +/** + * 处理更新组订阅的HTTP请求 + * + * @param aio NNG异步I/O对象,包含HTTP请求和响应信息 + */ void handle_grp_update_subscribe(nng_aio *aio) { NEU_PROCESS_HTTP_REQUEST_VALIDATE_JWT( @@ -283,6 +340,11 @@ void handle_grp_update_subscribe(nng_aio *aio) }) } +/** + * 处理取消组订阅的HTTP请求 + * + * @param aio NNG异步I/O对象,包含HTTP请求和响应信息 + */ void handle_grp_unsubscribe(nng_aio *aio) { neu_plugin_t *plugin = neu_rest_get_plugin(); @@ -306,6 +368,13 @@ void handle_grp_unsubscribe(nng_aio *aio) }) } +/** + * 发送批量订阅组的请求 + * + * @param aio NNG异步I/O对象,包含HTTP请求和响应信息 + * @param req 批量订阅组的请求数据 + * @return 错误码,0表示成功,其他表示错误 + */ static inline int send_subscribe_groups(nng_aio * aio, neu_json_subscribe_groups_req_t *req) { @@ -334,6 +403,11 @@ static inline int send_subscribe_groups(nng_aio * aio, return 0; } +/** + * 处理批量订阅组的HTTP请求 + * + * @param aio NNG异步I/O对象,包含HTTP请求和响应信息 + */ void handle_grp_subscribes(nng_aio *aio) { NEU_PROCESS_HTTP_REQUEST_VALIDATE_JWT( @@ -347,6 +421,11 @@ void handle_grp_subscribes(nng_aio *aio) }) } +/** + * 处理获取组订阅信息的HTTP请求 + * + * @param aio NNG异步I/O对象,包含HTTP请求和响应信息 + */ void handle_grp_get_subscribe(nng_aio *aio) { int ret = 0; @@ -376,6 +455,12 @@ void handle_grp_get_subscribe(nng_aio *aio) } } +/** + * 处理获取组订阅信息的响应 + * + * @param aio NNG异步I/O对象,包含HTTP请求和响应信息 + * @param groups 订阅组信息的数组 + */ void handle_grp_get_subscribe_resp(nng_aio * aio, neu_resp_get_subscribe_group_t *groups) { diff --git a/plugins/restful/handle.c b/plugins/restful/handle.c index fb2e1684..16585752 100644 --- a/plugins/restful/handle.c +++ b/plugins/restful/handle.c @@ -504,12 +504,30 @@ static struct neu_http_handler rest_handlers[] = { }, }; +/** + * @brief 获取 REST API 处理器列表 + * + * 该函数提供所有 REST API 的处理器列表和数量 + * + * @param handlers 指向处理器数组指针的指针,用于返回处理器列表 + * @param size 指向整数的指针,用于返回处理器数量 + * @return 无返回值 + */ void neu_rest_handler(const struct neu_http_handler **handlers, uint32_t *size) { *handlers = rest_handlers; *size = sizeof(rest_handlers) / sizeof(struct neu_http_handler); } +/** + * @brief 获取 CORS 处理器列表 + * + * 该函数提供用于处理 CORS 预检请求的处理器列表和数量 + * + * @param handlers 指向处理器数组指针的指针,用于返回 CORS 处理器列表 + * @param size 指向整数的指针,用于返回处理器数量 + * @return 无返回值 + */ void neu_rest_api_cors_handler(const struct neu_http_handler **handlers, uint32_t * size) { @@ -523,6 +541,14 @@ void neu_rest_api_cors_handler(const struct neu_http_handler **handlers, } } +/** + * @brief 初始化 REST 处理上下文 + * + * 该函数创建并初始化 REST 处理上下文,存储插件指针 + * + * @param plugin 插件对象指针 + * @return 返回初始化的上下文指针 + */ neu_rest_handle_ctx_t *neu_rest_init_ctx(void *plugin) { rest_ctx = calloc(1, sizeof(neu_rest_handle_ctx_t)); @@ -531,11 +557,26 @@ neu_rest_handle_ctx_t *neu_rest_init_ctx(void *plugin) return rest_ctx; } +/** + * @brief 释放 REST 处理上下文 + * + * 该函数释放 REST 处理上下文占用的内存资源 + * + * @param ctx 要释放的上下文指针 + * @return 无返回值 + */ void neu_rest_free_ctx(neu_rest_handle_ctx_t *ctx) { free(ctx); } +/** + * @brief 获取 REST 插件对象 + * + * 该函数返回初始化时存储的插件对象指针 + * + * @return 返回插件对象指针 + */ void *neu_rest_get_plugin() { return rest_ctx->plugin; diff --git a/plugins/restful/log_handle.c b/plugins/restful/log_handle.c index 91fbd562..4e5235a5 100644 --- a/plugins/restful/log_handle.c +++ b/plugins/restful/log_handle.c @@ -42,6 +42,15 @@ #include "log_handle.h" #include "utils/utarray.h" +/** + * @brief 处理日志文件的下载请求 + * + * 此函数处理日志文件打包和下载请求,将所有日志打包成 tar.gz 并通过 HTTP + * 响应返回 + * + * @param aio NNG 异步 I/O 对象,用于处理 HTTP 请求和响应 + * @return 无返回值 + */ void handle_logs_files(nng_aio *aio) { void * data = NULL; @@ -106,6 +115,14 @@ void handle_logs_files(nng_aio *aio) nlog_notice("download neuron_debug.tar.gz, ret: %d", rv); } +/** + * @brief 处理日志级别的修改请求 + * + * 该函数用于修改系统或指定节点的日志级别 + * + * @param aio NNG 异步 I/O 对象,用于处理 HTTP 请求和响应 + * @return 无返回值 + */ void handle_log_level(nng_aio *aio) { neu_plugin_t *plugin = neu_rest_get_plugin(); @@ -158,6 +175,16 @@ void handle_log_level(nng_aio *aio) }) } +/** + * @brief 读取文件内容到内存中 + * + * 该函数将指定文件的内容读取到动态分配的内存中 + * + * @param file_name 要读取的文件路径 + * @param datap 指向指针的指针,用于存储读取内容的内存地址 + * @param lenp 指向大小变量的指针,用于存储读取内容的长度 + * @return 成功返回 0,失败返回错误码 + */ int read_file(const char *file_name, void **datap, size_t *lenp) { int rv = 0; diff --git a/plugins/restful/metric_handle.c b/plugins/restful/metric_handle.c index 3908dc49..d3950e7d 100644 --- a/plugins/restful/metric_handle.c +++ b/plugins/restful/metric_handle.c @@ -90,6 +90,14 @@ "south_disconnected_nodes_total %zu\n" // clang-format on +/** + * 发送HTTP响应 + * + * @param aio 异步I/O对象,用于处理HTTP请求和响应 + * @param content 响应内容 + * @param status HTTP状态码 + * @return 0表示成功 + */ static int response(nng_aio *aio, char *content, enum nng_http_status status) { nng_http_res *res = NULL; @@ -120,6 +128,14 @@ static int response(nng_aio *aio, char *content, enum nng_http_status status) return 0; } +/** + * 解析指标类别字符串为枚举值 + * + * @param s 指标类别字符串 + * @param len 字符串长度 + * @param cat 输出参数,解析得到的指标类别枚举值 + * @return true表示解析成功,false表示解析失败 + */ static inline bool parse_metrics_catgory(const char *s, size_t len, neu_metrics_category_e *cat) { @@ -144,6 +160,12 @@ static inline bool parse_metrics_catgory(const char *s, size_t len, return false; } +/** + * 生成全局指标的Prometheus格式文本 + * + * @param metrics 全局指标数据 + * @param stream 输出流 + */ static inline void gen_global_metrics(const neu_metrics_t *metrics, FILE * stream) { @@ -158,6 +180,12 @@ static inline void gen_global_metrics(const neu_metrics_t *metrics, metrics->south_disconnected_nodes); } +/** + * 生成单个节点指标的Prometheus格式文本 + * + * @param node_metrics 节点指标数据 + * @param stream 输出流 + */ static inline void gen_single_node_metrics(neu_node_metrics_t *node_metrics, FILE * stream) { @@ -202,6 +230,13 @@ static inline void gen_single_node_metrics(neu_node_metrics_t *node_metrics, pthread_mutex_unlock(&node_metrics->lock); } +/** + * 检查节点指标中是否包含指定名称的条目 + * + * @param node_metrics 节点指标数据 + * @param name 条目名称 + * @return true表示存在,false表示不存在 + */ static inline bool has_entry(neu_node_metrics_t *node_metrics, const char *name) { neu_metric_entry_t * e = NULL; @@ -221,6 +256,13 @@ static inline bool has_entry(neu_node_metrics_t *node_metrics, const char *name) return false; } +/** + * 生成所有节点指标的Prometheus格式文本 + * + * @param metrics 指标数据 + * @param type_filter 节点类型过滤器 + * @param stream 输出流 + */ static void gen_all_node_metrics(neu_metrics_t *metrics, int type_filter, FILE *stream) { @@ -307,6 +349,12 @@ struct context { const char *node; }; +/** + * 生成节点指标的Prometheus格式文本 + * + * @param metrics 指标数据 + * @param ctx 上下文,包含过滤器、状态、输出流和节点名称 + */ static void gen_node_metrics(neu_metrics_t *metrics, struct context *ctx) { if (ctx->node[0]) { @@ -322,6 +370,11 @@ static void gen_node_metrics(neu_metrics_t *metrics, struct context *ctx) } } +/** + * 处理获取指标的HTTP请求,并以Prometheus格式返回指标数据 + * + * @param aio 异步I/O对象,用于处理HTTP请求和响应 + */ void handle_get_metric(nng_aio *aio) { int status = NNG_HTTP_STATUS_OK; diff --git a/plugins/restful/ndriver_handle.c b/plugins/restful/ndriver_handle.c index 6823e746..8df99ab2 100644 --- a/plugins/restful/ndriver_handle.c +++ b/plugins/restful/ndriver_handle.c @@ -31,6 +31,14 @@ #include "parser/neu_json_ndriver.h" +/** + * 发送北向驱动映射请求 + * + * @param aio 异步I/O对象,用于处理HTTP请求和响应 + * @param req 北向驱动映射请求数据 + * @param type 请求类型,如添加映射或删除映射 + * @return 0表示成功,其他值表示错误码 + */ static int send_ndriver_map_req(nng_aio *aio, neu_json_ndriver_map_t *req, neu_reqresp_type_e type) { @@ -61,6 +69,11 @@ static int send_ndriver_map_req(nng_aio *aio, neu_json_ndriver_map_t *req, return 0; } +/** + * 处理添加北向驱动映射的HTTP请求 + * + * @param aio 异步I/O对象,用于处理HTTP请求和响应 + */ void handle_add_ndriver_map(nng_aio *aio) { NEU_PROCESS_HTTP_REQUEST_VALIDATE_JWT( @@ -73,6 +86,11 @@ void handle_add_ndriver_map(nng_aio *aio) }) } +/** + * 处理删除北向驱动映射的HTTP请求 + * + * @param aio 异步I/O对象,用于处理HTTP请求和响应 + */ void handle_del_ndriver_map(nng_aio *aio) { NEU_PROCESS_HTTP_REQUEST_VALIDATE_JWT( @@ -85,6 +103,11 @@ void handle_del_ndriver_map(nng_aio *aio) }) } +/** + * 处理获取北向驱动映射列表的HTTP请求 + * + * @param aio 异步I/O对象,用于处理HTTP请求和响应 + */ void handle_get_ndriver_maps(nng_aio *aio) { int ret = 0; @@ -115,6 +138,12 @@ void handle_get_ndriver_maps(nng_aio *aio) } } +/** + * 处理获取北向驱动映射响应,并将结果格式化为JSON返回给客户端 + * + * @param aio 异步I/O对象,用于处理HTTP请求和响应 + * @param groups 北向驱动映射组信息 + */ void handle_get_ndriver_maps_resp(nng_aio * aio, neu_resp_get_ndriver_maps_t *groups) { @@ -147,6 +176,13 @@ void handle_get_ndriver_maps_resp(nng_aio * aio, utarray_free(groups->groups); } +/** + * 发送更新北向驱动标签参数的请求 + * + * @param aio 异步I/O对象,用于处理HTTP请求和响应 + * @param req 更新标签参数请求数据 + * @return 0表示成功,其他值表示错误码 + */ static inline int send_update_ndriver_tag_param_req(nng_aio * aio, neu_json_update_ndriver_tag_param_req_t *req) @@ -185,6 +221,11 @@ send_update_ndriver_tag_param_req(nng_aio * aio, return ret; } +/** + * 处理更新北向驱动标签参数的HTTP请求 + * + * @param aio 异步I/O对象,用于处理HTTP请求和响应 + */ void handle_put_ndriver_tag_param(nng_aio *aio) { NEU_PROCESS_HTTP_REQUEST_VALIDATE_JWT( @@ -198,6 +239,13 @@ void handle_put_ndriver_tag_param(nng_aio *aio) }) } +/** + * 发送更新北向驱动标签信息的请求 + * + * @param aio 异步I/O对象,用于处理HTTP请求和响应 + * @param req 更新标签信息请求数据 + * @return 0表示成功,其他值表示错误码 + */ static inline int send_update_ndriver_tag_info_req(nng_aio * aio, neu_json_update_ndriver_tag_info_req_t *req) @@ -236,6 +284,11 @@ send_update_ndriver_tag_info_req(nng_aio * aio, return ret; } +/** + * 处理更新北向驱动标签信息的HTTP请求 + * + * @param aio 异步I/O对象,用于处理HTTP请求和响应 + */ void handle_put_ndriver_tag_info(nng_aio *aio) { NEU_PROCESS_HTTP_REQUEST_VALIDATE_JWT( @@ -249,6 +302,11 @@ void handle_put_ndriver_tag_info(nng_aio *aio) }) } +/** + * 处理获取北向驱动标签列表的HTTP请求 + * + * @param aio 异步I/O对象,用于处理HTTP请求和响应 + */ void handle_get_ndriver_tags(nng_aio *aio) { int ret = 0; @@ -293,6 +351,12 @@ end: } } +/** + * 处理获取北向驱动标签响应,并将结果格式化为JSON返回给客户端 + * + * @param aio 异步I/O对象,用于处理HTTP请求和响应 + * @param tags 北向驱动标签信息 + */ void handle_get_ndriver_tags_resp(nng_aio * aio, neu_resp_get_ndriver_tags_t *tags) { diff --git a/plugins/restful/normal_handle.c b/plugins/restful/normal_handle.c index 52fb800a..f7581634 100644 --- a/plugins/restful/normal_handle.c +++ b/plugins/restful/normal_handle.c @@ -43,11 +43,23 @@ #include "normal_handle.h" +/** + * 处理ping请求,用于检查服务是否在线 + * + * @param aio NNG异步I/O对象,包含HTTP请求和响应信息 + * @return 无返回值,通过aio对象返回空JSON对象响应 + */ void handle_ping(nng_aio *aio) { neu_http_ok(aio, "{}"); } +/** + * 处理用户登录请求,验证用户名和密码,成功则返回JWT令牌 + * + * @param aio NNG异步I/O对象,包含HTTP请求和响应信息 + * @return 无返回值,通过aio对象返回HTTP响应,包含JWT令牌或错误信息 + */ void handle_login(nng_aio *aio) { NEU_PROCESS_HTTP_REQUEST( @@ -99,6 +111,12 @@ void handle_login(nng_aio *aio) }) } +/** + * 处理修改密码请求,验证用户旧密码后更新为新密码 + * + * @param aio NNG异步I/O对象,包含HTTP请求和响应信息 + * @return 无返回值,通过aio对象返回HTTP响应,表示操作成功或失败 + */ void handle_password(nng_aio *aio) { NEU_PROCESS_HTTP_REQUEST_VALIDATE_JWT( @@ -133,6 +151,13 @@ void handle_password(nng_aio *aio) }) } +/** + * 从文件中读取字符串内容 + * + * @param length 输出参数,返回读取的字符串长度 + * @param path 要读取的文件路径 + * @return 读取的字符串内容,失败则返回NULL + */ static char *file_string_read(size_t *length, const char *const path) { FILE *fp = fopen(path, "rb"); @@ -278,6 +303,12 @@ static struct { }, }; +/** + * 将插件名称转换为对应的schema名称 + * + * @param name 插件名称 + * @return 对应的schema名称,如果找不到对应关系则返回原名称 + */ static inline const char *plugin_name_to_schema_name(const char *name) { for (size_t i = 0; i < @@ -291,6 +322,12 @@ static inline const char *plugin_name_to_schema_name(const char *name) return name; } +/** + * 处理获取插件schema请求,返回指定插件的schema JSON配置 + * + * @param aio NNG异步I/O对象,包含HTTP请求和响应信息 + * @return 无返回值,通过aio对象返回HTTP响应,包含schema内容或错误信息 + */ void handle_get_plugin_schema(nng_aio *aio) { size_t len = 0; diff --git a/plugins/restful/plugin_handle.c b/plugins/restful/plugin_handle.c index e8eb183b..9730ed11 100644 --- a/plugins/restful/plugin_handle.c +++ b/plugins/restful/plugin_handle.c @@ -28,6 +28,12 @@ #include "plugin_handle.h" +/** + * 处理添加插件的HTTP请求 + * + * @param aio NNG异步I/O对象,包含HTTP请求和响应信息 + * @return 无返回值,通过aio对象返回HTTP响应 + */ void handle_add_plugin(nng_aio *aio) { neu_plugin_t *plugin = neu_rest_get_plugin(); @@ -50,6 +56,12 @@ void handle_add_plugin(nng_aio *aio) }) } +/** + * 处理删除插件的HTTP请求 + * + * @param aio NNG异步I/O对象,包含HTTP请求和响应信息 + * @return 无返回值,通过aio对象返回HTTP响应 + */ void handle_del_plugin(nng_aio *aio) { neu_plugin_t *plugin = neu_rest_get_plugin(); @@ -77,6 +89,12 @@ void handle_del_plugin(nng_aio *aio) }) } +/** + * 处理获取插件列表的HTTP请求 + * + * @param aio NNG异步I/O对象,包含HTTP请求和响应信息 + * @return 无返回值,通过aio对象返回HTTP响应 + */ void handle_get_plugin(nng_aio *aio) { neu_plugin_t * plugin = neu_rest_get_plugin(); @@ -96,6 +114,13 @@ void handle_get_plugin(nng_aio *aio) } } +/** + * 处理获取插件列表的响应,将插件信息转换为JSON格式并发送HTTP响应 + * + * @param aio NNG异步I/O对象,包含HTTP请求和响应信息 + * @param plugins 插件列表信息结构体指针 + * @return 无返回值,通过aio对象返回HTTP响应 + */ void handle_get_plugin_resp(nng_aio *aio, neu_resp_get_plugin_t *plugins) { neu_json_get_plugin_resp_t plugin_res = { 0 }; diff --git a/plugins/restful/rest.c b/plugins/restful/rest.c index 17e1faf7..7873e018 100644 --- a/plugins/restful/rest.c +++ b/plugins/restful/rest.c @@ -49,6 +49,13 @@ struct neu_plugin { neu_rest_handle_ctx_t *handle_ctx; }; +/** + * @brief 初始化HTTP服务器 + * + * 此函数创建并初始化NNG HTTP服务器,绑定到指定的URL地址和端口。 + * + * @return 成功返回服务器指针,失败返回NULL + */ static nng_http_server *server_init() { nng_url * url; @@ -72,6 +79,14 @@ static nng_http_server *server_init() return server; } +/** + * @brief 打开仪表板插件 + * + * 此函数创建并初始化仪表板插件实例,设置HTTP处理器和CORS处理器, + * 启动HTTP服务器。 + * + * @return 成功返回插件实例指针,失败返回NULL + */ static neu_plugin_t *dashb_plugin_open(void) { int rv; @@ -116,6 +131,14 @@ server_init_fail: return NULL; } +/** + * @brief 关闭仪表板插件 + * + * 此函数停止并释放HTTP服务器,清理插件资源。 + * + * @param plugin 插件实例指针 + * @return 成功返回0,失败返回错误码 + */ static int dashb_plugin_close(neu_plugin_t *plugin) { int rv = 0; @@ -128,6 +151,15 @@ static int dashb_plugin_close(neu_plugin_t *plugin) return rv; } +/** + * @brief 初始化仪表板插件 + * + * 此函数执行插件的初始化操作,包括JWT系统初始化。 + * + * @param plugin 插件实例指针 + * @param load 是否为加载时调用 + * @return 成功返回0,失败返回错误码 + */ static int dashb_plugin_init(neu_plugin_t *plugin, bool load) { (void) load; @@ -141,6 +173,14 @@ static int dashb_plugin_init(neu_plugin_t *plugin, bool load) return rv; } +/** + * @brief 卸载仪表板插件 + * + * 此函数执行插件的卸载操作,清理插件资源。 + * + * @param plugin 插件实例指针 + * @return 成功返回0,失败返回错误码 + */ static int dashb_plugin_uninit(neu_plugin_t *plugin) { int rv = 0; @@ -151,6 +191,15 @@ static int dashb_plugin_uninit(neu_plugin_t *plugin) return rv; } +/** + * @brief 配置仪表板插件 + * + * 此函数应用新的插件配置。 + * + * @param plugin 插件实例指针 + * @param configs 配置字符串 + * @return 成功返回0,失败返回错误码 + */ static int dashb_plugin_config(neu_plugin_t *plugin, const char *configs) { int rv = 0; @@ -162,6 +211,16 @@ static int dashb_plugin_config(neu_plugin_t *plugin, const char *configs) return rv; } +/** + * @brief 处理插件请求 + * + * 此函数处理各种插件请求类型,并调用相应的处理函数生成HTTP响应。 + * + * @param plugin 插件实例指针 + * @param header 请求头信息 + * @param data 请求数据 + * @return 成功返回0,失败返回错误码 + */ static int dashb_plugin_request(neu_plugin_t * plugin, neu_reqresp_head_t *header, void *data) { @@ -249,12 +308,28 @@ static int dashb_plugin_request(neu_plugin_t * plugin, return 0; } +/** + * @brief 启动仪表板插件 + * + * 此函数启动插件的运行状态。 + * + * @param plugin 插件实例指针 + * @return 成功返回0,失败返回错误码 + */ static int dashb_plugin_start(neu_plugin_t *plugin) { (void) plugin; return 0; } +/** + * @brief 停止仪表板插件 + * + * 此函数停止插件的运行状态。 + * + * @param plugin 插件实例指针 + * @return 成功返回0,失败返回错误码 + */ static int dashb_plugin_stop(neu_plugin_t *plugin) { (void) plugin; diff --git a/plugins/restful/rw_handle.c b/plugins/restful/rw_handle.c index 11e1947e..ef9e6d1f 100644 --- a/plugins/restful/rw_handle.c +++ b/plugins/restful/rw_handle.c @@ -28,6 +28,14 @@ #include "rw_handle.h" +/** + * @brief 处理读取请求的HTTP处理函数 + * + * 此函数处理来自客户端的读取请求,验证JWT令牌,解析读取命令, + * 并将命令发送给插件进行处理。 + * + * @param aio NNG异步I/O对象,包含HTTP请求和响应信息 + */ void handle_read(nng_aio *aio) { neu_plugin_t *plugin = neu_rest_get_plugin(); @@ -56,6 +64,14 @@ void handle_read(nng_aio *aio) }) } +/** + * @brief 处理写入请求的HTTP处理函数 + * + * 此函数处理来自客户端的标签写入请求,验证JWT令牌,解析写入命令, + * 检查值的大小是否合法,并将命令发送给插件进行处理。 + * + * @param aio NNG异步I/O对象,包含HTTP请求和响应信息 + */ void handle_write(nng_aio *aio) { neu_plugin_t *plugin = neu_rest_get_plugin(); @@ -135,6 +151,14 @@ void handle_write(nng_aio *aio) }) } +/** + * @brief 处理多标签写入请求的HTTP处理函数 + * + * 此函数处理来自客户端的多标签写入请求,验证JWT令牌,解析写入命令, + * 检查字符串值的大小是否合法,并将命令发送给插件进行处理。 + * + * @param aio NNG异步I/O对象,包含HTTP请求和响应信息 + */ void handle_write_tags(nng_aio *aio) { neu_plugin_t *plugin = neu_rest_get_plugin(); @@ -214,6 +238,14 @@ void handle_write_tags(nng_aio *aio) }) } +/** + * @brief 将JSON请求转换为内部命令结构 + * + * 此函数将解析后的JSON请求转换为内部命令结构,处理内存分配和值类型转换。 + * + * @param req 解析后的JSON请求结构 + * @param cmd 输出的内部命令结构 + */ static void trans(neu_json_write_gtags_req_t *req, neu_req_write_gtags_t *cmd) { cmd->driver = req->node; @@ -267,6 +299,14 @@ static void trans(neu_json_write_gtags_req_t *req, neu_req_write_gtags_t *cmd) } } +/** + * @brief 处理多组标签写入请求的HTTP处理函数 + * + * 此函数处理来自客户端的多组标签写入请求,验证JWT令牌,解析写入命令, + * 并将命令发送给插件进行处理。 + * + * @param aio NNG异步I/O对象,包含HTTP请求和响应信息 + */ void handle_write_gtags(nng_aio *aio) { neu_plugin_t *plugin = neu_rest_get_plugin(); @@ -294,6 +334,15 @@ void handle_write_gtags(nng_aio *aio) }) } +/** + * @brief 处理读取响应的函数 + * + * 此函数处理读取操作的响应,将内部响应结构转换为JSON格式, + * 并发送HTTP响应给客户端。 + * + * @param aio NNG异步I/O对象,用于发送HTTP响应 + * @param resp 内部读取响应结构,包含读取到的标签值 + */ void handle_read_resp(nng_aio *aio, neu_resp_read_group_t *resp) { neu_json_read_resp_t api_res = { 0 }; diff --git a/plugins/restful/template_handle.c b/plugins/restful/template_handle.c index 69673f08..13817568 100644 --- a/plugins/restful/template_handle.c +++ b/plugins/restful/template_handle.c @@ -30,6 +30,15 @@ #include "handle.h" +/** + * @brief 根据JSON数据设置标签属性 + * + * 将JSON解析得到的标签信息应用到neu_datatag_t结构体 + * + * @param tag 目标标签结构体 + * @param json_tag 源JSON标签数据 + * @return int 成功返回0,失败返回错误码 + */ static int set_tag_by_json(neu_datatag_t *tag, neu_json_tag_t *json_tag) { if (strlen(json_tag->name) >= NEU_TAG_NAME_LEN) { @@ -66,6 +75,15 @@ static int set_tag_by_json(neu_datatag_t *tag, neu_json_tag_t *json_tag) return 0; } +/** + * @brief 将JSON模板数据转移到命令结构体 + * + * 解析JSON模板数据并将其移动到命令结构体中 + * + * @param cmd 目标命令结构体 + * @param req 源JSON模板数据 + * @return int 成功返回0,失败返回错误码 + */ static int move_template_json(neu_req_add_template_t *cmd, neu_json_template_t * req) { @@ -136,6 +154,13 @@ error: return ret; } +/** + * @brief 处理添加模板的HTTP请求 + * + * 解析请求并添加一个新的模板 + * + * @param aio NNG异步I/O对象,用于处理HTTP请求和响应 + */ void handle_add_template(nng_aio *aio) { neu_plugin_t *plugin = neu_rest_get_plugin(); @@ -164,6 +189,13 @@ void handle_add_template(nng_aio *aio) }) } +/** + * @brief 处理删除模板的HTTP请求 + * + * 解析请求并删除指定名称的模板 + * + * @param aio NNG异步I/O对象,用于处理HTTP请求和响应 + */ void handle_del_template(nng_aio *aio) { neu_plugin_t *plugin = neu_rest_get_plugin(); @@ -193,6 +225,13 @@ void handle_del_template(nng_aio *aio) } } +/** + * @brief 处理获取模板的HTTP请求 + * + * 解析请求并获取指定名称的模板或所有模板 + * + * @param aio NNG异步I/O对象,用于处理HTTP请求和响应 + */ void handle_get_template(nng_aio *aio) { neu_plugin_t *plugin = neu_rest_get_plugin(); @@ -226,6 +265,14 @@ void handle_get_template(nng_aio *aio) } } +/** + * @brief 处理获取单个模板的响应 + * + * 将获取的模板数据格式化为JSON并发送HTTP响应 + * + * @param aio NNG异步I/O对象,用于处理HTTP响应 + * @param resp 获取的模板响应数据 + */ void handle_get_template_resp(nng_aio *aio, neu_resp_get_template_t *resp) { int rv = 0; @@ -306,6 +353,14 @@ end: return; } +/** + * @brief 处理获取多个模板的响应 + * + * 将获取的多个模板数据格式化为JSON并发送HTTP响应 + * + * @param aio NNG异步I/O对象,用于处理HTTP响应 + * @param resp 获取的多个模板响应数据 + */ void handle_get_templates_resp(nng_aio *aio, neu_resp_get_templates_t *resp) { @@ -337,6 +392,15 @@ void handle_get_templates_resp(nng_aio *aio, neu_resp_get_templates_t *resp) return; } +/** + * @brief 发送添加模板组请求 + * + * 解析并验证添加模板组请求参数,然后发送请求 + * + * @param aio NNG异步I/O对象,用于处理HTTP响应 + * @param req 添加模板组请求数据 + * @return int 成功返回0,失败返回错误码 + */ static inline int send_template_add_group_req(nng_aio * aio, neu_json_template_add_group_req_t *req) @@ -368,6 +432,13 @@ send_template_add_group_req(nng_aio * aio, return 0; } +/** + * @brief 处理添加模板组的HTTP请求 + * + * 解析请求并添加一个新的模板组 + * + * @param aio NNG异步I/O对象,用于处理HTTP请求和响应 + */ void handle_add_template_group(nng_aio *aio) { NEU_PROCESS_HTTP_REQUEST_VALIDATE_JWT( @@ -381,6 +452,15 @@ void handle_add_template_group(nng_aio *aio) }) } +/** + * @brief 发送更新模板组请求 + * + * 解析并验证更新模板组请求参数,然后发送请求 + * + * @param aio NNG异步I/O对象,用于处理HTTP响应 + * @param req 更新模板组请求数据 + * @return int 成功返回0,失败返回错误码 + */ static inline int send_template_update_group_req(nng_aio * aio, neu_json_template_update_group_req_t *req) @@ -435,6 +515,13 @@ send_template_update_group_req(nng_aio * aio, return 0; } +/** + * @brief 处理更新模板组的HTTP请求 + * + * 解析请求并更新指定的模板组 + * + * @param aio NNG异步I/O对象,用于处理HTTP请求和响应 + */ void handle_update_template_group(nng_aio *aio) { NEU_PROCESS_HTTP_REQUEST_VALIDATE_JWT( @@ -448,6 +535,15 @@ void handle_update_template_group(nng_aio *aio) }) } +/** + * @brief 发送删除模板组请求 + * + * 解析并验证删除模板组请求参数,然后发送请求 + * + * @param aio NNG异步I/O对象,用于处理HTTP响应 + * @param req 删除模板组请求数据 + * @return int 成功返回0,失败返回错误码 + */ static inline int send_template_del_group_req(nng_aio * aio, neu_json_template_del_group_req_t *req) @@ -478,6 +574,13 @@ send_template_del_group_req(nng_aio * aio, return 0; } +/** + * @brief 处理删除模板组的HTTP请求 + * + * 解析请求并删除指定的模板组 + * + * @param aio NNG异步I/O对象,用于处理HTTP请求和响应 + */ void handle_del_template_group(nng_aio *aio) { NEU_PROCESS_HTTP_REQUEST_VALIDATE_JWT( @@ -491,6 +594,16 @@ void handle_del_template_group(nng_aio *aio) }) } +/** + * @brief 发送模板修改标签请求 + * + * 解析并验证模板修改标签请求参数,然后发送请求 + * + * @param aio NNG异步I/O对象,用于处理HTTP响应 + * @param type 请求类型,如添加或更新标签 + * @param req 模板修改标签请求数据 + * @return int 成功返回0,失败返回错误码 + */ static int send_template_mod_tags_req(nng_aio *aio, neu_reqresp_type_e type, neu_json_template_mod_tags_req_t *req) { @@ -535,6 +648,15 @@ static int send_template_mod_tags_req(nng_aio *aio, neu_reqresp_type_e type, return ret; } +/** + * @brief 发送删除模板标签请求 + * + * 解析并验证删除模板标签请求参数,然后发送请求 + * + * @param aio NNG异步I/O对象,用于处理HTTP响应 + * @param req 删除模板标签请求数据 + * @return int 成功返回0,失败返回错误码 + */ static int send_template_del_tags_req(nng_aio * aio, neu_json_template_del_tags_req_t *req) { @@ -581,6 +703,13 @@ static int send_template_del_tags_req(nng_aio * aio, return ret; } +/** + * @brief 处理添加模板标签的HTTP请求 + * + * 解析请求并添加模板标签 + * + * @param aio NNG异步I/O对象,用于处理HTTP请求和响应 + */ void handle_add_template_tags(nng_aio *aio) { NEU_PROCESS_HTTP_REQUEST_VALIDATE_JWT( @@ -595,6 +724,13 @@ void handle_add_template_tags(nng_aio *aio) }) } +/** + * @brief 处理更新模板标签的HTTP请求 + * + * 解析请求并更新模板标签 + * + * @param aio NNG异步I/O对象,用于处理HTTP请求和响应 + */ void handle_update_template_tags(nng_aio *aio) { NEU_PROCESS_HTTP_REQUEST_VALIDATE_JWT( @@ -609,6 +745,13 @@ void handle_update_template_tags(nng_aio *aio) }) } +/** + * @brief 处理删除模板标签的HTTP请求 + * + * 解析请求并删除模板标签 + * + * @param aio NNG异步I/O对象,用于处理HTTP请求和响应 + */ void handle_del_template_tags(nng_aio *aio) { NEU_PROCESS_HTTP_REQUEST_VALIDATE_JWT( @@ -622,6 +765,13 @@ void handle_del_template_tags(nng_aio *aio) }) } +/** + * @brief 处理获取模板标签的HTTP请求 + * + * 解析请求并获取模板标签信息 + * + * @param aio NNG异步I/O对象,用于处理HTTP请求和响应 + */ void handle_get_template_tags(nng_aio *aio) { neu_plugin_t *plugin = neu_rest_get_plugin(); @@ -669,6 +819,15 @@ end: } } +/** + * @brief 发送模板实例化请求 + * + * 解析并验证模板实例化请求参数,然后发送请求 + * + * @param aio NNG异步I/O对象,用于处理HTTP响应 + * @param req 模板实例化请求数据 + * @return int 成功返回0,失败返回错误码 + */ static inline int send_template_inst_req(nng_aio * aio, neu_json_template_inst_req_t *req) { @@ -698,6 +857,13 @@ static inline int send_template_inst_req(nng_aio * aio, return 0; } +/** + * @brief 处理实例化模板的HTTP请求 + * + * 解析请求并实例化指定模板 + * + * @param aio NNG异步I/O对象,用于处理HTTP请求和响应 + */ void handle_instantiate_template(nng_aio *aio) { NEU_PROCESS_HTTP_REQUEST_VALIDATE_JWT( @@ -710,6 +876,15 @@ void handle_instantiate_template(nng_aio *aio) }) } +/** + * @brief 发送批量模板实例化请求 + * + * 解析并验证批量模板实例化请求参数,然后发送请求 + * + * @param aio NNG异步I/O对象,用于处理HTTP响应 + * @param req 批量模板实例化请求数据 + * @return int 成功返回0,失败返回错误码 + */ static inline int send_template_insts_req(nng_aio * aio, neu_json_template_insts_req_t *req) { @@ -744,6 +919,13 @@ static inline int send_template_insts_req(nng_aio * aio, return 0; } +/** + * @brief 处理批量实例化模板的HTTP请求 + * + * 解析请求并批量实例化指定模板 + * + * @param aio NNG异步I/O对象,用于处理HTTP请求和响应 + */ void handle_instantiate_templates(nng_aio *aio) { NEU_PROCESS_HTTP_REQUEST_VALIDATE_JWT( @@ -757,6 +939,13 @@ void handle_instantiate_templates(nng_aio *aio) }) } +/** + * @brief 处理获取模板组信息的HTTP请求 + * + * 解析请求并获取指定模板的组信息 + * + * @param aio NNG异步I/O对象,用于处理HTTP请求和响应 + */ void handle_get_template_group(nng_aio *aio) { neu_plugin_t *plugin = neu_rest_get_plugin(); diff --git a/plugins/restful/user.c b/plugins/restful/user.c index ed1c9f3c..31e1458c 100644 --- a/plugins/restful/user.c +++ b/plugins/restful/user.c @@ -23,11 +23,15 @@ static const unsigned char cov_2char[64] = { static const char ascii_dollar[] = { 0x24, 0x00 }; -/* - * SHA based password algorithm, describe by Ulrich Drepper here: - * https://www.akkadia.org/drepper/SHA-crypt.txt - * (note that it's in the public domain) - * See: https://github.com/openssl/openssl/blob/master/apps/passwd.c +/** + * @brief 使用SHA算法对密码进行加密 + * + * 基于Ulrich Drepper描述的SHA密码算法实现,该算法用于密码存储 + * + * @param passwd 需要加密的密码明文 + * @param magic 加密算法标识,目前支持'5'(SHA256)和'6'(SHA512) + * @param salt 加密盐值,用于增强安全性 + * @return char* 返回加密后的密码字符串,失败时返回NULL */ static char *shacrypt(const char *passwd, const char *magic, const char *salt) { @@ -313,6 +317,15 @@ err: return NULL; } +/** + * @brief 对密码进行哈希处理 + * + * 使用SHA算法对密码进行哈希处理,如果未提供盐值则生成随机盐值 + * + * @param passwd 需要哈希的密码明文 + * @param salt 可选的盐值,如果为NULL则生成新的随机盐值 + * @return char* 返回哈希后的密码字符串,失败时返回NULL + */ // NOTE: if provided, require salt length of 16 static char *hash_password(const char *passwd, const char *salt) { @@ -343,6 +356,15 @@ end: return hash; } +/** + * @brief 创建新用户 + * + * 创建一个新用户并对密码进行哈希处理 + * + * @param name 用户名 + * @param password 用户密码明文 + * @return neu_user_t* 返回创建的用户对象,失败时返回NULL + */ neu_user_t *neu_user_new(const char *name, const char *password) { neu_user_t *user = malloc(sizeof(*user)); @@ -363,6 +385,13 @@ neu_user_t *neu_user_new(const char *name, const char *password) return user; } +/** + * @brief 释放用户对象 + * + * 释放用户对象及其关联的内存资源 + * + * @param user 需要释放的用户对象 + */ void neu_user_free(neu_user_t *user) { if (NULL != user) { @@ -372,6 +401,15 @@ void neu_user_free(neu_user_t *user) } } +/** + * @brief 检查用户密码是否正确 + * + * 验证提供的密码是否与用户存储的哈希值匹配 + * + * @param user 用户对象 + * @param password 需要验证的密码明文 + * @return bool 密码正确返回true,错误返回false + */ bool neu_user_check_password(neu_user_t *user, const char *password) { char *salt = user->hash + 3; @@ -383,6 +421,14 @@ bool neu_user_check_password(neu_user_t *user, const char *password) return pass; } +/** + * @brief 从持久化存储加载用户信息 + * + * 根据用户名从持久化存储中加载用户信息 + * + * @param name 要加载的用户名 + * @return neu_user_t* 返回加载的用户对象,失败时返回NULL + */ neu_user_t *neu_load_user(const char *name) { neu_user_t *user = malloc(sizeof(*user)); @@ -403,6 +449,14 @@ neu_user_t *neu_load_user(const char *name) return user; } +/** + * @brief 保存用户信息到持久化存储 + * + * 将用户信息保存到持久化存储中 + * + * @param user 要保存的用户对象 + * @return int 成功返回0,失败返回错误码 + */ int neu_save_user(neu_user_t *user) { neu_persist_user_info_t info = { @@ -413,6 +467,15 @@ int neu_save_user(neu_user_t *user) return neu_persister_update_user(&info); } +/** + * @brief 更新用户密码 + * + * 使用新密码更新用户的密码哈希 + * + * @param user 用户对象 + * @param new_password 新密码明文 + * @return int 成功返回0,失败返回错误码 + */ int neu_user_update_password(neu_user_t *user, const char *new_password) { char *salt = user->hash + 3; diff --git a/plugins/restful/version_handle.c b/plugins/restful/version_handle.c index 477b814e..6b8a13d6 100644 --- a/plugins/restful/version_handle.c +++ b/plugins/restful/version_handle.c @@ -26,6 +26,14 @@ #include "handle.h" #include "utils/http.h" +/** + * @brief 处理获取版本信息的HTTP请求 + * + * 获取当前Neuron的版本信息并以JSON格式返回,包括版本号、Git修订号和构建日期 + * + * @param aio NNG异步I/O对象,用于处理HTTP请求和响应 + * @return 无返回值,通过aio对象发送响应 + */ void handle_get_version(nng_aio *aio) { char *result = NULL;