Chuyển tới nội dung chính

Mô hình hóa miền.
Triển khai bằng Rust.

Riverbase là framework Rust cho backend HTTP theo hướng miền. Khai báo command, aggregate và query; nhận route có kiểu, OpenAPI, xác thực OIDC, policy Casbin, đọc dữ liệu an toàn theo tenant, và nhật ký kiểm toán được commit cùng mọi thay đổi.

cargo add riverbase_core riverbase_proc riverbase_http --git https://github.com/riverbase-io/riverbase
Đọc phần bắt đầu nhanhXem tài liệu

Rust 1.78+axum · Diesel · Postgres

nhật ký kiểm toán · riverbase.todochỉ ghi thêm
  1. 001030todo.updatedupdate-todotodo:2d9e0a47
  2. 001029todo.clearedclear-donetodo:*
  3. 001028todo.createdcreate-todotodo:2d9e0a47
  4. 001027todo.updatedupdate-todotodo:c04e5b18
  5. 001026todo.createdcreate-todotodo:c04e5b18
  6. 001025todo.updatedupdate-todotodo:7f3a91c2
  7. 001024todo.createdcreate-todotodo:7f3a91c2

Xây trên hệ sinh thái Rust bạn vốn tin dùng

  • axum
  • tokio
  • Diesel
  • diesel-async
  • PostgreSQL
  • serde
  • OpenAPI
  • JSON Schema
  • OpenID Connect
  • Keycloak
  • Casbin
  • ractor
  • NATS
  • Redis
  • OpenDAL
  • RxDB
  • SSE
  • WebSocket

riverbase_http::RiverbaseApp

Chỉ mount những gì bạn cần

Mỗi route tồn tại vì một dòng trong main.rs đặt nó ở đó. Tắt một dòng để xem dịch vụ ngừng phục vụ gì.

Đọc tài liệu
service/src/main.rs
app.serve().await?;
Dịch vụ phục vụ
  • GET/api/openapi.jsonTài liệu OpenAPI
  • POST/api/riverbase.todo/create-todo:post/todoCommand
  • POST/api/riverbase.todo/update-todo:exec/todo/{id}Command
  • GET/api/riverbase.todo/todo.listQuery: lọc, sắp xếp, phân trang
  • GET/api/riverbase.todo/todo.item/{id}Query: một dòng
  • GET/api/auth/infoAi đang gọi: OIDC, phiên, hồ sơ
  • POST/api/auth/switch-profile/{id}Chuyển hồ sơ đang dùng

7 route, mỗi route đứng sau xác thực và Casbin trừ khi được khai báo công khai

riverbase_core · riverbase_proc · riverbase_http

Xây một miền trong sáu tệp

Dịch vụ todo tham chiếu trong hướng dẫn, theo thứ tự bạn sẽ viết.

Đọc tài liệu
domain/src/postgres/schema.rs
use riverbase_core::diesel_table_with_domain_fields;
diesel_table_with_domain_fields! {
todo_examples.todo_items (_id) {
title -> Text,
done -> Bool,
notes -> Text,
version -> Int4,
}
}

riverbase_core::command

Chuyện gì xảy ra với một command

Một request, một transaction. Thay đổi trạng thái và nhật ký kiểm toán được commit cùng nhau, sau đó thông điệp tích hợp rời đi qua outbox. Kéo để xem lịch sử của một dòng bên dưới.

Đọc tài liệu
  1. Routecreate-todo:​postaxum giải mã {"payload": …} thành CreateTodoPayload. Dữ liệu sai dừng ở đây.
  2. Policycreate-todo.​executeNgười gọi được xác thực, rồi Casbin kiểm tra activity. Policy rỗng là từ chối.
  3. EngineCommandEngineMột actor ractor mở một transaction Postgres cho toàn bộ command.
  4. AggregateTodoAggregate::​create#[domain_action] ghi dòng dữ liệu và đặt tên sự kiện. Ghi bên ngoài nó sẽ lỗi.
  5. Kiểm toántodo.​createdEvent, activity, response và outbox được commit cùng trạng thái, hoặc không gì cả.
  6. Đọctodo.​listQuery engine trả về dòng dữ liệu, đã lọc theo tenant và scope policy.
Lịch sử của todo:7f3a91c2seq 001036 · 4 / 4 sự kiện
_id
7f3a91c2
title
null
done
null
notes
null
_etag
null
_updated
10:15:40

Trạng thái được lưu trữ, không phát lại. Mỗi sự kiện kiểm toán ghi lại dòng dữ liệu mà nó tạo ra, nên lịch sử cho thấy chính xác điều gì đã thay đổi và khi nào.

Có sẵn trong bộ khung

Những phần hầu hết backend phải tự dựng, đã nối vào cùng một miền.

Crate và feature

Có kiểu từ đầu đến cuối

Payload
Struct Rust sinh bộ giải mã, JSON Schema và OpenAPI từ một khai báo
riverbase_core
SQL
Diesel và diesel-async trên Postgres. Sai schema sẽ không biên dịch được.
riverbase_core

Truy cập

Danh tính
JWT Keycloak / OIDC, phiên, CSRF, hồ sơ; MockAuth khi phát triển
riverbase_http
Phân quyền
Casbin trên mọi route, mặc định từ chối; policy tenant và scope trên mọi dòng
riverbase_http

Nhất quán

Nhật ký kiểm toán
Các dòng command, event, activity và response được commit cùng thay đổi trạng thái
riverbase_core
Outbox
Thông điệp tích hợp gửi đi sau commit, ít nhất một lần, tới NATS, Redis hoặc Postgres
riverbase_core

Runtime

Đồng thời
Pool actor ractor cho command, semaphore cho đọc, một transaction cho mỗi command
riverbase_core
Chế độ triển khai
Tiến trình command/query gộp hoặc tách từ cùng một domain crate
riverbase_http

Mở rộng

Tiện ích mở rộng
Worker và theo dõi job trên bus, lưu trữ media trên OpenDAL, form khai báo
riverbase_*

Cargo workspace · 6 crates

Bắt đầu với ba crate. Thêm phần còn lại khi domain cần.

Tất cả crate

Feature flag

Mọi crate là git dependency. Chỉ bật những feature bạn dùng:

cargo add riverbase_http --git https://github.com/riverbase-io/riverbase
  • riverbase_core/nats-ioMessage bus NATS cho outbox và worker
  • riverbase_http/websocket,sse,rxdbThời gian thực: cầu nối WebSocket, SSE, đồng bộ RxDB
  • riverbase_file/aws-s3Lưu trữ media tương thích S3
  • riverbase_task/nats-ioWorker tiêu thụ subject NATS

Năm phút để có một API đang chạy

~/todo-app
1Thêm các crate
cargo add riverbase_core riverbase_proc riverbase_http --git https://github.com/riverbase-io/riverbase
Updating git repository `https://github.com/riverbase-io/riverbase`
Adding riverbase_core (git) to dependencies
Adding riverbase_http (git) to dependencies
2Khai báo domain
cat domain/src/domain.rs
riverbase_core::domain! {
domain TodoDomain {
meta { namespace: "riverbase.todo", title: "Todo", description: "…" }
command: TodoCommandEngine,
query: TodoQueryEngine,
spawn(runtime: &DomainRuntime) { /* … */ }
}
}
3Chạy
cargo run -p todo-service
Compiling todo-service v0.2.0
INFO database ready (framework + todo migrations applied)
INFO listening on 0.0.0.0:8080
curl -s -XPOST :8080/api/riverbase.todo/create-todo:post/todo -H 'content-type: application/json' -d '{"payload":{"title":"Ship it"}}'
{"id":"7f3a91c2-…","title":"Ship it","notes":""}

Mở /api/openapi.json để xem tài liệu API được sinh ra. Kết quả hiển thị chỉ mang tính minh họa.

Bắt đầu từ phần bắt đầu nhanh.