Skip to content
This repository was archived by the owner on Mar 20, 2023. It is now read-only.

Commit d810741

Browse files
definitelynobodyrvolosatovs
authored andcommitted
fix: add some redirects to root_post
- If a user already has a job running, just redirect to it. - If too many workloads are running, show an error. Signed-off-by: Nicholas Farshidmehr <nicholas@profian.com>
1 parent b382e33 commit d810741

File tree

3 files changed

+24
-29
lines changed

3 files changed

+24
-29
lines changed

src/main.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
mod auth;
88
mod jobs;
9+
mod redirect;
910
mod reference;
1011
mod secret;
1112
mod templates;
@@ -20,7 +21,7 @@ use std::time::Duration;
2021

2122
use axum::extract::Multipart;
2223
use axum::http::StatusCode;
23-
use axum::response::IntoResponse;
24+
use axum::response::{IntoResponse, Redirect};
2425
use axum::routing::{get, post};
2526
use axum::{Router, Server};
2627

@@ -257,8 +258,13 @@ async fn root_post(
257258
jobs: usize,
258259
) -> impl IntoResponse {
259260
let (ttl, size) = limits.decide(user.read().await.is_starred("enarx/enarx").await);
260-
if user.read().await.data.job.is_some() || jobs::Job::count() >= jobs {
261-
return Err(StatusCode::TOO_MANY_REQUESTS.into_response());
261+
262+
if user.read().await.data.job.is_some() {
263+
return Err(Redirect::to("/").into_response());
264+
}
265+
266+
if jobs::Job::count() >= jobs {
267+
return Err(redirect::too_many_workloads().into_response());
262268
}
263269

264270
let mut wasm = None;
@@ -341,8 +347,12 @@ async fn root_post(
341347
let uuid = {
342348
let mut lock = user.write().await;
343349

344-
if lock.data.job.is_some() || jobs::Job::count() >= jobs {
345-
return Err(StatusCode::TOO_MANY_REQUESTS.into_response());
350+
if lock.data.job.is_some() {
351+
return Err(Redirect::to("/").into_response());
352+
}
353+
354+
if jobs::Job::count() >= jobs {
355+
return Err(redirect::too_many_workloads().into_response());
346356
}
347357

348358
let job = jobs::Job::new(command, wasm, toml).map_err(|e| {

src/redirect.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// SPDX-FileCopyrightText: 2022 Profian Inc. <opensource@profian.com>
2+
// SPDX-License-Identifier: AGPL-3.0-only
3+
4+
use axum::response::Redirect;
5+
6+
/// The server is already running too many jobs.
7+
pub fn too_many_workloads() -> Redirect {
8+
Redirect::to("/?message=too_many_workloads")
9+
}

templates/idx.html

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -131,34 +131,10 @@
131131

132132
// SECURITY: Do not put query parameter values in messageHTML.
133133
switch (messageCode) {
134-
case 'no_session': {
135-
if (!authenticated) {
136-
messageHTML.push("It looks like you have been logged out.");
137-
}
138-
break;
139-
}
140-
case 'workload_not_found': {
141-
if (authenticated) {
142-
messageHTML.push("The workload was not found or you are not permitted to view it.");
143-
} else {
144-
messageHTML.push("You need to be logged in to view workloads.");
145-
}
146-
break;
147-
}
148134
case 'too_many_workloads': {
149135
messageHTML.push("Too many workloads are running on this server! Please try again later.");
150136
break;
151137
}
152-
case 'workload_killed': {
153-
// Users probably don't care if the workload was killed.
154-
// This is just here for testing.
155-
// messageHTML.push("Workload killed successfully.");
156-
break;
157-
}
158-
case 'internal_error': {
159-
messageHTML.push("An internal error occurred handling your request. Please try again or contact an administrator.");
160-
break;
161-
}
162138
default: {
163139
break;
164140
}

0 commit comments

Comments
 (0)