Files
EventHubBack/src/eventhub_app.erl
2026-04-20 21:04:16 +03:00

58 lines
1.6 KiB
Erlang

-module(eventhub_app).
-behaviour(application).
-export([start/2, stop/1]).
start(_StartType, _StartArgs) ->
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(),
start_http(),
{ok, Pid};
Error ->
Error
end.
stop(_State) ->
ok.
start_http() ->
Port = application:get_env(eventhub, http_port, 8080),
Dispatch = cowboy_router:compile([
{'_', [
{"/health", handler_health, []},
{"/v1/register", handler_register, []},
{"/v1/login", handler_login, []},
{"/v1/refresh", handler_refresh, []},
{"/v1/user/me", handler_user_me, []},
{"/v1/user/bookings", handler_user_bookings, []},
{"/v1/search", handler_search, []},
{"/v1/calendars", handler_calendars, []},
{"/v1/calendars/:id", handler_calendar_by_id, []},
{"/v1/calendars/:calendar_id/events", handler_events, []},
{"/v1/events/:id", handler_event_by_id, []},
{"/v1/events/:id/occurrences", handler_event_occurrences, []},
{"/v1/events/:id/occurrences/:start_time", handler_event_occurrences, []},
{"/v1/events/:id/bookings", handler_bookings, []},
{"/v1/bookings/:id", handler_booking_by_id, []}
]}
]),
Middlewares = [
cowboy_router,
cowboy_handler
],
Env = #{dispatch => Dispatch},
cowboy:start_clear(http, [{port, Port}], #{
env => Env,
middlewares => Middlewares
}),
io:format("HTTP server started on port ~p~n", [Port]).