Skip to content

Commit

Permalink
Support querying judge state
Browse files Browse the repository at this point in the history
  • Loading branch information
slhmy committed Oct 17, 2023
1 parent 5a07971 commit f5aa334
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
8 changes: 6 additions & 2 deletions judge-server/src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use utoipa::OpenApi;
#[derive(utoipa::OpenApi)]
#[openapi(external_docs(
url = "/swagger-ui/?urls.primaryName=judge",
description = "Start judge"
description = "Judger API docs",
))]
pub struct ApiDoc;

Expand All @@ -17,7 +17,7 @@ pub fn route(cfg: &mut web::ServiceConfig) {
web::scope("/api/v1")
.configure(judge::route)
.service(greet::greet)
.service(judge::run_judge),
.configure(state::route),
)
.service(
utoipa_swagger_ui::SwaggerUi::new("/swagger-ui/{_:.*}").urls(vec![
Expand All @@ -29,6 +29,10 @@ pub fn route(cfg: &mut web::ServiceConfig) {
utoipa_swagger_ui::Url::new("judge", "/api-docs/judge.json"),
judge::JudgeApiDoc::openapi(),
),
(
utoipa_swagger_ui::Url::new("state", "/api-docs/state.json"),
state::StateApiDoc::openapi(),
),
]),
);
}
27 changes: 27 additions & 0 deletions judge-server/src/service/state.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use std::sync::RwLock;

use actix_web::{get, web, HttpResponse};
use lazy_static::lazy_static;

use crate::error::ServiceError;

#[derive(Clone, Debug, PartialEq)]
pub enum State {
Idle,
Expand Down Expand Up @@ -30,3 +33,27 @@ pub fn set_idle() {
let mut state = STATE.write().unwrap();
*state = State::Idle;
}

#[derive(utoipa::OpenApi)]
#[openapi(paths(get_state))]
pub struct StateApiDoc;

pub fn route(cfg: &mut web::ServiceConfig) {
cfg.service(web::scope("/state").service(get_state));
}

#[utoipa::path(
context_path = "/api/v1/state",
responses(
(status = 200, description = "Judge run successfully")
)
)]
#[get("")]
pub async fn get_state() -> Result<HttpResponse, ServiceError> {
let state = STATE.read().map_err(|e| {
ServiceError::InternalError(anyhow::anyhow!("Failed to lock state: {:?}", e))
})?;
Ok(HttpResponse::Ok().content_type(
"application/text; charset=utf-8",
).body(format!("{:?}", *state)))
}

0 comments on commit f5aa334

Please sign in to comment.