创建短链接

本章将实现创建短链接功能。

本章代码在03/创建短链接分支。

模板文件

可以看到,我们只是简单的做了一个表单。

/// 处理提交的内容
pub async fn index_action(
    Extension(state): Extension<AppState>,
    Form(cu): Form<form::CreateUrl>,
) -> HandlerRedirectResult {
    let id = core::short_url(&cu.url);
    let handler_name = "index_action";
    let client = get_client(&state, handler_name).await?;
    let result = db::create(&client, cu, id)
        .await
        .map_err(log_error(handler_name.to_string()))?;
    let msg = MsgArgs {
        ok: Some(format!("添加成功,短网址是:{}", result.id)),
        err: None,
        target: Some("/".to_string()),
    };
    Ok(redirect_with_msg("/msg", Some(&msg)))
}

/// 首页
pub async fn index() -> HandlerHtmlResult {
    let handler_name = "index";
    let tmpl = IndexTemplate {};
    render(tmpl).map_err(log_error(handler_name.to_string()))
}
  • index_action:用于接收用户提交的表单,并将其作为参数调用数据库操作的函数

  • index:显示表单

要查看完整内容,请先登录