Skip to content

Commit

Permalink
feat: api-example 实现数据访问逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
yangjing committed Aug 15, 2024
1 parent f5162da commit 2b0eff7
Show file tree
Hide file tree
Showing 17 changed files with 60 additions and 136 deletions.
2 changes: 1 addition & 1 deletion .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 设置运行 cargo 时的系统环境变量
[env]
RUST_LOG = "info,ultimate=debug,ultimate_common=debug"
RUST_LOG = "info,tower_http=debug,ultimate=debug,ultimate_common=debug"

[alias]
clippy-all = "clippy --all-features -- -D warnings"
Expand Down
10 changes: 4 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
[workspace]
members = [
"ultimates/*",
"crates/*",
"examples/db-example",
"examples/api-example",
]
members = ["ultimates/*", "crates/*", "examples/api-example"]
resolver = "2"

[workspace.package]
Expand All @@ -30,6 +25,9 @@ ultimate = { version = "0.1", path = "ultimates/ultimate" }
ultimate-db = { version = "0.1", path = "ultimates/ultimate-db" }
ultimate-web = { version = "0.1", path = "ultimates/ultimate-web" }
# -- projects end
# begin -- memory allocator
tikv-jemallocator = "0.6"
# end -- memory allocator
derive_more = { version = "1.0", features = ["from", "display", "constructor"] }
toml = "0.8"
config = { version = "0.14", default-features = false, features = [
Expand Down
30 changes: 27 additions & 3 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Examples

```sh
git clone https://github.com/yangbajing/ultimate-common
cd ultimate-common/examples
```

## 服务依赖

使用 docker compose 启动并初始化 PG 数据库
Expand All @@ -8,15 +13,17 @@
# 当前目录下有容器,先删除容器及数据卷(一般在 sql 有变更时需要)
#docker compose down --volumes --remove-orphans

docker compose up -d --build
docker compose up -d --build && docker compose logs -f
```

## api-example

### 启动服务

重新打开一个终端窗口并进入 `examples/` 目录,执行以下命令运行程序

```sh
cargo run --bin api-example
cargo run --release --bin api-example
```

### 测试服务
Expand All @@ -43,9 +50,26 @@ curl -v --location 'http://localhost:8888/auth/login/pwd' \

#### 用户-分页查询

_你需要使用上面 [使用密码登录](#使用密码登录) 来获取 token,并替换到下面 `Bearer <token>` 位置。_

```sh
curl -v --location 'http://localhost:8888/v1/user/page' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer eyJ0eXAiOiJKV1QiLCJlbmMiOiJBMTI4Q0JDLUhTMjU2IiwiYWxnIjoiZGlyIn0..EZwETCBq1CNs8yO5Zec09Q.g3JoMryHoq01ZO3TQ2Ja_ppJZb9SYdon-LfB6OGyH7s.sBCGn14NuoxujmAgRpkYPg' \
--data '{}' | python -m json.tool --no-ensure-ascii
--data '{
"filter": {
"ctime": {
"$gte": "2024-08-15T00:00:00+0800",
"$lt": "2024-08-16T00:00:00+0800"
}
}
}' | python -m json.tool --no-ensure-ascii
```

#### 用户-根据ID查询

```sh
curl -v --location 'http://localhost:8888/v1/user/10000' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer eyJ0eXAiOiJKV1QiLCJlbmMiOiJBMTI4Q0JDLUhTMjU2IiwiYWxnIjoiZGlyIn0..EZwETCBq1CNs8yO5Zec09Q.g3JoMryHoq01ZO3TQ2Ja_ppJZb9SYdon-LfB6OGyH7s.sBCGn14NuoxujmAgRpkYPg' | python -m json.tool --no-ensure-ascii
```
7 changes: 7 additions & 0 deletions examples/api-example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ description.workspace = true
license-file.workspace = true
repository.workspace = true

[[bin]]
name = "api-example"
path = "bin/api-example.rs"

[lints]
workspace = true

Expand All @@ -30,3 +34,6 @@ sea-query.workspace = true
sea-query-binder.workspace = true
modql.workspace = true
enum-iterator.workspace = true

[target.'cfg(not(target_env = "msvc"))'.dependencies]
tikv-jemallocator.workspace = true
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use api_example::{router::new_api_router, state::new_app_state};
use ultimate_web::server::init_server;

#[cfg(not(target_env = "msvc"))]
#[global_allocator]
static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;

#[tokio::main]
async fn main() -> ultimate::Result<()> {
let state = new_app_state().await?;
Expand Down
3 changes: 1 addition & 2 deletions examples/api-example/src/user/user_serv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use super::{

#[derive(Constructor)]
pub struct UserServ {
_app: AppState,
ctx: CtxW,
}

Expand Down Expand Up @@ -59,6 +58,6 @@ impl FromRequestParts<AppState> for UserServ {

async fn from_request_parts(parts: &mut Parts, state: &AppState) -> core::result::Result<Self, Self::Rejection> {
let ctx = CtxW::from_request_parts(parts, state).await?;
Ok(UserServ::new(state.clone(), ctx))
Ok(UserServ::new(ctx))
}
}
26 changes: 0 additions & 26 deletions examples/db-example/Cargo.toml

This file was deleted.

1 change: 0 additions & 1 deletion examples/db-example/src/lib.rs

This file was deleted.

3 changes: 0 additions & 3 deletions examples/db-example/src/main.rs

This file was deleted.

7 changes: 0 additions & 7 deletions examples/db-example/src/role/mod.rs

This file was deleted.

42 changes: 0 additions & 42 deletions examples/db-example/src/role/model.rs

This file was deleted.

41 changes: 0 additions & 41 deletions examples/db-example/src/role/role_bmc.rs

This file was deleted.

3 changes: 0 additions & 3 deletions examples/db-example/src/role/role_service.rs

This file was deleted.

1 change: 1 addition & 0 deletions examples/postgresql.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
log_statement = "all"
3 changes: 3 additions & 0 deletions examples/software/postgres/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@ ENV PGPORT 15432

RUN localedef -i zh_CN -c -f UTF-8 -A /usr/share/locale/locale.alias zh_CN.UTF-8

EXPOSE 15432

COPY scripts/*.sql /docker-entrypoint-initdb.d/
COPY --chmod=0600 .pgpass /root/.pgpass

12 changes: 12 additions & 0 deletions examples/software/postgres/scripts/00.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh

pg_setup_postgresql_conf() {
{
printf '\n'
printf "log_statement = 'all'"
printf '\n'
} >> "$PGDATA/postgresql.conf"
}

pg_setup_postgresql_conf

1 change: 0 additions & 1 deletion ultimates/ultimate-db/src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use serde::{Deserialize, Serialize};
use sqlx::{postgres::PgRow, FromRow};
use uuid::Uuid;

#[allow(unused)]
pub trait DbRowType: HasSeaFields + for<'r> FromRow<'r, PgRow> + Unpin + Send {}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Display)]
Expand Down

0 comments on commit 2b0eff7

Please sign in to comment.