Skip to navigation
Create a Simple API in Rust using the Axum Framework
18.11.24
## Setup ``` mkdir simple-api-rust-axum cd simple-api-rust-axum cargo init ``` ## Add Libs ``` cargo add axum cargo add tokio -F full cargo add chrono -F serde cargo add serde -F derive cargo add serde_json cargo add uuid -F "v4 serde" cargo add tower-http -F "cors" ``` ## Cargo.toml ``` [package] name = "simple-api-rust-axum" version = "0.1.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] axum = "0.7.2" chrono = { version = "0.4.24", features = ["serde"] } serde = { version = "1.0.159", features = ["derive"] } serde_json = "1.0.95" tokio = { version = "1.26.0", features = ["full"] } tower-http = { version = "0.5.0", features = ["cors"] } uuid = { version = "1.3.0", features = ["v4","serde"] } ``` ## src/main.rs ``` use axum::{response::IntoResponse, routing::get, Json, Router}; async fn health_checker_handler() -> impl IntoResponse { const MESSAGE: &str = "Build Simple CRUD API in Rust using Axum"; let json_response = serde_json::json!({ "status": "success", "message": MESSAGE }); Json(json_response) } #[tokio::main] async fn main() { let app = Router::new().route("/api/healthchecker", get(health_checker_handler)); println!("Server started successfully"); let listener = tokio::net::TcpListener::bind("0.0.0.0:8000").await.unwrap(); axum::serve(listener, app).await.unwrap(); } ``` ## Run ``` cargo install cargo-watch cargo watch -q -c -w src/ -x run ``` ## Visit ``` http://localhost:8000/api/healthchecker ```
https://codevoweb.com/create-a-simple-api-in-rust-using-the-axum-framework/
Reply
Anonymous
Information Epoch 1787324682
Save trees.
Home
Notebook
Contact us