快速导览
让我们在几分钟内在 Loco 上创建一个博客后端。首先安装 `loco` 和 `sea-orm-cli`:
cargo install loco
cargo install sea-orm-cli
现在你可以创建你的新应用 (选择 "SaaS
app")。选择带有客户端渲染的 SaaS 应用:
❯ loco new
✔ ❯ App name? · myapp
✔ ❯ What would you like to build? · Saas App with client side rendering
✔ ❯ Select a DB Provider · Sqlite
✔ ❯ Select your background worker type · Async (in-process tokio async tasks)
🚂 Loco app generated successfully in:
myapp/
- assets: You've selected `clientside` for your asset serving configuration.
Next step, build your frontend:
$ cd frontend/
$ npm install && npm run build
你将拥有:
现在 cd
进入你的 myapp
目录,并通过运行 cargo loco start
启动你的应用:
如果你配置了客户端资源服务选项,请确保在启动服务器之前构建你的前端。这可以通过进入前端目录 (`cd frontend`) 并运行 `pnpm install` 和 `pnpm build` 来完成。
$ cargo loco start
▄ ▀
▀ ▄
▄ ▀ ▄ ▄ ▄▀
▄ ▀▄▄
▄ ▀ ▀ ▀▄▀█▄
▀█▄
▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄ ▀▀█
██████ █████ ███ █████ ███ █████ ███ ▀█
██████ █████ ███ █████ ▀▀▀ █████ ███ ▄█▄
██████ █████ ███ █████ █████ ███ ████▄
██████ █████ ███ █████ ▄▄▄ █████ ███ █████
██████ █████ ███ ████ ███ █████ ███ ████▀
▀▀▀██▄ ▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀ ██▀
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
https://loco.rs
listening on port 5150
你不必通过 `cargo` 运行,但在开发中强烈建议这样做。如果你使用 `--release` 构建,你的二进制文件将包含所有内容,包括你的代码,而不需要 `cargo` 或 Rust。
添加 CRUD API
我们已经生成了一个带有用户身份验证的基础 SaaS 应用。让我们通过使用 scaffold 添加一个 post 和一个完整的 CRUD API,将其变成一个博客后端:
你可以使用 `-api`、`--html` 和 `--htmx` 标志,在生成 `api`、`html` 或 `htmx` scaffold 之间进行选择。
因为我们正在为客户端构建一个带有客户端代码库的后端,我们将使用 --api
构建一个 API:
$ cargo loco generate scaffold post title:string content:text --api
:
:
added: "src/controllers/post.rs"
injected: "src/controllers/mod.rs"
injected: "src/app.rs"
added: "tests/requests/post.rs"
injected: "tests/requests/mod.rs"
* Migration for `post` added! You can now apply it with `$ cargo loco db migrate`.
* A test for model `posts` was added. Run with `cargo test`.
* Controller `post` was added successfully.
* Tests for controller `post` was added successfully. Run `cargo test`.
你的数据库已迁移,模型、实体和一个完整的 CRUD 控制器已自动生成。
再次启动你的应用:
$ cargo loco start
▄ ▀
▀ ▄
▄ ▀ ▄ ▄ ▄▀
▄ ▀▄▄
▄ ▀ ▀ ▀▄▀█▄
▀█▄
▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄ ▀▀█
██████ █████ ███ █████ ███ █████ ███ ▀█
██████ █████ ███ █████ ▀▀▀ █████ ███ ▄█▄
██████ █████ ███ █████ █████ ███ ████▄
██████ █████ ███ █████ ▄▄▄ █████ ███ █████
██████ █████ ███ ████ ███ █████ ███ ████▀
▀▀▀██▄ ▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀ ██▀
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
https://loco.rs
listening on port 5150
根据你选择的 scaffold 模板选项 (`-api`、`--html`、`--htmx`),创建 scaffold 资源的步骤将有所不同。使用 `--api` 标志或 `--htmx` 标志,你可以使用下面的例子。但是使用 `--html` 标志,建议你在浏览器中完成 post 创建步骤。
如果你想使用 curl
测试 --html
scaffold,你需要发送 Content-Type 为 application/x-www-form-urlencoded
的请求,并且默认情况下 body 为 title=Your+Title&content=Your+Content
。如果需要,可以更改代码以允许 application/json
作为 Content-Type
。
接下来,尝试使用 curl
添加一个 post:
$ curl -X POST -H "Content-Type: application/json" -d '{
"title": "Your Title",
"content": "Your Content xxx"
}' localhost:5150/api/posts
你可以列出你的 posts:
$ curl localhost:5150/api/posts
对于那些计数的人 -- 创建博客后端的命令是:
cargo install loco
cargo install sea-orm-cli
loco new
cargo loco generate scaffold post title:string content:text --api
完成! 享受你的 loco 之旅 🚂
检查 SaaS 身份验证
你生成的应用包含一个完全可用的身份验证套件,基于 JWT。
注册新用户
/api/auth/register
端点在数据库中创建一个新用户,并带有用于帐户验证的 email_verification_token
。一封包含验证链接的欢迎邮件将发送给用户。
$ curl --location 'localhost:5150/api/auth/register' \
--header 'Content-Type: application/json' \
--data-raw '{
"name": "Loco user",
"email": "user@loco.rs",
"password": "12341234"
}'
出于安全原因,如果用户已注册,则不会创建新用户,并且返回 200 状态,而不会暴露用户电子邮件详细信息。
登录
注册新用户后,使用以下请求登录:
$ curl --location 'localhost:5150/api/auth/login' \
--header 'Content-Type: application/json' \
--data-raw '{
"email": "user@loco.rs",
"password": "12341234"
}'
响应包括用于身份验证的 JWT token、用户 ID、姓名和验证状态。
{
"token": "...",
"pid": "2b20f998-b11e-4aeb-96d7-beca7671abda",
"name": "Loco user",
"claims": null
"is_verified": false
}
在你的客户端应用中,你保存此 JWT token,并使用 bearer token (见下文) 使用它进行后续请求,以便对这些请求进行身份验证。
获取当前用户
此端点受身份验证中间件保护。我们将使用我们之前获得的 token,使用 bearer token 技术执行请求 (将 TOKEN 替换为你之前获得的 JWT token):
$ curl --location --request GET 'localhost:5150/api/auth/current' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer TOKEN'
那应该是你的第一个经过身份验证的请求!
查看 controllers/auth.rs
的源代码,了解如何在自己的控制器中使用身份验证中间件。