Rust axum サンプル

Rust github: https://github.com/tokio-rs/axum

use axum:: {
	routing::{get, post},
	http::StatusCode,
	response::IntoResponse,
	Json, Router,
};
use serde::{Deserialize, Serialize};
use std::net::SocketAddr;

#[tokio::main]
async fn main() {
	tracing_subscriber::fmt::init();

	let app = Router::new()
		.route("/", get(root))
		.route("/users", post(create_user));

	let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
	axum::server(listener, app).await.unwrap();
}

async fn root() -> &'static str {
	"Hello, World"
}

async fn create_user(
	Json(payload): Json<CreateUser>,
) -> (StatusCode, Json<User>){
	let user = User {
		id: 1337,
		username: payload.username,
	};

	(StatusCode::CREATED, Json(user))
}

#[derive(Deserialize)]
struct CreateUser {
	username: String,
}

#[derive(Serialize)]
struct User {
	id: u64,
	username: String,
}

axum project
https://github.com/tokio-rs/axum/blob/main/ECOSYSTEM.md

Cargo.toml

[dependencies]
axum="0.5"
tokio = { version="1", features-["full"] }

main.rs

use axum::{routing::get, Router};

#[tokio::main]
async fn main(){
	let app = Router::new().route("/",  get(|| async {"hello world!"}));

	axum::Server::bind(&&"0.0.0.0:3000".parse().unwrap())
		.serve(app.into_make_service())
		.await
		.unwrap();
}

workspace Cargo.toml

[workspace]
members = [
	"hello-world"
]

$ cargo new hello-world

[dependencies]
axum = "0.5"
tokio = { version = "1", features = ["full"] }
use axum::{routing::get, Router};
use std::net::SocketAddr;

#[tokio::main]
async fn main() {
	let app = Router::new().route("/", get(handler));

	let addr = SocketAddr::from(([192,168,56,10], 3000));

	axum::Server::bind(&addr)
		.serve(app.into_make_service())
		.await
		.unwrap();
}

async fn handler() -> &'static str {
	"hello world!"
}

/workspace/generate-random-number

[dependencies]
axum = "0.5"
rand = "0.8"
serde = { version = "1", features = ["derive"]}
tokio = { version = "1", features = ["full"]}
use axum::{extract::Query, response::Html, routing::get, Router};
use rand::{thread_rng, Rng};
use serde::Deserialize;
use std::net::SocketAddr;

#[tokio::main]
async fn main() {
	let app = Router::new().route("/", get(handler));

	let addr = SocketAddr::from(([192,168,56,10], 3000));
	println!("listening on {}", addr);
	axum::Server::bind(&addr)
		.serve(app.into_make_service())
		.await
		.unwrap();
}

#[derive(Deserialize)]
struct RangeParameters {
	start: usize,
	end: usize,
}

async fn handler(Query(range): Query<RangeParameters>)-> Html<String> {

	let random_number = thread_rng().gen_range(range.start..range.end);

	Html(format!("<h1>Random Number: {}</h1>", random_number))
}

http://192.168.56.10:3000/?start=50&end=100

use axum::{response::Html, routing::get, Router};
use std::net::SocketAddr;

#[tokio::main]
async fn main() {
	let app = Router::new().route("/", get(handler));

	let addr = SocketAddr::from(([192,168,56,10], 3000));

	axum::Server::bind(&addr)
		.serve(app.into_make_service())
		.await
		.unwrap();
}

async fn handler() -> HTML<&'static str> {
	Html(include_str!("../index.html"))
}