| 1 |
|
-module(shurbej_http). |
| 2 |
|
-export([routes/0, web_dist_dir/0, web_dist_path/1]). |
| 3 |
|
|
| 4 |
|
%% Resolve once at route-compile time so the SPA handler doesn't recompute it |
| 5 |
|
%% per request. Falls back to "web/dist" relative to cwd for dev. |
| 6 |
|
web_dist_dir() -> |
| 7 |
2 |
case application:get_env(shurbej, web_dist_dir) of |
| 8 |
:-( |
{ok, Dir} -> to_list(Dir); |
| 9 |
2 |
undefined -> "web/dist" |
| 10 |
|
end. |
| 11 |
|
|
| 12 |
|
web_dist_path(Rel) -> |
| 13 |
1 |
filename:join(web_dist_dir(), Rel). |
| 14 |
|
|
| 15 |
:-( |
to_list(B) when is_binary(B) -> binary_to_list(B); |
| 16 |
:-( |
to_list(L) when is_list(L) -> L. |
| 17 |
|
|
| 18 |
|
routes() -> |
| 19 |
1 |
UserRoutes = library_routes("/users/:user_id"), |
| 20 |
1 |
GroupRoutes = library_routes("/groups/:group_id"), |
| 21 |
1 |
AssetsDir = filename:join(web_dist_dir(), "assets"), |
| 22 |
|
[ |
| 23 |
|
%% Auth — session-based login flow |
| 24 |
|
{"/keys", shurbej_http_keys, #{action => create_key}}, |
| 25 |
|
{"/keys/current", shurbej_http_keys, #{action => current}}, |
| 26 |
|
{"/keys/sessions", shurbej_http_keys, #{action => sessions}}, |
| 27 |
|
{"/keys/sessions/:token", shurbej_http_keys, #{action => session}}, |
| 28 |
|
{"/keys/:key", shurbej_http_keys, #{action => by_key}}, |
| 29 |
|
|
| 30 |
|
%% Login page (browser-facing) |
| 31 |
|
{"/login", shurbej_http_login, #{}}, |
| 32 |
|
|
| 33 |
|
%% JSON login for web UI |
| 34 |
|
{"/auth/login", shurbej_http_auth, #{}}, |
| 35 |
|
|
| 36 |
|
%% WebSocket for login session notifications |
| 37 |
|
{"/ws", shurbej_ws_login, #{}}, |
| 38 |
|
|
| 39 |
|
%% Streaming API (Zotero-compatible topicUpdated push) |
| 40 |
|
{"/stream", shurbej_ws_stream, #{}}, |
| 41 |
|
|
| 42 |
|
%% Item template |
| 43 |
|
{"/items/new", shurbej_http_item_template, #{}}, |
| 44 |
|
|
| 45 |
|
%% Item type metadata |
| 46 |
|
{"/itemTypes", shurbej_http_meta, #{action => item_types}}, |
| 47 |
|
{"/itemFields", shurbej_http_meta, #{action => item_fields}}, |
| 48 |
|
{"/itemTypeFields", shurbej_http_meta, #{action => item_type_fields}}, |
| 49 |
|
{"/itemTypeCreatorTypes", shurbej_http_meta, #{action => item_type_creator_types}}, |
| 50 |
|
{"/creatorFields", shurbej_http_meta, #{action => creator_fields}}, |
| 51 |
|
|
| 52 |
|
%% Group listing (per-user) + group metadata |
| 53 |
|
{"/users/:user_id/groups", shurbej_http_groups, #{}}, |
| 54 |
|
{"/groups/:group_id", shurbej_http_group, #{}} |
| 55 |
|
] |
| 56 |
1 |
++ UserRoutes |
| 57 |
|
++ GroupRoutes |
| 58 |
|
++ [ |
| 59 |
|
%% File upload endpoint |
| 60 |
|
{"/upload/:upload_key", shurbej_http_upload, #{}}, |
| 61 |
|
|
| 62 |
|
%% Schema |
| 63 |
|
{"/schema", shurbej_http_schema, #{}}, |
| 64 |
|
|
| 65 |
|
%% Retractions (stub — Zotero checks this for retracted papers) |
| 66 |
|
{"/retractions/list", shurbej_http_stub, #{body => []}}, |
| 67 |
|
|
| 68 |
|
%% Web UI (built SPA — catch-all, must be last) |
| 69 |
|
{"/assets/[...]", cowboy_static, {dir, AssetsDir}}, |
| 70 |
|
{"/[...]", shurbej_http_spa, #{}} |
| 71 |
|
]. |
| 72 |
|
|
| 73 |
|
%% Produce the library-scoped route set for a given path prefix. |
| 74 |
|
%% Used for both /users/:user_id and /groups/:group_id. |
| 75 |
|
library_routes(Prefix) -> |
| 76 |
2 |
[ |
| 77 |
|
%% Items |
| 78 |
|
{Prefix ++ "/items", shurbej_http_items, #{scope => all}}, |
| 79 |
|
{Prefix ++ "/items/top", shurbej_http_items, #{scope => top}}, |
| 80 |
|
{Prefix ++ "/items/trash", shurbej_http_items, #{scope => trash}}, |
| 81 |
|
{Prefix ++ "/items/:item_key", shurbej_http_items, #{scope => single}}, |
| 82 |
|
{Prefix ++ "/items/:item_key/children", shurbej_http_items, #{scope => children}}, |
| 83 |
|
{Prefix ++ "/items/:item_key/tags", shurbej_http_tags, #{scope => item_tags}}, |
| 84 |
|
|
| 85 |
|
%% Collections |
| 86 |
|
{Prefix ++ "/collections", shurbej_http_collections, #{scope => all}}, |
| 87 |
|
{Prefix ++ "/collections/top", shurbej_http_collections, #{scope => top}}, |
| 88 |
|
{Prefix ++ "/collections/:coll_key", shurbej_http_collections, #{scope => single}}, |
| 89 |
|
{Prefix ++ "/collections/:coll_key/collections", shurbej_http_collections, #{scope => subcollections}}, |
| 90 |
|
{Prefix ++ "/collections/:coll_key/items", shurbej_http_items, #{scope => collection}}, |
| 91 |
|
{Prefix ++ "/collections/:coll_key/items/top", shurbej_http_items, #{scope => collection_top}}, |
| 92 |
|
|
| 93 |
|
%% Searches |
| 94 |
|
{Prefix ++ "/searches", shurbej_http_searches, #{scope => all}}, |
| 95 |
|
{Prefix ++ "/searches/:search_key", shurbej_http_searches, #{scope => single}}, |
| 96 |
|
|
| 97 |
|
%% Tags |
| 98 |
|
{Prefix ++ "/tags", shurbej_http_tags, #{scope => all}}, |
| 99 |
|
|
| 100 |
|
%% Settings |
| 101 |
|
{Prefix ++ "/settings", shurbej_http_settings, #{scope => all}}, |
| 102 |
|
{Prefix ++ "/settings/:setting_key", shurbej_http_settings, #{scope => single}}, |
| 103 |
|
|
| 104 |
|
%% Deleted |
| 105 |
|
{Prefix ++ "/deleted", shurbej_http_deleted, #{}}, |
| 106 |
|
|
| 107 |
|
%% Full-text |
| 108 |
|
{Prefix ++ "/fulltext", shurbej_http_fulltext, #{scope => versions}}, |
| 109 |
|
{Prefix ++ "/items/:item_key/fulltext", shurbej_http_fulltext, #{scope => single}}, |
| 110 |
|
|
| 111 |
|
%% Files |
| 112 |
|
{Prefix ++ "/items/:item_key/file", shurbej_http_files, #{}}, |
| 113 |
|
{Prefix ++ "/items/:item_key/file/view", shurbej_http_files, #{action => view}}, |
| 114 |
|
{Prefix ++ "/items/:item_key/file/view/url", shurbej_http_files, #{action => view_url}} |
| 115 |
|
]. |