Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cefc2ad
commit d77521b
Showing
7 changed files
with
204 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use anyhow::Error; | ||
use hyper::Body; | ||
use routerify::Router; | ||
|
||
mod url_maps; | ||
|
||
#[macro_export] | ||
macro_rules! json_response { | ||
(body: $body:expr) => { | ||
Response::builder() | ||
.header(hyper::header::CONTENT_TYPE, "application/json") | ||
.body(serde_json::to_string($body).unwrap().into()) | ||
.unwrap() | ||
}; | ||
(status: $status:expr, body: $body:expr) => { | ||
Response::builder() | ||
.header(hyper::header::CONTENT_TYPE, "application/json") | ||
.status($status) | ||
.body(serde_json::to_string($body).unwrap().into()) | ||
.unwrap() | ||
}; | ||
(error: $e:expr) => { | ||
json_response!( | ||
status: hyper::StatusCode::INTERNAL_SERVER_ERROR, | ||
body: &serde_json::json!({ | ||
"error": $e.to_string(), | ||
}).to_string()) | ||
}; | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! sender_failed_json { | ||
($m: expr, $f: tt) => { | ||
match $m { | ||
Ok(_) => {} | ||
Err(e) => { | ||
tracing::error!("Database Manager failed to get {}! error: {}", $f, e); | ||
return Ok(json_response!(error: e)); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! recv_failed_json { | ||
($m: expr, $status: expr) => { | ||
match $m { | ||
Ok(d) => d, | ||
Err(e) => { | ||
tracing::error!("Database Manager returned error: {}", e); | ||
return Ok(json_response!( | ||
status: $status, | ||
body: &e.to_string())) | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub fn url_maps_router() -> Router<Body, Error> { | ||
Router::builder() | ||
.scope("/url_maps", url_maps::router()) | ||
.build() | ||
.unwrap() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use anyhow::Error; | ||
use hyper::Body; | ||
use routerify::Router; | ||
|
||
mod url_map_service; | ||
|
||
pub fn router() -> Router<Body, Error> { | ||
Router::builder() | ||
.get("/:id", url_map_service::read_data_by_id) | ||
.get("/", url_map_service::read_data_list) | ||
.post("/", url_map_service::create_data) | ||
.put("/:id", url_map_service::update_data) | ||
.delete("/:id", url_map_service::delete_data_by_id) | ||
.build() | ||
.unwrap() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
use crate::dao::url_map_dao::UrlMap; | ||
use crate::{json_response, recv_failed_json, sender_failed_json, BaseMapperEnum}; | ||
use anyhow::Result; | ||
use hyper::{body::to_bytes, Body, Request, Response}; | ||
use routerify::ext::RequestExt; | ||
use serde::{Deserialize, Serialize}; | ||
use tokio::sync::mpsc::Sender; | ||
|
||
pub async fn read_data_list(req: Request<Body>) -> Result<Response<Body>> { | ||
let (tx, rx) = tokio::sync::oneshot::channel(); | ||
let sender = req | ||
.data::<Sender<BaseMapperEnum<String, UrlMap>>>() | ||
.unwrap(); | ||
sender_failed_json!( | ||
sender.send(BaseMapperEnum::ReadDataList { resp: tx }).await, | ||
"GetUrlMaps" | ||
); | ||
let url_maps = recv_failed_json!(rx.await.unwrap(), hyper::StatusCode::INTERNAL_SERVER_ERROR); | ||
Ok(json_response!(body: &url_maps)) | ||
} | ||
|
||
pub async fn read_data_by_id(req: Request<Body>) -> Result<Response<Body>> { | ||
let sender = req | ||
.data::<Sender<BaseMapperEnum<String, UrlMap>>>() | ||
.unwrap(); | ||
let (tx, rx) = tokio::sync::oneshot::channel(); | ||
let id = req.param("id").unwrap(); | ||
sender_failed_json!( | ||
sender | ||
.send(BaseMapperEnum::ReadDataById { | ||
id: id.into(), | ||
resp: tx | ||
}) | ||
.await, | ||
"GetUrlMap" | ||
); | ||
let url_map = recv_failed_json!(rx.await.unwrap(), hyper::StatusCode::NOT_FOUND); | ||
Ok(json_response!(body: &url_map)) | ||
} | ||
|
||
pub async fn create_data(mut req: Request<Body>) -> Result<Response<Body>> { | ||
let body = req.body_mut(); | ||
let url_map_bytes = to_bytes(body).await?; | ||
let url_map = serde_json::from_slice::<UrlMap>(&url_map_bytes)?; | ||
let (tx, rx) = tokio::sync::oneshot::channel(); | ||
let sender = req | ||
.data::<Sender<BaseMapperEnum<String, UrlMap>>>() | ||
.unwrap(); | ||
sender_failed_json!( | ||
sender | ||
.send(BaseMapperEnum::CreateData { | ||
data: url_map, | ||
resp: tx | ||
}) | ||
.await, | ||
"CreateUrlMap" | ||
); | ||
let url_map = recv_failed_json!(rx.await.unwrap(), hyper::StatusCode::UNPROCESSABLE_ENTITY); | ||
Ok(json_response!(body: &url_map)) | ||
} | ||
|
||
pub async fn update_data(mut req: Request<Body>) -> Result<Response<Body>> { | ||
#[derive(Debug, Serialize, Deserialize)] | ||
struct UrlMapUrl { | ||
url: String, | ||
} | ||
|
||
let body = req.body_mut(); | ||
let url_map_url_bytes = to_bytes(body).await?; | ||
let url_map_url = serde_json::from_slice::<UrlMapUrl>(&url_map_url_bytes)?; | ||
let id = req.param("id").unwrap(); | ||
let url_map = UrlMap::new(id.into(), url_map_url.url); | ||
let (tx, rx) = tokio::sync::oneshot::channel(); | ||
let sender = req | ||
.data::<Sender<BaseMapperEnum<String, UrlMap>>>() | ||
.unwrap(); | ||
sender_failed_json!( | ||
sender | ||
.send(BaseMapperEnum::UpdateData { | ||
data: url_map, | ||
resp: tx | ||
}) | ||
.await, | ||
"UpdateUrlMap" | ||
); | ||
let url_map = recv_failed_json!(rx.await.unwrap(), hyper::StatusCode::UNPROCESSABLE_ENTITY); | ||
Ok(json_response!(body: &url_map)) | ||
} | ||
|
||
pub async fn delete_data_by_id(req: Request<Body>) -> Result<Response<Body>> { | ||
let id = req.param("id").unwrap(); | ||
let sender = req | ||
.data::<Sender<BaseMapperEnum<String, UrlMap>>>() | ||
.unwrap(); | ||
let (tx, rx) = tokio::sync::oneshot::channel(); | ||
sender_failed_json!( | ||
sender | ||
.send(BaseMapperEnum::DeleteDataById { | ||
id: id.into(), | ||
resp: tx | ||
}) | ||
.await, | ||
"DeleteUrlMap" | ||
); | ||
recv_failed_json!(rx.await.unwrap(), hyper::StatusCode::NOT_FOUND); | ||
Ok(json_response!(body: &serde_json::json!({ | ||
"ok": "true" | ||
}).to_string())) | ||
} |