Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refact: API 스키마 및 에러 핸들링 개선 #38

Merged
merged 13 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions Dockerfile.dev
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dockerfile.local 은 어때요? dev 는 약간 dev 환경에 대해 써야 되는 단어같아서요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Isolate builder
FROM debian:12.6-slim AS isolate-builder

# install isolate
RUN apt-get update && apt-get --no-install-recommends install -y \
git build-essential pkg-config libcap-dev
RUN git config --global http.sslVerify false
RUN git clone --depth 1 --branch v2.0 https://github.com/ioi/isolate.git /usr/src/isolate
WORKDIR /usr/src/isolate
RUN make isolate

# Dev image
# docker run -it --rm -v $PWD:/usr/src/hodu --privileged -p 8080:8080 hodu-dev
FROM rust:1.80.0-slim AS runner

# should run docker with `-v $PWD:/usr/src/hodu`
WORKDIR /usr/src/hodu
RUN cargo install cargo-watch

# install language specific tools
RUN apt-get update && apt-get --no-install-recommends install -y \
# language: c
gcc libc6-dev \
# language: c++
g++ \
# language: java
openjdk-17-jdk \
# language: python
python3 \
# language: javascript
nodejs \
# clean
&& rm -rf /var/lib/apt/lists/* \
&& apt-get clean

# copy bin
COPY --from=isolate-builder /usr/src/isolate/isolate /usr/local/bin/isolate
COPY --from=isolate-builder /usr/src/isolate/isolate-check-environment /usr/local/bin/isolate-check-environment
COPY --from=isolate-builder /usr/src/isolate/default.cf /usr/local/etc/isolate

ENTRYPOINT ["cargo", "watch", "-x", "run"]
1 change: 1 addition & 0 deletions hodu-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ serde_json = "1.0"
thiserror = "1.0.63"
tracing = "0.1"
tracing-subscriber = "0.3"
futures = "0.3"

[dependencies.uuid]
version = "1.10.0"
Expand Down
20 changes: 11 additions & 9 deletions hodu-server/src/api/judge/error.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
use actix_web::{http::StatusCode, HttpResponse, ResponseError};
use serde::Serialize;

#[derive(thiserror::Error, Debug)]
#[derive(thiserror::Error, Debug, Serialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE", tag = "detail")]
pub enum JudgeError {
#[error("PayloadParseError: {0}")]
PayloadParseError(#[source] actix_web::Error),
}

#[derive(Serialize, Debug)]
struct JudgeErrorResponse {
detail: String,
PayloadParseError(
#[source]
#[serde(skip)]
actix_web::Error,
),
#[error("HoduCoreError")]
HoduCoreError,
}

impl ResponseError for JudgeError {
fn status_code(&self) -> StatusCode {
match self {
JudgeError::PayloadParseError(_) => StatusCode::BAD_REQUEST,
JudgeError::HoduCoreError => StatusCode::INTERNAL_SERVER_ERROR,
}
}

fn error_response(&self) -> HttpResponse {
let status_code = self.status_code();
let detail = self.to_string();
let error_response = JudgeErrorResponse { detail };
let error_response = self;
HttpResponse::build(status_code).json(error_response)
}
}
100 changes: 55 additions & 45 deletions hodu-server/src/api/judge/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ impl Into<hodu_core::Language> for Language {
}
}

#[derive(Serialize)]
#[serde(remote = "hodu_core::MarkResultStatus")]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
enum MarkResponseStatus {
Correct,
Wrong,
CompileError,
RuntimeError,
TimeLimitExceeded,
MemoryLimitExceeded,
}

#[derive(Serialize)]
struct MarkResponseFields {
time: Option<f64>,
Expand All @@ -65,56 +77,54 @@ struct MarkResponseFields {
stderr: Option<String>,
}

impl MarkResponseFields {
pub fn from(result: &hodu_core::MarkResult, fields: &Vec<Field>) -> Self {
if fields.contains(&Field::WildCard) {
return MarkResponseFields {
time: Some(result.time),
memory: Some(result.memory),
stdout: Some(result.stdout.clone()),
stderr: Some(result.stderr.clone()),
};
} else {
MarkResponseFields {
time: if fields.contains(&Field::Time) {
Some(result.time)
} else {
None
},
memory: if fields.contains(&Field::Memory) {
Some(result.memory)
} else {
None
},
stdout: if fields.contains(&Field::Stdout) {
Some(result.stdout.clone())
} else {
None
},
stderr: if fields.contains(&Field::Stderr) {
Some(result.stderr.clone())
} else {
None
},
}
}
}
}

#[derive(Serialize)]
pub struct MarkResponse {
status: String,
pub struct MarkResponse<'a> {
#[serde(with = "MarkResponseStatus")]
status: &'a hodu_core::MarkResultStatus,
fields: MarkResponseFields,
}

impl MarkResponse {
pub fn new(result: &hodu_core::MarkResult, fields: &Vec<Field>) -> Self {
impl<'a> MarkResponse<'a> {
pub fn new(result: &'a hodu_core::MarkResult, fields: &Vec<Field>) -> Self {
MarkResponse {
status: match result.status {
hodu_core::MarkResultStatus::Correct => "correct".to_string(),
hodu_core::MarkResultStatus::Wrong => "wrong".to_string(),
hodu_core::MarkResultStatus::CompileError => "compile_error".to_string(),
hodu_core::MarkResultStatus::RuntimeError => "runtime_error".to_string(),
hodu_core::MarkResultStatus::TimeLimitExceeded => "time_limit_exceeded".to_string(),
hodu_core::MarkResultStatus::MemoryLimitExceeded => {
"memory_limit_exceeded".to_string()
}
},
fields: if fields.contains(&Field::WildCard) {
MarkResponseFields {
time: Some(result.time),
memory: Some(result.memory),
stdout: Some(result.stdout.clone()),
stderr: Some(result.stderr.clone()),
}
} else {
MarkResponseFields {
time: if fields.contains(&Field::Time) {
Some(result.time)
} else {
None
},
memory: if fields.contains(&Field::Memory) {
Some(result.memory)
} else {
None
},
stdout: if fields.contains(&Field::Stdout) {
Some(result.stdout.clone())
} else {
None
},
stderr: if fields.contains(&Field::Stderr) {
Some(result.stderr.clone())
} else {
None
},
}
},
status: &result.status,
fields: MarkResponseFields::from(result, fields),
}
}
}
Expand Down
12 changes: 9 additions & 3 deletions hodu-server/src/api/judge/view.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use std::panic::AssertUnwindSafe;

use actix_web::{post, web, Responder};
use hodu_core::{mark, MarkParams};

use futures::FutureExt;

use crate::api::judge::{
error::JudgeError,
schema::{CodeSubmission, MarkResponse},
Expand All @@ -17,15 +21,17 @@ async fn submit_code(
submission.language
);

let output = mark(MarkParams {
let output = AssertUnwindSafe(mark(MarkParams {
Copy link
Collaborator Author

@minkyu97 minkyu97 Aug 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

주의) 장기적으로는 futures::FutureExt::catch_unwindstd::panic::AssertUnwindSafe 를 걷어내고 hodu-core 의 응답을 Result 로 감싸는 것이 좋겠습니다.

language: &submission.language.clone().into(),
code: &submission.code,
expected_stdout: &submission.expected_stdout,
stdin: &submission.stdin,
memory_limit: submission.memory_limit,
time_limit: submission.time_limit,
})
.await;
}))
.catch_unwind()
.await
.map_err(|_| JudgeError::HoduCoreError)?;

Ok(web::Json(
serde_json::to_value(MarkResponse::new(&output, &submission.fields)).unwrap(),
Expand Down
Loading