环境信息:
- EMQX 版本:5.x
- 部署环境:K8s
我希望开发一个 EMQX 插件,并为其添加一个自定义的 REST API 接口(例如 GET /api/v5/my-plugin/test)。这个接口需要能够通过 Dashboard 的认证体系进行访问。
想请教官方团队和社区专家,实现此功能的标准做法或最佳实践是什么?是否需要通过特定的函数注册路由,或是遵循某种模块命名规范?
环境信息:
我希望开发一个 EMQX 插件,并为其添加一个自定义的 REST API 接口(例如 GET /api/v5/my-plugin/test)。这个接口需要能够通过 Dashboard 的认证体系进行访问。
想请教官方团队和社区专家,实现此功能的标准做法或最佳实践是什么?是否需要通过特定的函数注册路由,或是遵循某种模块命名规范?
暂时没有,目前只能自己 hack,自己看源码改 cowboy 的路由表。
大概插件里面做:
append_handlers_to_dashboard_listeners() ->
{ok, _} = application:ensure_all_started(emqx_dashboard),
Res =
lists:flatten([
append_handlers_to_dashboard_listeners(Type)
|| Type <-
['http:dashboard', 'https:dashboard']
]),
Res == [] andalso error(append_handlers_to_dashboard_listeners_failed).
append_handlers_to_dashboard_listeners(Type) ->
case persistent_term:get(Type, undefined) of
[{'_', [], Routes}] ->
persistent_term:put(Type, [{'_', [], raw_cowboy_routers() ++ Routes}]),
[Type];
undefined ->
[]
end.
remove_handlers_from_dashboard_listeners() ->
remove_handlers_from_dashboard_listeners('http:dashboard'),
remove_handlers_from_dashboard_listeners('https:dashboard').
remove_handlers_from_dashboard_listeners(Type) ->
case persistent_term:get(Type, undefined) of
[{'_', [], Routes}] ->
Routes1 = lists:filter(
fun
({[<<"api">>, <<"v5">>, <<"xxx">> | _], _, _, _}) ->
false;
(_) ->
true
end,
Routes
),
persistent_term:put(Type, [{'_', [], Routes1}]),
ok;
undefined ->
ok
end.
自己实现raw_cowboy_routers() ,这个 hack 有个缺点:没有 swagger 文档。但是功能是有的。