Rocket provides code that allows us to build web servers and web based applications such as web APIs.
We’ll start by just creating a simple endpoint, and then we’ll look at adding Open API and swagger support.
Starting simple
Create yourself a Rust package, for example with cargo
cargo new myapi --bin
add the following dependency to your Cargo.toml
rocket = "0.5.1"
Now create a Rocket.toml so we can configure rocket’s server and mine looks like this
[default] port = 8080 address = "127.0.0.1"
We need a main.rs (so you can delete the lib.rs if you wish for now) and here’s a very basic starting point
#[macro_use] extern crate rocket;
#[get("/")]
fn index() -> &'static str {
"Hello, world!"
}
#[launch]
fn rocket() -> _ {
rocket::build().mount("/", routes![index])
}
Now, run the application using
cargo run
As you can see this is a minimal API style, i.e. we create a function supplying it with an HTTP method and we add it to the routes list.
Adding the usual echo endpoint
Now let’s add my version of “Hello World” for API’s, a simple echo endpoint.
#[get("/echo?<text>")]
fn echo(text: &str) -> String {
format!("Echo: {}", text)
}
Now add this to the routes i.e.
#[launch]
fn rocket() -> _ {
rocket::build().mount("/", routes![index, echo])
}
Add Open API and Swagger
Now we have a couple of simple endpoints, let’s add Open API and Swagger and change the echo endpoint to use Json. I’m purposefully going to keep the index as non-Open API just to demonstrate running both Open API and non-Open API endpoints.
We’re going to need a few addition to our Cargo.toml – now, unfortunately it’s easy to get multiple version dependencies for these, so the one’s shown here will work together without warning/errors
[dependencies]
rocket = { version = "0.5.1", features = ["json"] }
openapi = "0.1.5"
serde = "1.0.219"
rocket_okapi = { version = "0.9.0", features = ["swagger"] }
schemars = "0.8.22"
Notice we’re adding features to the rocket crate and we’ve got some creates for swagger and open api. The schamars crate 0.8.22 was being used by other crates, hence I locked this down to the same version.
We’ll extend our echo endpoint to return Json, but before we do I’ll list the use clauses that are listed after #[macro_use] extern crate rocket;
use rocket::serde::{Serialize, json::Json};
use rocket_okapi::{openapi, openapi_get_routes};
use rocket_okapi::swagger_ui::{make_swagger_ui, SwaggerUIConfig};
use schemars::JsonSchema;
We’ll create a response object for the echo endpoint and update the echo endpoint to both return this and add the openapi attribute to allow this endpoint to have an open api spec generated for it
#[derive(Serialize, JsonSchema)]
struct EchoResponse {
message: String,
}
#[openapi]
#[get("/echo?<text>")]
fn echo(text: &str) -> Json<EchoResponse> {
Json(EchoResponse {
message: format!("Echo: {}", text),
})
}
Next up we need to change the rocket function, so let’s just see the latest version
#[launch]
fn rocket() -> _ {
rocket::build()
.mount("/", routes![index])
.mount("/", openapi_get_routes![echo])
.mount(
"/swagger",
make_swagger_ui(&SwaggerUIConfig {
url: "/openapi.json".to_owned(),
..Default::default()
})
)
}
Now I purposefully left the index route without an open api attribute just to demonstrate, if you have such endpoints, you need to still use the routes! macro, if you add index to openapi_get_routes! without the open api attribute you’ll get some slight ambiguous error’s such as a function with a similar name exists.
Now run your application and go to http://localhost:8080/swagger/index.html and you can interact with your endpoints via the Swagger UI you can also access the openapi.json file using http://localhost:8080/openapi.json.
Code
Available on GitHub.