我目前有工作代码:

    // build our application with a route
    let app = Router::new()
        .route("/api/:start/:tokens/:left/:top/:right/:bottom", get(stats))
        .route("/dbtest", get(dbtest))
        .layer(Extension(pool))
        ;
...
// we can extract the connection pool with `Extension`
async fn dbtest(
    Extension(pool): Extension<PgPool>,
) -> Result<String, (StatusCode, String)> {
    sqlx::query_scalar("select 'hello world from pg'")
        .fetch_one(&pool)
        .await
        .map_err(internal_error)
}
...
async fn stats(
    Path((start, tokens, left, top, bottom, right)): Path<(DateTime<Utc>, DashVec, u32, u32, u32, u32)>,
) -> String {
    format!("{} {:?} {:?} {:?}", start.to_rfc3339(), tokens, (left, top), (right, bottom))
}

我如何制作一个可以使用both数据库pool并解析其Path的处理程序?

推荐答案

您可以将它们定义为单独的参数:

async fn combined(
    Extension(pool): Extension<PgPool>,
    Path((start, tokens, left, top, bottom, right)): Path<(DateTime<Utc>, DashVec, u32, u32, u32, u32)>,
) -> Result<String, (StatusCode, String)> {
    println!("{} {:?} {:?} {:?}", start.to_rfc3339(), tokens, (left, top), (right, bottom));
    sqlx::query_scalar("select 'hello world from pg'")
        .fetch_one(&pool)
        .await
        .map_err(internal_error)
}

PathExtensionextractors,并且handler函数可以具有"zero or more extractors as arguments".

Postgresql相关问答推荐

我需要一个变量来引用上周的星期五

在特定值的组聚合中添加标签列作为结果

如何在postgres中测试锁定

忽略 split_part 的第 n 个分隔符之后

Power BI + Postgres:只读用户

在 PostgreSQL 中 Select 数字命名列会返回 ?column?

如何更改几何列的 SRID?

表或列名不能以数字开头?

postgresql 在查询中插入空值

使用 postgres regexp_replace 将字符串列表替换为始终相同的字符串

从 PostgreSQL 中的字段中提取数字

如何判断每个组中是否存在值

在 PostgreSQL 中跳过每 n 个结果行

如何在 postgresql 交叉表中用零替换空值

PostgreSQL/JDBC 和 TIMESTAMP 与 TIMESTAMPTZ

Windows 上的 GeoDjango:Could not find the GDAL library" / "OSError: [WinError 126] The specified module could not be found

有没有办法确保 WHERE 子句在 DISTINCT 之后发生?

PostgreSQL 9 在 Windows 上安装:Unable to write inside TEMP environment path.

与 iexact 一起使用时,Django get_or_create 无法设置字段

Ruby 中 DateTime 的毫秒分辨率