36 lines
1.0 KiB
Rust
36 lines
1.0 KiB
Rust
use axum::{Router, routing::get, Json};
|
|
use serde::Serialize;
|
|
use std::net::SocketAddr;
|
|
use tokio::net::TcpListener;
|
|
#[derive(Serialize)]
|
|
struct Variables {
|
|
user_name: String,
|
|
age: i32,
|
|
is_student: bool,
|
|
score: f32,
|
|
}
|
|
async fn display_variables() -> Json<Variables> {
|
|
// Declare and assign variables of different types
|
|
let user_name = String::from("Alice");
|
|
let age = 25;
|
|
let is_student = true;
|
|
let score = 95.5;
|
|
// Return variables as JSON
|
|
Json(Variables {
|
|
user_name,
|
|
age,
|
|
is_student,
|
|
score,
|
|
})
|
|
}
|
|
#[tokio::main]
|
|
async fn main() {
|
|
// Create Axum router with a single route
|
|
let router = Router::new().route("/variables", get(display_variables));
|
|
// Set up server address and port
|
|
let addr = SocketAddr::from(([0, 0, 0, 0], 8080));
|
|
let tcp = TcpListener::bind(&addr).await.unwrap();
|
|
println!("Ready and listening on {}", addr);
|
|
// Start the server
|
|
axum::serve(tcp, router).await.unwrap();
|
|
} |