Skip to content

Commit ada19c6

Browse files
mjcarsongabaker
authored andcommitted
fix(api): Fixed issue that required transparent rewrites for the UI
This resolves an issue where Thorium required transparent rewrites to serve the UI and API in a single application. Closes #41
1 parent 451e60c commit ada19c6

27 files changed

+224
-306
lines changed

api/src/lib.rs

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ async fn initial_settings_consistency_scan(
112112
Ok(())
113113
}
114114

115+
/// Set a fallback that returns a 404 to disable
116+
#[cfg(feature = "api")]
117+
async fn disable_fallback() -> http::StatusCode {
118+
http::StatusCode::NOT_FOUND
119+
}
120+
115121
#[cfg(feature = "api")]
116122
/// Build the axum app
117123
fn build_app(
@@ -137,24 +143,32 @@ fn build_app(
137143

138144
// build an axum router
139145
let mut app = axum::Router::new();
140-
app = basic::mount(app);
141-
app = binaries::mount(app, conf);
142-
app = docs::mount(app, conf);
143-
app = events::mount(app);
144-
app = files::mount(app);
145-
app = groups::mount(app);
146-
app = images::mount(app);
147-
app = jobs::mount(app);
148-
app = pipelines::mount(app);
149-
app = network_policies::mount(app);
150-
app = reactions::mount(app);
151-
app = repos::mount(app);
152-
app = search::mount(app);
153-
app = streams::mount(app);
154-
app = system::mount(app);
146+
// build a router for our api routes
147+
let mut api_router = axum::Router::new()
148+
// disable the fallback for api routes
149+
.fallback(disable_fallback);
150+
// add all of our api routes to our api router
151+
api_router = basic::mount(api_router);
152+
api_router = binaries::mount(api_router, conf);
153+
api_router = docs::mount(api_router, conf);
154+
api_router = events::mount(api_router);
155+
api_router = files::mount(api_router);
156+
api_router = groups::mount(api_router);
157+
api_router = images::mount(api_router);
158+
api_router = jobs::mount(api_router);
159+
api_router = pipelines::mount(api_router);
160+
api_router = network_policies::mount(api_router);
161+
api_router = reactions::mount(api_router);
162+
api_router = repos::mount(api_router);
163+
api_router = search::mount(api_router);
164+
api_router = streams::mount(api_router);
165+
api_router = system::mount(api_router);
166+
api_router = users::mount(api_router);
167+
api_router = trees::mount(api_router);
168+
// add our api routes
169+
app = app.nest("/api", api_router);
170+
// create a ui router and mount our ui routes then merge it
155171
app = ui::mount(app);
156-
app = users::mount(app);
157-
app = trees::mount(app);
158172
// setup our tracing
159173
let trace_provider = trace::setup("ThoriumAPI", &conf.thorium.tracing);
160174
// build cors middleware for our app

api/src/routes/basic.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
use crate::models::backends::system;
21
use crate::models::Version;
2+
use crate::models::backends::system;
33
use crate::utils::{ApiError, AppState};
4-
use axum::extract::Json;
5-
use axum::extract::State;
4+
use axum::Router;
5+
use axum::extract::{Json, State};
66
use axum::http::StatusCode;
77
use axum::routing::get;
8-
use axum::Router;
9-
use tracing::{event, instrument, Level};
8+
use tracing::{Level, event, instrument};
109
use utoipa::OpenApi;
1110

1211
use super::OpenApiSecurity;
@@ -121,8 +120,8 @@ async fn openapi() -> Json<utoipa::openapi::OpenApi> {
121120
// * `router` - The router to add routes too
122121
pub fn mount(router: Router<AppState>) -> Router<AppState> {
123122
router
124-
.route("/api/", get(identify))
125-
.route("/api/banner", get(banner))
126-
.route("/api/version", get(version))
127-
.route("/api/health", get(health))
123+
.route("/", get(identify))
124+
.route("/banner", get(banner))
125+
.route("/version", get(version))
126+
.route("/health", get(health))
128127
}

api/src/routes/binaries.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use axum::Router;
22
use tower_http::services::ServeDir;
33

4-
use crate::{utils::AppState, Conf};
4+
use crate::{Conf, utils::AppState};
55

66
/// Serve our binaries
77
///
@@ -21,5 +21,5 @@ fn user(conf: &Conf) -> ServeDir {
2121
///
2222
// * `router` - The router to add routes too
2323
pub fn mount(router: Router<AppState>, conf: &Conf) -> Router<AppState> {
24-
router.nest_service("/api/binaries", user(conf))
24+
router.nest_service("/binaries", user(conf))
2525
}

api/src/routes/docs.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
use axum::routing::{get_service, MethodRouter};
21
use axum::Router;
2+
use axum::routing::{MethodRouter, get_service};
33
use tower_http::services::{ServeDir, ServeFile};
44
use utoipa::openapi::security::{Http, HttpAuthScheme, SecurityScheme};
55
use utoipa::{Modify, OpenApi};
66
use utoipa_swagger_ui::SwaggerUi;
77

8+
use super::BasicApiDocs;
89
use super::events::EventApiDocs;
910
use super::files::FileApiDocs;
1011
use super::groups::GroupApiDocs;
@@ -14,15 +15,14 @@ use super::network_policies::NetworkPolicyDocs;
1415
use super::pipelines::PipelineApiDocs;
1516
use super::reactions::ReactionApiDocs;
1617
use super::repos::RepoApiDocs;
17-
use super::search::events::{ResultSearchEventApiDocs, TagSearchEventApiDocs};
1818
use super::search::SearchApiDocs;
19+
use super::search::events::{ResultSearchEventApiDocs, TagSearchEventApiDocs};
1920
use super::streams::StreamApiDocs;
2021
use super::system::SystemApiDocs;
2122
use super::users::UserApiDocs;
22-
use super::BasicApiDocs;
2323

2424
use crate::models::{ResultSearchEvent, SearchEvent, TagSearchEvent};
25-
use crate::{utils::AppState, Conf};
25+
use crate::{Conf, utils::AppState};
2626

2727
/// The struct containing our OpenAPI security info
2828
pub struct OpenApiSecurity;
@@ -73,34 +73,34 @@ fn dev(conf: &Conf) -> MethodRouter {
7373
// * `router` - The router to add routes too
7474
pub fn mount(router: Router<AppState>, conf: &Conf) -> Router<AppState> {
7575
router
76-
.nest_service("/api/docs/user", user(conf))
77-
.nest_service("/api/docs/dev", dev(conf))
76+
.nest_service("/docs/user", user(conf))
77+
.nest_service("/docs/dev", dev(conf))
7878
.merge(
79-
SwaggerUi::new("/api/docs/swagger-ui")
80-
.url("/api/openapi.json", BasicApiDocs::openapi())
81-
.url("/api/events/openapi.json", EventApiDocs::openapi())
82-
.url("/api/files/openapi.json", FileApiDocs::openapi())
83-
.url("/api/groups/openapi.json", GroupApiDocs::openapi())
84-
.url("/api/images/openapi.json", ImageApiDocs::openapi())
85-
.url("/api/jobs/openapi.json", JobApiDocs::openapi())
79+
SwaggerUi::new("/docs/swagger-ui")
80+
.url("/openapi.json", BasicApiDocs::openapi())
81+
.url("/events/openapi.json", EventApiDocs::openapi())
82+
.url("/files/openapi.json", FileApiDocs::openapi())
83+
.url("/groups/openapi.json", GroupApiDocs::openapi())
84+
.url("/images/openapi.json", ImageApiDocs::openapi())
85+
.url("/jobs/openapi.json", JobApiDocs::openapi())
8686
.url(
87-
"/api/networkpolicies/openapi.json",
87+
"/networkpolicies/openapi.json",
8888
NetworkPolicyDocs::openapi(),
8989
)
90-
.url("/api/pipelines/openapi.json", PipelineApiDocs::openapi())
91-
.url("/api/reactions/openapi.json", ReactionApiDocs::openapi())
92-
.url("/api/repos/openapi.json", RepoApiDocs::openapi())
93-
.url("/api/search/openapi.json", SearchApiDocs::openapi())
90+
.url("/pipelines/openapi.json", PipelineApiDocs::openapi())
91+
.url("/reactions/openapi.json", ReactionApiDocs::openapi())
92+
.url("/repos/openapi.json", RepoApiDocs::openapi())
93+
.url("/search/openapi.json", SearchApiDocs::openapi())
9494
.url(
95-
format!("/api/search/events/{}", ResultSearchEvent::url()),
95+
format!("/search/events/{}", ResultSearchEvent::url()),
9696
ResultSearchEventApiDocs::openapi(),
9797
)
9898
.url(
99-
format!("/api/search/events/{}", TagSearchEvent::url()),
99+
format!("/search/events/{}", TagSearchEvent::url()),
100100
TagSearchEventApiDocs::openapi(),
101101
)
102-
.url("/api/stream/openapi.json", StreamApiDocs::openapi())
103-
.url("/api/system/openapi.json", SystemApiDocs::openapi())
104-
.url("/api/users/openapi.json", UserApiDocs::openapi()),
102+
.url("/stream/openapi.json", StreamApiDocs::openapi())
103+
.url("/system/openapi.json", SystemApiDocs::openapi())
104+
.url("/users/openapi.json", UserApiDocs::openapi()),
105105
)
106106
}

api/src/routes/events.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! The routes supporting events in Thorium
2+
use axum::Router;
23
use axum::extract::{Json, Path, Query, State};
34
use axum::http::StatusCode;
45
use axum::routing::{delete, get, patch};
5-
use axum::Router;
66
use tracing::instrument;
77
use utoipa::OpenApi;
88

@@ -166,8 +166,8 @@ async fn openapi() -> Json<utoipa::openapi::OpenApi> {
166166
// * `router` - The router to add routes too
167167
pub fn mount(router: Router<AppState>) -> Router<AppState> {
168168
router
169-
.route("/api/events/pop/{kind}/", patch(pop))
170-
.route("/api/events/clear/{kind}/", delete(clear))
171-
.route("/api/events/reset/{kind}/", patch(reset_all))
172-
.route("/api/events/cache/status/", get(get_cache_status))
169+
.route("/events/pop/{kind}/", patch(pop))
170+
.route("/events/clear/{kind}/", delete(clear))
171+
.route("/events/reset/{kind}/", patch(reset_all))
172+
.route("/events/cache/status/", get(get_cache_status))
173173
}

api/src/routes/files.rs

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
//! The files related routes for Thorium
22
3+
use axum::Router;
34
use axum::extract::{Json, Multipart, Path, State};
45
use axum::http::StatusCode;
56
use axum::response::IntoResponse;
67
use axum::routing::{delete, get, patch, post};
7-
use axum::Router;
88
use axum_extra::body::AsyncReadBody;
99
use tracing::instrument;
1010
use utoipa::OpenApi;
@@ -687,33 +687,27 @@ async fn openapi() -> Json<utoipa::openapi::OpenApi> {
687687
/// * `router` - The router to add routes too
688688
pub fn mount(router: Router<AppState>) -> Router<AppState> {
689689
router
690-
.route("/api/files/", get(list).post(upload))
691-
.route("/api/files/details/", get(list_details))
692-
.route("/api/files/sample/{sha256}", get(get_sample))
693-
.route(
694-
"/api/files/sample/{sha256}/{submission}",
695-
delete(delete_sample),
696-
)
697-
.route("/api/files/exists", post(exists))
698-
.route("/api/files/sample/{sha256}/download", get(download))
699-
.route(
700-
"/api/files/sample/{sha256}/download/zip",
701-
get(download_as_zip),
702-
)
703-
.route("/api/files/sample/{sha256}", patch(update))
704-
.route("/api/files/tags/{sha256}", post(tag).delete(delete_tags))
705-
.route("/api/files/comment/{sha256}", post(create_comment))
706-
.route("/api/files/comment/{sha256}/{id}", delete(delete_comment))
690+
.route("/files/", get(list).post(upload))
691+
.route("/files/details/", get(list_details))
692+
.route("/files/sample/{sha256}", get(get_sample))
693+
.route("/files/sample/{sha256}/{submission}", delete(delete_sample))
694+
.route("/files/exists", post(exists))
695+
.route("/files/sample/{sha256}/download", get(download))
696+
.route("/files/sample/{sha256}/download/zip", get(download_as_zip))
697+
.route("/files/sample/{sha256}", patch(update))
698+
.route("/files/tags/{sha256}", post(tag).delete(delete_tags))
699+
.route("/files/comment/{sha256}", post(create_comment))
700+
.route("/files/comment/{sha256}/{id}", delete(delete_comment))
707701
.route(
708-
"/api/files/comment/download/{sha256}/{comment}/{name}",
702+
"/files/comment/download/{sha256}/{comment}/{name}",
709703
get(download_attachment),
710704
)
711705
.route(
712-
"/api/files/results/{sha256}",
706+
"/files/results/{sha256}",
713707
get(get_results).post(upload_results),
714708
)
715709
.route(
716-
"/api/files/result-files/{sha256}/{tool}/{result_id}",
710+
"/files/result-files/{sha256}/{tool}/{result_id}",
717711
get(download_result_file),
718712
)
719713
}

api/src/routes/groups.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
use axum::Router;
12
use axum::extract::{Json, Path, Query, State};
23
use axum::http::StatusCode;
34
use axum::routing::{get, patch, post};
4-
use axum::Router;
55
use tracing::instrument;
66

77
use utoipa::OpenApi;
@@ -304,10 +304,10 @@ async fn openapi() -> Json<utoipa::openapi::OpenApi> {
304304
// * `router` - The router to add routes too
305305
pub fn mount(router: Router<AppState>) -> Router<AppState> {
306306
router
307-
.route("/api/groups/", post(create).get(list))
308-
.route("/api/groups/{group}/details", get(get_group))
309-
.route("/api/groups/details/", get(list_details))
310-
.route("/api/groups/{group}", patch(update).delete(delete_group))
311-
.route("/api/groups/sync/ldap", post(sync_ldap))
312-
.route("/api/groups/{group}/stats", get(get_stats))
307+
.route("/groups/", post(create).get(list))
308+
.route("/groups/{group}/details", get(get_group))
309+
.route("/groups/details/", get(list_details))
310+
.route("/groups/{group}", patch(update).delete(delete_group))
311+
.route("/groups/sync/ldap", post(sync_ldap))
312+
.route("/groups/{group}/stats", get(get_stats))
313313
}

api/src/routes/images.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use super::shared;
22
use crate::is_admin;
3+
use axum::Router;
34
use axum::extract::{Json, Path, State};
45
use axum::http::StatusCode;
56
use axum::routing::{delete, get, patch, post};
6-
use axum::Router;
77
use tracing::instrument;
88
use utoipa::OpenApi;
99
use uuid::Uuid;
@@ -19,12 +19,12 @@ use crate::models::{
1919
FilesHandlerUpdate, Group, HostPath, HostPathTypes, Image, ImageArgs, ImageArgsUpdate,
2020
ImageBan, ImageBanKind, ImageBanUpdate, ImageDetailsList, ImageKey, ImageLifetime, ImageList,
2121
ImageListParams, ImageNetworkPolicyUpdate, ImageRequest, ImageScaler, ImageUpdate,
22-
ImageVersion, Kvm, KvmUpdate, KwargDependency, Notification, NotificationLevel,
22+
ImageVersion, Kvm, KvmUpdate, KwargDependency, NFS, Notification, NotificationLevel,
2323
NotificationParams, NotificationRequest, OutputCollection, OutputCollectionUpdate,
2424
OutputDisplayType, OutputHandler, RepoDependencySettings, Resources, ResourcesRequest,
2525
ResourcesUpdate, ResultDependencySettings, ResultDependencySettingsUpdate,
2626
SampleDependencySettings, Secret, SecurityContext, SecurityContextUpdate, SpawnLimits,
27-
TagDependencySettings, TagDependencySettingsUpdate, User, Volume, VolumeTypes, NFS,
27+
TagDependencySettings, TagDependencySettingsUpdate, User, Volume, VolumeTypes,
2828
};
2929
use crate::utils::{ApiError, AppState};
3030

@@ -422,21 +422,21 @@ async fn openapi() -> Json<utoipa::openapi::OpenApi> {
422422
// * `router` - The router to add routes too
423423
pub fn mount(router: Router<AppState>) -> Router<AppState> {
424424
router
425-
.route("/api/images/", post(create))
426-
.route("/api/images/data/{group}/{image}", get(get_image))
427-
.route("/api/images/{group}/", get(list))
428-
.route("/api/images/{group}/details/", get(list_details))
425+
.route("/images/", post(create))
426+
.route("/images/data/{group}/{image}", get(get_image))
427+
.route("/images/{group}/", get(list))
428+
.route("/images/{group}/details/", get(list_details))
429429
.route(
430-
"/api/images/{group}/{image}",
430+
"/images/{group}/{image}",
431431
patch(update).delete(delete_image),
432432
)
433-
.route("/api/images/runtimes/update", post(runtimes_update))
433+
.route("/images/runtimes/update", post(runtimes_update))
434434
.route(
435-
"/api/images/notifications/{group}/{image}",
435+
"/images/notifications/{group}/{image}",
436436
get(get_notifications).post(create_notification),
437437
)
438438
.route(
439-
"/api/images/notifications/{group}/{image}/{id}",
439+
"/images/notifications/{group}/{image}/{id}",
440440
delete(delete_notification),
441441
)
442442
}

api/src/routes/jobs.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
use axum::Router;
12
use axum::extract::{Json, Path, Query, State};
23
use axum::http::StatusCode;
34
use axum::response::IntoResponse;
45
use axum::response::Response;
56
use axum::routing::{get, patch, post};
6-
use axum::Router;
77
use tracing::instrument;
88
use utoipa::OpenApi;
99
use uuid::Uuid;
@@ -401,20 +401,20 @@ async fn openapi() -> Json<utoipa::openapi::OpenApi> {
401401
pub fn mount(router: Router<AppState>) -> Router<AppState> {
402402
router
403403
.route(
404-
"/api/jobs/claim/{group}/{pipeline}/{stage}/{cluster}/{node}/{worker}/{limit}",
404+
"/jobs/claim/{group}/{pipeline}/{stage}/{cluster}/{node}/{worker}/{limit}",
405405
patch(claim),
406406
)
407-
.route("/api/jobs/handle/{id}/proceed/{runtime}", post(proceed))
408-
.route("/api/jobs/handle/{id}/error", post(error))
409-
.route("/api/jobs/handle/{id}/sleep", post(sleep))
410-
.route("/api/jobs/handle/{id}/checkpoint", post(checkpoint))
411-
.route("/api/jobs/bulk/reset", post(bulk_reset))
407+
.route("/jobs/handle/{id}/proceed/{runtime}", post(proceed))
408+
.route("/jobs/handle/{id}/error", post(error))
409+
.route("/jobs/handle/{id}/sleep", post(sleep))
410+
.route("/jobs/handle/{id}/checkpoint", post(checkpoint))
411+
.route("/jobs/bulk/reset", post(bulk_reset))
412412
.route(
413-
"/api/jobs/deadlines/{scaler}/{start}/{end}",
413+
"/jobs/deadlines/{scaler}/{start}/{end}",
414414
get(read_deadlines),
415415
)
416416
.route(
417-
"/api/jobs/bulk/running/{scaler}/{start}/{end}",
417+
"/jobs/bulk/running/{scaler}/{start}/{end}",
418418
get(bulk_running),
419419
)
420420
}

0 commit comments

Comments
 (0)