Перенести все админские эндпоинты на порт 8445 и добавить отдельную авторизацию для админов. Часть 1

This commit is contained in:
2026-04-27 15:54:48 +03:00
parent 62bc62f990
commit 4ed6a961ab
40 changed files with 3573 additions and 800 deletions

View File

@@ -1,21 +1,18 @@
-module(eventhub_app).
-behaviour(application).
-export([start/2, stop/1]).
start(_StartType, _StartArgs) ->
pg:start_link(),
application:ensure_all_started(mnesia),
application:ensure_all_started(cowboy),
case infra_sup:start_link() of
{ok, Pid} ->
ok = infra_mnesia:init_tables(),
ok = infra_mnesia:wait_for_tables(),
connect_nodes(),
start_http(),
start_admin_http(),
% Запускаем сборщик метрик Prometheus
start_http(), % Пользовательский API (8080)
start_admin_http(), % Административный API (8445)
application:ensure_all_started(prometheus),
application:ensure_all_started(prometheus_cowboy),
{ok, Pid};
@@ -23,12 +20,13 @@ start(_StartType, _StartArgs) ->
Error
end.
stop(_State) ->
ok.
stop(_State) -> ok.
%% ===================================================================
%% Пользовательский HTTP (порт 8080) — только публичные эндпоинты
%% ===================================================================
start_http() ->
Port = application:get_env(eventhub, http_port, 8080),
Dispatch = cowboy_router:compile([
{'_', [
{"/metrics/[:registry]", prometheus_cowboy2_handler, []},
@@ -52,83 +50,66 @@ start_http() ->
{"/v1/reviews/:id", handler_review_by_id, []},
{"/v1/reports", handler_reports, []},
{"/v1/tickets", handler_tickets, []},
{"/v1/subscription", handler_subscription, []},
% Админские маршруты - более конкретные ПЕРЕД общими
{"/v1/admin/reports", handler_reports, []},
{"/v1/admin/reports/:id", handler_report_by_id, []},
{"/v1/admin/reviews/:id", handler_admin_reviews, []},
{"/v1/admin/banned-words", handler_banned_words, []},
{"/v1/admin/banned-words/:word", handler_banned_words, []},
{"/v1/admin/tickets/stats", handler_ticket_stats, []},
{"/v1/admin/tickets/:id", handler_ticket_by_id, []},
{"/v1/admin/tickets", handler_tickets, []},
{"/v1/admin/subscriptions", handler_admin_subscriptions, []},
{"/v1/admin/subscriptions/:id", handler_admin_subscriptions, []},
% Общий маршрут для заморозки (должен быть последним)
{"/v1/admin/:target_type/:id", handler_admin_moderation, []}
{"/v1/subscription", handler_subscription, []}
]}
]),
Middlewares = [
cowboy_router,
cowboy_handler
],
Middlewares = [cowboy_router, cowboy_handler],
Env = #{dispatch => Dispatch},
cowboy:start_clear(http, [{port, Port}], #{
env => Env,
middlewares => Middlewares
}),
cowboy:start_clear(http, [{port, Port}], #{env => Env, middlewares => Middlewares}),
io:format("HTTP server started on port ~p~n", [Port]).
%% ===================================================================
%% Административный HTTP (порт 8445) — все админские эндпоинты
%% ===================================================================
start_admin_http() ->
Port = application:get_env(eventhub, admin_http_port, 8445),
Dispatch = cowboy_router:compile([
{'_', [
{"/admin/health", admin_handler_health, []},
{"/admin/stats", admin_handler_stats, []},
{"/admin/users", admin_handler_users, []},
{"/admin/users/:id", admin_handler_user_by_id, []}
% ================== БАЗОВЫЕ ==================
{"/v1/admin/health", admin_handler_health, []},
{"/v1/admin/stats", admin_handler_stats, []},
{"/v1/admin/login", admin_handler_login, []},
% ================== ПОЛЬЗОВАТЕЛИ ==================
{"/v1/admin/users", admin_handler_users, []},
{"/v1/admin/users/:id", admin_handler_user_by_id, []},
% ================== ОТЧЁТЫ ==================
{"/v1/admin/reports", admin_handler_reports, []},
{"/v1/admin/reports/:id", admin_handler_report_by_id, []},
% ================== ОТЗЫВЫ ==================
{"/v1/admin/reviews/:id", admin_handler_reviews, []},
% ================== БАН-СЛОВА ==================
{"/v1/admin/banned-words", admin_handler_banned_words, []},
{"/v1/admin/banned-words/:word", admin_handler_banned_words, []},
% ================== ТИКЕТЫ ==================
{"/v1/admin/tickets/stats", admin_handler_ticket_stats, []},
{"/v1/admin/tickets/:id", admin_handler_ticket_by_id, []},
{"/v1/admin/tickets", admin_handler_tickets, []},
% ================== ПОДПИСКИ ==================
{"/v1/admin/subscriptions", admin_handler_subscriptions, []},
{"/v1/admin/subscriptions/:id", admin_handler_subscriptions, []},
% ================== МОДЕРАЦИЯ (общий маршрут) ==================
{"/v1/admin/:target_type/:id", admin_handler_moderation, []}
]}
]),
Middlewares = [
cowboy_router,
cowboy_handler
],
Middlewares = [cowboy_router, cowboy_handler],
Env = #{dispatch => Dispatch},
cowboy:start_clear(admin_http, [{port, Port}], #{
env => Env,
middlewares => Middlewares
}),
cowboy:start_clear(admin_http, [{port, Port}], #{env => Env, middlewares => Middlewares}),
io:format("Admin HTTP server started on port ~p~n", [Port]),
% WebSocket для пользователей
WsDispatch = cowboy_router:compile([
{'_', [{"/ws", ws_handler, []}]}
]),
cowboy:start_clear(ws, [{port, 8081}], #{
env => #{dispatch => WsDispatch}
}),
WsDispatch = cowboy_router:compile([{'_', [{"/ws", ws_handler, []}]}]),
cowboy:start_clear(ws, [{port, 8081}], #{env => #{dispatch => WsDispatch}}),
% WebSocket для админов
AdminWsDispatch = cowboy_router:compile([
{'_', [{"/admin/ws", admin_ws_handler, []}]}
]),
cowboy:start_clear(admin_ws, [{port, 8446}], #{
env => #{dispatch => AdminWsDispatch}
}),
AdminWsDispatch = cowboy_router:compile([{'_', [{"/admin/ws", admin_ws_handler, []}]}]),
cowboy:start_clear(admin_ws, [{port, 8446}], #{env => #{dispatch => AdminWsDispatch}}),
io:format("WebSocket started on ports 8081 (user) and 8446 (admin)~n").
%% ===================================================================
%% Ручное подключение к нодам кластера (запасной вариант)
%% ===================================================================
connect_nodes() ->
case os:getenv("JOIN_NODES") of
false -> ok;