diff --git a/judge-server/src/service/mod.rs b/judge-server/src/service/mod.rs index 39417e2..e031fb6 100644 --- a/judge-server/src/service/mod.rs +++ b/judge-server/src/service/mod.rs @@ -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; @@ -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![ @@ -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(), + ), ]), ); } diff --git a/judge-server/src/service/state.rs b/judge-server/src/service/state.rs index 42c832d..a37b4b6 100644 --- a/judge-server/src/service/state.rs +++ b/judge-server/src/service/state.rs @@ -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, @@ -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 { + 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))) +}