Skip to content

Commit

Permalink
feat: 短连接生成
Browse files Browse the repository at this point in the history
  • Loading branch information
yansongda committed Nov 6, 2023
1 parent e17831f commit 9ddf922
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/api/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn health() -> Router {
pub fn api_v1() -> Router {
let unauthorized = Router::new()
.route("/users/login", post(v1::users::login))
.route("/shortlink/detail", get(v1::shortlink::detail));
.route("/shortlink/redirect/:short", get(v1::shortlink::redirect));

let users = Router::new()
.nest(
Expand All @@ -38,7 +38,9 @@ pub fn api_v1() -> Router {
let shortlink = Router::new()
.nest(
"/shortlink",
Router::new().route("/create", post(v1::shortlink::create)),
Router::new()
.route("/create", post(v1::shortlink::create))
.route("/detail", get(v1::shortlink::detail)),
)
.layer(ServiceBuilder::new().layer(middleware::from_fn(authorization)));

Expand Down
11 changes: 9 additions & 2 deletions src/api/v1/shortlink.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use axum::extract::Query;
use axum::extract::{Path, Query};
use axum::{Extension, Json};
use axum::response::Redirect;

use crate::api::response::Resp;
use crate::model::result::Response;
use crate::model::result::{Result, Response};
use crate::model::shortlink::{CreateRequest, CreateResponse, DetailRequest, DetailResponse};
use crate::model::user::CurrentUser;
use crate::service;
Expand All @@ -22,3 +23,9 @@ pub async fn detail(Query(params): Query<DetailRequest>) -> Resp<DetailResponse>

Ok(Response::success(DetailResponse::from(shortlink)))
}

pub async fn redirect(Path(short): Path<String>) -> Result<Redirect> {
let shortlink = service::shortlink::detail(short.as_str()).await?;

Ok(Redirect::temporary(shortlink.link.as_str()))
}

0 comments on commit 9ddf922

Please sign in to comment.