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

Add docs endpoint to retrieve OpenAPI documentation #713

Merged
merged 2 commits into from
Mar 10, 2022
Merged
Changes from 1 commit
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
43 changes: 39 additions & 4 deletions sui/src/rest_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ async fn main() -> Result<(), String> {

let mut api = ApiDescription::new();

// [DOCS]
api.register(docs).unwrap();

// [DEBUG]
api.register(genesis).unwrap();
api.register(sui_start).unwrap();
Expand All @@ -62,11 +65,12 @@ async fn main() -> Result<(), String> {
api.register(call).unwrap();
api.register(sync).unwrap();

api.openapi("Sui API", "0.1")
.write(&mut std::io::stdout())
let documentation = api
.openapi("Sui API", "0.1")
.json()
.map_err(|e| e.to_string())?;

let api_context = ServerContext::new();
let api_context = ServerContext::new(documentation);

let server = HttpServerStarter::new(&config_dropshot, api, api_context, &log)
.map_err(|error| format!("failed to create server: {}", error))?
Expand All @@ -79,6 +83,7 @@ async fn main() -> Result<(), String> {
* Server context (state shared by handler functions)
*/
struct ServerContext {
documentation: serde_json::Value,
genesis_config_path: String,
wallet_config_path: String,
network_config_path: String,
Expand All @@ -92,8 +97,9 @@ struct ServerContext {
}

impl ServerContext {
pub fn new() -> ServerContext {
pub fn new(documentation: serde_json::Value) -> ServerContext {
ServerContext {
documentation,
genesis_config_path: String::from("genesis.conf"),
wallet_config_path: String::from("wallet.conf"),
network_config_path: String::from("./network.conf"),
Expand All @@ -106,6 +112,35 @@ impl ServerContext {
}
}

/**
Response containing the API documentation.
*/
#[derive(Deserialize, Serialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
struct DocumentationResponse {
/** A JSON object containing the OpenAPI definition for this API. */
documentation: serde_json::Value,
}

/**
Generate OpenAPI documentation.
*/
#[endpoint {
method = GET,
path = "/docs",
tags = [ "docs" ],
}]
async fn docs(
rqctx: Arc<RequestContext<ServerContext>>,
) -> Result<HttpResponseOk<DocumentationResponse>, HttpError> {
let server_context = rqctx.context();
let documentation = &server_context.documentation;

Ok(HttpResponseOk(DocumentationResponse {
documentation: documentation.clone(),
}))
}

/**
Request containing the server configuration.

Expand Down