diff options
Diffstat (limited to 'apps/dreki_web/src/dreki_web_app.erl')
-rw-r--r-- | apps/dreki_web/src/dreki_web_app.erl | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/apps/dreki_web/src/dreki_web_app.erl b/apps/dreki_web/src/dreki_web_app.erl new file mode 100644 index 0000000..5b6454e --- /dev/null +++ b/apps/dreki_web/src/dreki_web_app.erl @@ -0,0 +1,68 @@ +%%%------------------------------------------------------------------- +%% @doc dreki_web public API +%% @end +%%%------------------------------------------------------------------- + +-module(dreki_web_app). + +-behaviour(application). + +-export([start/2, stop/1]). + +start(_StartType, _StartArgs) -> + Config = application:get_all_env(dreki_web), + Transport = proplists:get_value(transport, Config), + CowboyEnv = #{ + middlewares => [dreki_web_auth, cowboy_router, cowboy_handler], + stream_handlers => [cowboy_telemetry_h, cowboy_stream_h], + env => #{ + dispatch => routes() + } + }, + {ok, _} = cowboy:start_clear(dreki_web_listener, Transport, CowboyEnv), + opentelemetry_cowboy:setup(), + IP = proplists:get_value(ip, Transport), + Port = proplists:get_value(port, Transport), + logger:notice("dreki_web listening on ~p:~p", [IP, Port]), + dreki_web_sup:start_link(). + +stop(_State) -> + ok. + +%% internal functions + +routes() -> + Trails = [ + {"/", dreki_web_index, undefined}, + {"/static/[...]", cowboy_static, + {priv_dir, dreki_web, "static", [{mimetypes, dreki_web, detect_web_mimetype}]}}, + + %% API + {"/api/tasks/:id", dreki_web_task, undefined}, + + %% Admin API + {"/api/admin/world", dreki_web_admin_world, index}, + {"/api/admin/world/graph", dreki_web_admin_world, graph}, + {"/api/admin/world/graph.dot", dreki_web_admin_world, {graph, dot}}, + {"/api/admin/tasks", dreki_web_admin_tasks, undefined}, + {"/api/admin/tasks/:id", dreki_web_admin_task, undefined}, + + %% Admin UI + {"/admin", dreki_web_ui_index, undefined}, + {"/admin/nodes/:id", dreki_web_ui_node, undefined}, + {"/admin/tasks", dreki_web_ui_tasks, undefined}, + {"/admin/tasks/:id", dreki_web_ui_task, undefined}, + + %%{"/admin/stores", dreki_web_ui_stores, undefined}, + %%{"/admin/:location/stores", dreki_web_ui_stores, undefined}, + {"/admin/:location/:namespace", dreki_web_ui_stores, undefined}, + {"/admin/:location/:namespace/:directory", dreki_web_ui_stores, undefined}, + {"/admin/:location/:namespace/:directory/_/:action", dreki_web_ui_stores, action}, + {"/admin/:location/:namespace/:directory/:id", dreki_web_ui_stores, undefined}, + + {"/admin/[...]", dreki_web_ui_error, #{code => 404, status => <<"Not found">>}}, + + %% 404 Catch all + {'_', dreki_web_error, #{code => 404, status => <<"Not found">>}} + ], + trails:single_host_compile(Trails). |