Skip to content

Commit

Permalink
feat: response middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
jayden-dang committed Jul 8, 2024
1 parent e4805cd commit fbb2c05
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ thiserror = "1.0.61"
tokio = { version = "1.38.0", features = ["full"] }
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
uuid = { version = "1.9.1", features = ["v4", "fast-rng"] }
16 changes: 9 additions & 7 deletions https.org
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Configs
#+name: host
#+BEGIN_SRC elisp
"http://localhost:8080"
"http://localhost:3333"
#+END_SRC

#+name: headers
Expand All @@ -25,16 +25,18 @@ GET :host/hello
#+RESULTS:
#+BEGIN_SRC js
{
"error": "Not Found"
"msg": "hello"
}
// GET http://localhost:8080/hello
// HTTP/1.1 404 Not Found
// GET http://localhost:3333/hello
// HTTP/1.1 202 Accepted
// content-type: application/json
// content-length: 21
// date: Wed, 26 Jun 2024 14:36:32 GMT
// Request duration: 0.004924s
// content-length: 15
// date: Mon, 08 Jul 2024 09:54:37 GMT
// Request duration: 0.008933s
#+END_SRC

#+RESULTS:

** User
#+BEGIN_SRC restclient :var host=host :var headers=headers
POST :host/users
Expand Down
21 changes: 18 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use std::{env, sync::Arc};

use axum::{
extract::Path,
http::StatusCode,
http::{Method, StatusCode, Uri},
middleware,
response::{IntoResponse, Response},
routing::get,
Extension, Json, Router,
Expand All @@ -11,6 +12,7 @@ use dotenv::{dotenv, var};
use rust_microservice::{configs::ProdConfig, dbs::initialed_db, errors::AppError, AppResult};
use serde_json::json;
use tracing::info;
use uuid::Uuid;

#[tokio::main]
async fn main() {
Expand All @@ -20,7 +22,10 @@ async fn main() {
let cfg = ProdConfig::from_env().expect("Cann't get env");
let pool = initialed_db(&cfg.postgres.dsn, cfg.postgres.max_conns).await;

let app = Router::new().route("/:msg", get(say_hello)).layer(Extension(Arc::new(pool)));
let app = Router::new()
.route("/:msg", get(say_hello))
.layer(middleware::map_response(mw_map_response))
.layer(Extension(Arc::new(pool)));
info!("Connect Database successfully");

info!("Server is running on port: {}", cfg.web.addr);
Expand All @@ -29,9 +34,19 @@ async fn main() {
}

pub async fn say_hello(Path(msg): Path<String>) -> AppResult<Json<serde_json::Value>> {
if !msg.is_empty() {
info!("->> Function Say Hello");
if msg.is_empty() {
Err(AppError::NotFound)
} else {
Ok(Json(json!({"msg" : msg})))
}
}

pub async fn mw_map_response(uri: Uri, req_method: Method, res: Response) -> Response {
let uuid = Uuid::new_v4();
info!("->> MAP RESPONSE");
info!("->> UUid: {}", uuid.to_string());
info!("->> Method: {}", req_method.to_string());
info!("->> Uri: {}", uri.to_string());
(StatusCode::ACCEPTED, res).into_response()
}

0 comments on commit fbb2c05

Please sign in to comment.