Skip to content

Commit

Permalink
feat: add openapi document into server
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobLinCool committed Sep 11, 2023
1 parent 1162b8a commit fc8d9c6
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 3 deletions.
6 changes: 4 additions & 2 deletions openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ info:
url: http://github.com/JacobLinCool
email: [email protected]
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:
Expand Down
2 changes: 2 additions & 0 deletions src/server/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ pub fn rocket() -> Rocket<Build> {
"/",
routes![
routes::index,
routes::openapi_yml,
routes::openapi,
routes::info,
routes::system,
routes::compile,
Expand Down
1 change: 0 additions & 1 deletion src/server/jwt.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
1 change: 1 addition & 0 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
24 changes: 24 additions & 0 deletions src/server/openapi.rs
Original file line number Diff line number Diff line change
@@ -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<Self, Self::Error> {
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, ()))
}
}
17 changes: 17 additions & 0 deletions src/server/routes.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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=<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<ServerInfo> {
Json(ServerInfo {
Expand Down

0 comments on commit fc8d9c6

Please sign in to comment.