Skip to content

Commit

Permalink
Add /docs endpoint to retrieve OpenAPI documentation (#713)
Browse files Browse the repository at this point in the history
  • Loading branch information
arun-koshy committed Mar 10, 2022
1 parent 18568c9 commit 30ada65
Showing 1 changed file with 39 additions and 4 deletions.
43 changes: 39 additions & 4 deletions sui/src/rest_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,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 @@ -66,11 +69,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 @@ -83,6 +87,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 @@ -96,8 +101,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 @@ -110,6 +116,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

1 comment on commit 30ada65

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

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

Bench results

�[0m�[0m�[1m�[32m Finished�[0m release [optimized] target(s) in 0.28s
�[0m�[0m�[1m�[32m Running�[0m target/release/bench
�[2m2022-03-10T09:14:22.159847Z�[0m �[32m INFO�[0m �[2mbench�[0m�[2m:�[0m Starting benchmark: TransactionsAndCerts
�[2m2022-03-10T09:14:22.159876Z�[0m �[32m INFO�[0m �[2mbench�[0m�[2m:�[0m Preparing accounts.
�[2m2022-03-10T09:14:22.160329Z�[0m �[32m INFO�[0m �[2mbench�[0m�[2m:�[0m Open database on path: "/tmp/DB_BCF5A31077BFF56AEB6C6B513C0302C7C6DA936D"
�[2m2022-03-10T09:14:27.440315Z�[0m �[32m INFO�[0m �[2mbench�[0m�[2m:�[0m Preparing transactions.
�[2m2022-03-10T09:14:36.018841Z�[0m �[32m INFO�[0m �[2msui_network::transport�[0m�[2m:�[0m Listening to TCP traffic on 127.0.0.1:9555
�[2m2022-03-10T09:14:37.020393Z�[0m �[32m INFO�[0m �[2mbench�[0m�[2m:�[0m Number of TCP connections: 2
�[2m2022-03-10T09:14:37.020426Z�[0m �[32m INFO�[0m �[2mbench�[0m�[2m:�[0m Set max_in_flight to 500
�[2m2022-03-10T09:14:37.020430Z�[0m �[32m INFO�[0m �[2mbench�[0m�[2m:�[0m Sending requests.
�[2m2022-03-10T09:14:37.026343Z�[0m �[32m INFO�[0m �[2msui_network::network�[0m�[2m:�[0m Sending TCP requests to 127.0.0.1:9555
�[2m2022-03-10T09:14:37.032019Z�[0m �[32m INFO�[0m �[2msui_network::network�[0m�[2m:�[0m Sending TCP requests to 127.0.0.1:9555
�[2m2022-03-10T09:14:37.924677Z�[0m �[32m INFO�[0m �[2msui_core::authority_server�[0m�[2m:�[0m 127.0.0.1:9555 has processed 5000 packets
�[2m2022-03-10T09:14:38.690559Z�[0m �[32m INFO�[0m �[2msui_network::network�[0m�[2m:�[0m In flight 500 Remaining 35000
�[2m2022-03-10T09:14:38.750867Z�[0m �[32m INFO�[0m �[2msui_network::network�[0m�[2m:�[0m In flight 500 Remaining 35000
�[2m2022-03-10T09:14:38.804791Z�[0m �[32m INFO�[0m �[2msui_core::authority_server�[0m�[2m:�[0m 127.0.0.1:9555 has processed 10000 packets
�[2m2022-03-10T09:14:39.688818Z�[0m �[32m INFO�[0m �[2msui_core::authority_server�[0m�[2m:�[0m 127.0.0.1:9555 has processed 15000 packets
�[2m2022-03-10T09:14:40.431746Z�[0m �[32m INFO�[0m �[2msui_network::network�[0m�[2m:�[0m In flight 500 Remaining 30000
�[2m2022-03-10T09:14:40.516500Z�[0m �[32m INFO�[0m �[2msui_network::network�[0m�[2m:�[0m In flight 500 Remaining 30000
�[2m2022-03-10T09:14:40.576606Z�[0m �[32m INFO�[0m �[2msui_core::authority_server�[0m�[2m:�[0m 127.0.0.1:9555 has processed 20000 packets
�[2m2022-03-10T09:14:41.467006Z�[0m �[32m INFO�[0m �[2msui_core::authority_server�[0m�[2m:�[0m 127.0.0.1:9555 has processed 25000 packets
�[2m2022-03-10T09:14:42.250357Z�[0m �[32m INFO�[0m �[2msui_network::network�[0m�[2m:�[0m In flight 500 Remaining 25000
�[2m2022-03-10T09:14:42.331122Z�[0m �[32m INFO�[0m �[2msui_network::network�[0m�[2m:�[0m In flight 500 Remaining 25000
�[2m2022-03-10T09:14:42.361470Z�[0m �[32m INFO�[0m �[2msui_core::authority_server�[0m�[2m:�[0m 127.0.0.1:9555 has processed 30000 packets
�[2m2022-03-10T09:14:43.255324Z�[0m �[32m INFO�[0m �[2msui_core::authority_server�[0m�[2m:�[0m 127.0.0.1:9555 has processed 35000 packets
�[2m2022-03-10T09:14:44.024845Z�[0m �[32m INFO�[0m �[2msui_network::network�[0m�[2m:�[0m In flight 500 Remaining 20000
�[2m2022-03-10T09:14:44.093797Z�[0m �[32m INFO�[0m �[2msui_network::network�[0m�[2m:�[0m In flight 500 Remaining 20000
�[2m2022-03-10T09:14:44.150345Z�[0m �[32m INFO�[0m �[2msui_core::authority_server�[0m�[2m:�[0m 127.0.0.1:9555 has processed 40000 packets
�[2m2022-03-10T09:14:45.040795Z�[0m �[32m INFO�[0m �[2msui_core::authority_server�[0m�[2m:�[0m 127.0.0.1:9555 has processed 45000 packets
�[2m2022-03-10T09:14:45.844395Z�[0m �[32m INFO�[0m �[2msui_network::network�[0m�[2m:�[0m In flight 500 Remaining 15000
�[2m2022-03-10T09:14:45.911299Z�[0m �[32m INFO�[0m �[2msui_network::network�[0m�[2m:�[0m In flight 500 Remaining 15000
�[2m2022-03-10T09:14:45.937968Z�[0m �[32m INFO�[0m �[2msui_core::authority_server�[0m�[2m:�[0m 127.0.0.1:9555 has processed 50000 packets
�[2m2022-03-10T09:14:46.832052Z�[0m �[32m INFO�[0m �[2msui_core::authority_server�[0m�[2m:�[0m 127.0.0.1:9555 has processed 55000 packets
�[2m2022-03-10T09:14:47.616257Z�[0m �[32m INFO�[0m �[2msui_network::network�[0m�[2m:�[0m In flight 500 Remaining 10000
�[2m2022-03-10T09:14:47.682645Z�[0m �[32m INFO�[0m �[2msui_network::network�[0m�[2m:�[0m In flight 500 Remaining 10000
�[2m2022-03-10T09:14:47.728420Z�[0m �[32m INFO�[0m �[2msui_core::authority_server�[0m�[2m:�[0m 127.0.0.1:9555 has processed 60000 packets
�[2m2022-03-10T09:14:48.622518Z�[0m �[32m INFO�[0m �[2msui_core::authority_server�[0m�[2m:�[0m 127.0.0.1:9555 has processed 65000 packets
�[2m2022-03-10T09:14:49.387822Z�[0m �[32m INFO�[0m �[2msui_network::network�[0m�[2m:�[0m In flight 500 Remaining 5000
�[2m2022-03-10T09:14:49.452659Z�[0m �[32m INFO�[0m �[2msui_network::network�[0m�[2m:�[0m In flight 500 Remaining 5000
�[2m2022-03-10T09:14:49.519106Z�[0m �[32m INFO�[0m �[2msui_core::authority_server�[0m�[2m:�[0m 127.0.0.1:9555 has processed 70000 packets
�[2m2022-03-10T09:14:50.411699Z�[0m �[32m INFO�[0m �[2msui_core::authority_server�[0m�[2m:�[0m 127.0.0.1:9555 has processed 75000 packets
�[2m2022-03-10T09:14:51.320290Z�[0m �[32m INFO�[0m �[2msui_core::authority_server�[0m�[2m:�[0m 127.0.0.1:9555 has processed 80000 packets
�[2m2022-03-10T09:14:51.338816Z�[0m �[32m INFO�[0m �[2msui_network::network�[0m�[2m:�[0m Done sending TCP requests to 127.0.0.1:9555
�[2m2022-03-10T09:14:51.371553Z�[0m �[32m INFO�[0m �[2msui_network::network�[0m�[2m:�[0m Done sending TCP requests to 127.0.0.1:9555
�[2m2022-03-10T09:14:51.373299Z�[0m �[32m INFO�[0m �[2mbench�[0m�[2m:�[0m Received 80000 responses.
�[2m2022-03-10T09:14:51.560757Z�[0m �[33m WARN�[0m �[2mbench�[0m�[2m:�[0m Completed benchmark for TransactionsAndCerts
Total time: 14352854us, items: 40000, tx/sec: 2786.902172905821

Please sign in to comment.