新建路由库
创建一个route库
项目中跟路由相关的一些操作代码都放在这个库,方便后续进行管理。
cargo new --lib route
添加依赖
route/src/Cargo.toml
[package]
name = "route"
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.5"
tokio = {
version = "1.37.0", features = ["full"] }
调整路由相关代码
route/src/lib.rs
use axum::{
routing::get, Router};
pub fn route() -> Router {
Router::new().route("/", get(|| async {
"Hello, World!" }))
}
backend/Cargo.toml
workspace = {
members = ["route"] }
[package]
name = "axum-next-app"
version = "0.1.0"
edition = "2021"
authors = ["SunsJay <[email protected]>"]
keywords = ["axum", "web", "database"]
license = "MIT"
[[bin]]
name = "axum-next-app"
path = "main.rs"
[dependencies]
toml = "0.8"
serde = {
version = "1.0", features = ["derive"] }
axum = "0.7.5"
tokio = {
version = "1.37.0", features =