From fc8d9c693641793712e7ab2e257d519a200859de Mon Sep 17 00:00:00 2001 From: JacobLinCool Date: Mon, 11 Sep 2023 16:18:17 +0800 Subject: [PATCH] feat: add openapi document into server --- openapi.yml | 6 ++++-- src/server/core.rs | 2 ++ src/server/jwt.rs | 1 - src/server/mod.rs | 1 + src/server/openapi.rs | 24 ++++++++++++++++++++++++ src/server/routes.rs | 17 +++++++++++++++++ 6 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 src/server/openapi.rs diff --git a/openapi.yml b/openapi.yml index fc154e6..f2cfca3 100644 --- a/openapi.yml +++ b/openapi.yml @@ -8,11 +8,13 @@ info: url: http://github.com/JacobLinCool email: jacob@csie.cool servers: - - url: https://{host}:{port} + - url: "{protocol}://{host}:{port}" description: The compilet server variables: + protocol: + default: https host: - default: compilet-alpha.csie.cool + default: localhost port: default: "443" paths: diff --git a/src/server/core.rs b/src/server/core.rs index 5d6d105..3fac8ca 100644 --- a/src/server/core.rs +++ b/src/server/core.rs @@ -35,6 +35,8 @@ pub fn rocket() -> Rocket { "/", routes![ routes::index, + routes::openapi_yml, + routes::openapi, routes::info, routes::system, routes::compile, diff --git a/src/server/jwt.rs b/src/server/jwt.rs index ddce72c..f8c3007 100644 --- a/src/server/jwt.rs +++ b/src/server/jwt.rs @@ -1,7 +1,6 @@ use crate::config::*; use jsonwebtoken::{decode, Algorithm, DecodingKey, Validation}; use rocket::request::{self, FromRequest, Request}; - use rocket::serde::{Deserialize, Serialize}; #[derive(Debug, Serialize, Deserialize)] diff --git a/src/server/mod.rs b/src/server/mod.rs index 719715e..668430f 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -2,6 +2,7 @@ pub mod compress; pub mod core; pub mod cors; pub mod jwt; +pub mod openapi; pub mod routes; pub mod system; pub mod version; diff --git a/src/server/openapi.rs b/src/server/openapi.rs new file mode 100644 index 0000000..e016b99 --- /dev/null +++ b/src/server/openapi.rs @@ -0,0 +1,24 @@ +use rocket::request::{self, FromRequest, Request}; + +pub const OPENAPI_DOCUMENT: &str = std::include_str!("../../openapi.yml"); + +pub struct SelfRoot(pub String); + +#[rocket::async_trait] +impl<'r> FromRequest<'r> for SelfRoot { + type Error = (); + + async fn from_request(request: &'r Request<'_>) -> request::Outcome { + let keys: Vec<_> = request.headers().get("Origin").collect(); + if keys.len() == 1 { + return request::Outcome::Success(SelfRoot(keys[0].to_string())); + } + + let keys: Vec<_> = request.headers().get("Host").collect(); + if keys.len() == 1 { + return request::Outcome::Success(SelfRoot(format!("http://{}", keys[0]))); + } + + request::Outcome::Failure((rocket::http::Status::Unauthorized, ())) + } +} diff --git a/src/server/routes.rs b/src/server/routes.rs index e3e7be7..bb8b2b2 100644 --- a/src/server/routes.rs +++ b/src/server/routes.rs @@ -1,11 +1,13 @@ use super::core::ServerInfo; use super::jwt::Token; +use super::openapi::SelfRoot; use super::system::{Status, SystemInfo}; use crate::compile::{CodeSubmission, CompileResult, WasmCache}; use crate::compilers::{get_compiler_for_language, get_compilers}; use crate::config::{auto_cleanup, no_cache}; use base64::engine::general_purpose; use base64::Engine; +use rocket::response::Redirect; use rocket::serde::json::Json; use rocket::State; use sha256::digest; @@ -23,6 +25,21 @@ pub fn index() -> &'static str { "I am Compilet. (https://github.com/wasm-oj/compilet)" } +#[get("/openapi.yml")] +pub fn openapi_yml() -> &'static str { + super::openapi::OPENAPI_DOCUMENT +} + +/// resolve the origin and redirect to https://api-spec.pages.dev/rapidoc?url= +#[get("/openapi")] +pub fn openapi(root: SelfRoot) -> Redirect { + let url = format!( + "https://api-spec.pages.dev/rapidoc?url={}/openapi.yml", + root.0 + ); + Redirect::to(url) +} + #[get("/info")] pub fn info() -> Json { Json(ServerInfo {