Fix /api/runner/status

This commit is contained in:
Joscha 2023-08-10 23:31:46 +02:00
parent f3d646c8d5
commit 2079d0b12d
3 changed files with 19 additions and 10 deletions

View file

@ -12,6 +12,7 @@ use axum::{
};
use sqlx::SqlitePool;
use time::OffsetDateTime;
use tracing::debug;
use crate::{
config::Config,
@ -24,7 +25,7 @@ use crate::{
};
async fn post_status(
TypedHeader(auth): TypedHeader<Authorization<Basic>>,
auth: Option<TypedHeader<Authorization<Basic>>>,
State(config): State<&'static Config>,
State(db): State<SqlitePool>,
State(bench_repo): State<Option<BenchRepo>>,
@ -62,6 +63,8 @@ async fn post_status(
let abort_work = guard.should_abort_work(&name);
drop(guard);
// TODO Insert finished work into DB
// Find new work
let work = if let Some(hash) = work {
let bench = match bench_repo {
@ -78,14 +81,16 @@ async fn post_status(
None
};
debug!("Received status update from {name}");
Ok(Json(ServerResponse { work, abort_work }).into_response())
}
pub fn router(server: &Server) -> Router<Server> {
if server.repo.is_none() {
return Router::new().route("/api/runner/status", post(post_status));
return Router::new();
}
// TODO Add routes
Router::new()
// TODO Get repo tar
// TODO Get bench repo tar
Router::new().route("/api/runner/status", post(post_status))
}

View file

@ -3,6 +3,7 @@ use axum::{
headers::{authorization::Basic, Authorization},
http::{header, HeaderValue, StatusCode},
response::Response,
TypedHeader,
};
use crate::config::Config;
@ -23,10 +24,12 @@ fn is_password_valid(password: &str, config: &'static Config) -> bool {
pub fn authenticate(
config: &'static Config,
auth: Authorization<Basic>,
auth: Option<TypedHeader<Authorization<Basic>>>,
) -> Result<String, Response> {
if is_username_valid(auth.username()) && is_password_valid(auth.password(), config) {
return Ok(auth.username().to_string());
if let Some(auth) = auth {
if is_username_valid(auth.username()) && is_password_valid(auth.password(), config) {
return Ok(auth.username().to_string());
}
}
Err((

View file

@ -23,7 +23,7 @@ pub struct Measurement {
}
#[derive(Clone, Serialize, Deserialize)]
#[serde(rename = "snake_case")]
#[serde(rename_all = "snake_case")]
#[serde(tag = "type")]
pub enum Line {
Stdout(String),
@ -41,7 +41,7 @@ pub struct FinishedRun {
}
#[derive(Clone, Serialize, Deserialize)]
#[serde(rename = "snake_case")]
#[serde(rename_all = "snake_case")]
#[serde(tag = "type")]
pub enum RunnerStatus {
/// The runner is not performing any work.
@ -74,6 +74,7 @@ pub struct RunnerRequest {
///
/// If the server has a commit available, it should respond with a non-null
/// [`Response::work`].
#[serde(default)]
pub request_work: bool,
/// The runner has finished a run and wants to submit the results.
@ -81,7 +82,7 @@ pub struct RunnerRequest {
}
#[derive(Clone, Serialize, Deserialize)]
#[serde(rename = "snake_case")]
#[serde(rename_all = "snake_case")]
#[serde(tag = "type")]
pub enum BenchMethod {
/// Use internal (deterministic) benchmarking code.