Rest API with Swagger UIλ︎
Create an API service that provides a documented and interactive API using Open API.
Axum is the web framework, supported by Tokio async runtime and Serde for JSON serialisation.
Create projectλ︎
Create a basic project using the Cargo tool.
Project configuration file
Dependenciesλ︎
Add library dependencies for Axium and other useful libraries
Updating Crates
LSP / Rustaceannvim will automatically detect newer versions of creates available.
SPC l a
to select the "Update creates" code action. Or select "Update all creates" action if there are multiple creates with newer versions.
Create a web serverλ︎
Use the Rust standard library to define IP address for the web server and manage errors.
Use axum to define a router and routes to handle requests.
Edit the src/main.rs
and replace the existing code
use std::{error::Error, net::SocketAddr};
use axum::{
routing::get,
Router,
};
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let addr = SocketAddr::from(([127, 0, 0, 1], 8000));
let app = Router::new().route("/", get(|| async { "Hello, Axum" }));
println!("listening on {}", addr);
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await
.unwrap();
Ok(())
}
Run and test web serverλ︎
Start the Rust project in watch mode, so changes are automatically updated on save.
NOTE: bacon has a long list of crate library dependencies... downloading the internet!
Bacon is the new cargo watch
Cargo-watch is no longer maintained, recommends bacon the background code checker instead.