1、安装 cargo-generate
bash
cargo install cargo-generate
2、创建一个极简空 crate
bash
cargo new my-rocket-template --bin
3、建一个自己的"项目骨架"仓库(只需做一次)
bash
my-rocket-template/
├── Cargo.toml # 把 rocket 等依赖先写好
├── Rocket.toml
├── src/
│ ├── main.rs
│ ├── lib.rs
│ ├── catchers/
│ ├── models/
│ ├── services/
│ ├── controllers/
│ ├── views/
│ ├── db/
│ ├── utils/
│ └── config/
├── templates/
├── static/
└── migrations/
各目录详细解释
| 目录 | 作用 | 设计理由 |
|---|---|---|
models/ |
定义数据结构(User、Post等) | 与数据库表一一对应,作为"数据载体" |
services/ |
实现业务逻辑(CRUD、验证等) | 控制器不直接操作数据库,保持"瘦控制器" |
controllers/ |
处理HTTP请求,调用服务,返回响应 | 只负责"接收请求 → 调用服务 → 返回结果" |
views/ |
构造模板渲染所需上下文 | 把复杂数据结构转成模板易用的格式 |
db/ |
管理数据库连接池 | 统一入口,避免重复建立连接 |
utils/ |
工具函数、错误类型定义 | 避免重复代码,统一错误处理风格 |
config/ |
读取配置文件(Rocket.toml + 环境变量) | 支持本地/测试/生产多环境切换 |
catchers/ |
处理HTTP错误 | 包含具体的错误处理函数 |
模板目录结构(templates)
bash
templates/
├── base.html.tera # 公共布局(导航栏、页脚)
├── index.html.tera # 首页
├── users/
│ ├── list.html.tera # 用户列表页
│ ├── detail.html.tera # 用户详情页
│ └── create.html.tera # 创建用户表单页
└── posts/
├── list.html.tera # 帖子列表页
├── detail.html.tera # 帖子详情页
└── create.html.tera # 创建帖子表单页
静态资源目录(static)
bash
static/
├── css/
│ └── style.css # 全局样式
└── js/
└── main.js # 全局脚本
✅ 静态资源路径在 Rocket.toml 中配置,Rocket 会自动映射为 /static/css/style.css 这类 URL。
数据库迁移目录(migrations)
bash
migrations/
└── 001_initial_schema.sql # 初始建表语句
✅ 使用纯 SQL 文件,方便 DBA 审查;也可接入 sqlx migrate 工具做版本管理。
配置文件(Rocket.toml)
| 配置块 | 作用 |
|---|---|
[default] |
默认配置(端口、模板路径、数据库URL) |
[debug] |
开发环境日志级别 |
[release] |
生产环境日志级别 |
[databases] |
数据库连接池配置 |
✅ 支持环境变量覆盖,例如 ROCKET_PORT=8080 cargo run
Cargo.toml源码
rust
[package]
name = "my-rocket-template"
version = "0.1.0"
edition = "2024"
[dependencies]
# 引入Rocket Web框架,版本0.5.1
# Rocket是一个用于Rust的快速、类型安全的Web框架
rocket = "0.5.1"
src/controllers/hello.rs
rust
// hello路由处理模块
// 该模块包含了处理hello相关路由的函数
// 从rocket框架导入get属性宏
// get宏用于声明HTTP GET请求的处理函数
use rocket::get;
// 声明index函数为处理根路径(/)的GET请求的处理函数
// #[get("/")]属性指定了该函数处理的路径和HTTP方法
// pub关键字表示该函数可以被外部模块访问
// 返回类型为String,表示返回纯文本响应
#[get("/")]
pub fn index() -> String {
// 返回"Hello, Rocket!"字符串作为响应
"Hello, Rocket!".to_string()
}
// 声明hello_name函数为处理/hello/<name>路径的GET请求的处理函数
// <name>是路径参数,会被解析为String类型并传递给函数
// pub关键字表示该函数可以被外部模块访问
// 返回类型为String,表示返回纯文本响应
#[get("/hello/<name>")]
pub fn hello_name(name: String) -> String {
// 使用format!宏将传入的name参数插入到响应字符串中
// 返回类似"Hello, Alice!"这样的字符串作为响应
format!("Hello, {}!", name)
}
src/controllers/mod.rs
rust
// controllers模块的入口文件
// 该文件声明了controllers模块下的子模块
// 声明hello子模块为公共模块
// hello模块包含了处理hello相关路由的函数
pub mod hello;
src/catchers/errors.rs
rust
// errors模块
// 该模块包含了处理各种HTTP错误的函数
// 从rocket框架导入必要的类型和宏
// Request: 表示HTTP请求的结构体
// catch: 用于声明错误处理函数的属性宏
use rocket::{Request, catch};
// 声明not_found函数为处理404 Not Found错误的函数
// #[catch(404)]属性指定了该函数处理的HTTP错误代码
// pub关键字表示该函数可以被外部模块访问
// 参数req是对Request对象的引用,包含了关于请求的信息
// 返回类型为String,表示返回纯文本响应
#[catch(404)]
pub fn not_found(req: &Request) -> String {
// 使用format!宏生成错误消息,包含了未找到的URI
// 返回类似"Sorry, '/unknown-path' was not found."这样的字符串作为响应
format!("Sorry, '{}' was not found.", req.uri())
}
src/catchers/mod.rs
rust
// catchers模块的入口文件
// 该文件声明了catchers模块下的子模块
// 声明errors子模块为公共模块
// errors模块包含了处理各种HTTP错误的函数
pub mod errors;
src/main.rs源码
rust
// 项目主文件入口
// 该文件负责启动Rocket Web服务器并配置路由和错误处理
// 从rocket框架导入必要的宏和函数
// launch: 标记主启动函数的属性宏
// routes: 用于声明路由列表的宏
// catchers: 用于声明错误处理函数列表的宏
use rocket::{launch, routes, catchers};
// 声明项目内部的controllers模块
// controllers模块包含所有处理HTTP请求的路由函数
mod controllers;
// 声明项目内部的catchers模块
// catchers模块包含所有处理HTTP错误的函数
mod catchers;
// 从controllers::hello子模块导入具体的路由处理函数
// index: 处理根路径(/)的请求
// hello_name: 处理/hello/<name>路径的请求
use controllers::hello::{index, hello_name};
// 从catchers::errors子模块导入404错误处理函数
// not_found: 处理404 Not Found错误
use catchers::errors::not_found;
// #[launch]属性标记该函数为Rocket应用的启动点
// Rocket框架会自动调用这个函数来启动服务器
#[launch]
fn rocket() -> _ {
// 创建并配置Rocket应用实例
rocket::build()
// 将路由函数挂载到根路径(/)下
// 这样,index函数处理http://localhost:8000/
// hello_name函数处理http://localhost:8000/hello/<name>
.mount("/", routes![index, hello_name])
// 注册错误处理函数
// 这里注册了404错误处理函数not_found
.register("/", catchers![not_found])
}
src/lib.rs源码
rust
// 项目库文件入口
// 该文件声明了项目的公共模块,允许外部代码访问这些模块
// 声明公共的controllers模块
// controllers模块包含了所有的路由处理函数
pub mod controllers;
// 声明公共的catchers模块
// catchers模块包含了错误处理函数
pub mod catchers;
运行代码
在项目根目录下执行:
rust
cargo run
运行不报错的话,如下:
bash
cargo run
Compiling my-rocket-template v0.1.0 (/mnt/mydata/myworks/my-rocket-template)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 11.53s
Running `target/debug/my-rocket-template`
🔧 Configured for debug.
>> address: 127.0.0.1
>> port: 8000
>> workers: 8
>> max blocking threads: 512
>> ident: Rocket
>> IP header: X-Real-IP
>> limits: bytes = 8KiB, data-form = 2MiB, file = 1MiB, form = 32KiB, json = 1MiB, msgpack = 1MiB, string = 8KiB
>> temp dir: /tmp
>> http/2: true
>> keep-alive: 5s
>> tls: disabled
>> shutdown: ctrlc = true, force = true, signals = [SIGTERM], grace = 2s, mercy = 3s
>> log level: normal
>> cli colors: true
📬 Routes:
>> (index) GET /
>> (hello_name) GET /hello/<name>
🥅 Catchers:
>> (not_found) 404
📡 Fairings:
>> Shield (liftoff, response, singleton)
🛡️ Shield:
>> X-Frame-Options: SAMEORIGIN
>> X-Content-Type-Options: nosniff
>> Permissions-Policy: interest-cohort=()
🚀 Rocket has launched from http://127.0.0.1:8000
浏览器访问:http://127.0.0.1:8000/ 显示:Hello, Rocket!
浏览器访问:http://127.0.0.1:8000/hello/任意内容 显示:Hello, 任意内容!
在GitHub 创建空仓库(不要勾选任何初始化文件)
1、登录 GitHub → New repository
2、名称填 rocket-web-app(或任意)
3、保持 Public 或 Private 均可,不要 选 Add a README / Add .gitignore / Add a license
4、创建后得到地址:
https://github.com/\<你的用户名>/rocket-web-app.git
本地项目初始化并提交
bash
cd rocket-web-app # 进入项目根目录
git init # 生成 .git
git add . # 跟踪所有文件(Cargo.toml、src、templates...)
git config --global user.name "你的名字"
git config --global user.email "你的邮箱"
git commit -m "Initial commit: Rocket MVC scaffold"
关联远程仓库并推送
bash
git branch -M main # 把默认分支命名成 main(GitHub 默认)
git remote add origin https://github.com/<你的用户名>/rocket-web-app.git
git push -u origin main # 第一次推送加 -u,以后直接 git push 即可
刷新 GitHub 页面,能看到完整目录结构即成功。
常见小问题速查
| 现象 | 解决 |
|---|---|
error: src refspec main does not match any |
先 git commit 再 push;或本地分支叫 master,就 git branch -M master + git push -u origin master |
| 403 拒绝推送 | 仓库是别人的 / 没权限;或没登录 GitHub CLI,用 gh auth login 再试 |
| 想改 SSH 方式 | git remote set-url origin git@github.com:<用户名>/rocket-web-app.git |
以后新建项目
以后新建项目就用这一行
bash
cargo generate --git https://github.com/nelou100-arch/my-rocket-template.git
输出:
bash
❯ cargo generate --git https://github.com/nelou100-arch/my-rocket-template.git
🤷 Project Name: rocket-web
🔧 Destination: /home/you/文档/myworks/rocket-web ...
🔧 project-name: rocket-web ...
🔧 Generating template ...
[ 1/42] Done: .gitignore [ 2/42] Done: .history/Cargo_20251216144100.toml [ 3/42] Done: .history/Cargo_20251216150200.toml [ 4/42] Done: .history/src/catchers/errors_20251216150434.rs [ 5/42] Done: .history/src/catchers/errors_20251216150440.rs [ 6/42] Done: .history/src/catchers/mod_20251216150422.rs [ 7/42] Done: .history/src/catchers/mod_20251216150427.rs [ 8/42] Done: .history/src/catchers [ 9/42] Done: .history/src/controllers/hello_20251216150326.rs [10/42] Done: .history/src/controllers/hello_20251216150332.rs [11/42] Done: .history/src/controllers/mod_20251216150311.rs [12/42] Done: .history/src/controllers/mod_20251216150319.rs [13/42] Done: .history/src/controllers/mod_20251216153144.rs [14/42] Done: .history/src/controllers/mod_20251216153149.rs [15/42] Done: .history/src/controllers [16/42] Done: .history/src/lib_20251216144155.rs [17/42] Done: .history/src/lib_20251216150233.rs [18/42] Done: .history/src/lib_20251216150502.rs [19/42] Done: .history/src/lib_20251216150506.rs [20/42] Done: .history/src/lib_20251216150517.rs [21/42] Done: .history/src/lib_20251216152827.rs [22/42] Done: .history/src/main_20251216144100.rs [23/42] Done: .history/src/main_20251216150224.rs [24/42] Done: .history/src/main_20251216150530.rs [25/42] Done: .history/src/main_20251216150536.rs [26/42] Done: .history/src/main_20251216152514.rs [27/42] Done: .history/src/main_20251216152517.rs [28/42] Done: .history/src/main_20251216152530.rs [29/42] Done: .history/src/main_20251216152714.rs [30/42] Done: .history/src [31/42] Done: .history [32/42] Done: Cargo.lock [33/42] Done: Cargo.toml [34/42] Done: src/catchers/errors.rs [35/42] Done: src/catchers/mod.rs [36/42] Done: src/catchers [37/42] Done: src/controllers/hello.rs [38/42] Done: src/controllers/mod.rs [39/42] Done: src/controllers [40/42] Done: src/lib.rs [41/42] Done: src/main.rs [42/42] Done: src 🔧 Moving generated files into: `/home/you/文档/myworks/rocket-web`...
🔧 Initializing a fresh Git repository
✨ Done! New project created /home/you/文档/myworks/rocket-web