Skip to content

Commit

Permalink
* Update Axum 0.6 to Axum 0.7
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremychone committed Nov 27, 2023
1 parent 075e17a commit a39cf54
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 24 deletions.
2 changes: 1 addition & 1 deletion crates/libs/lib-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ macro_rules! exec_rpc_fn {
let params = from_value(params).map_err(|_| Error::RpcFailJsonParams {
rpc_method: rpc_fn_name.to_string(),
})?;
$rpc_fn($ctx, $mm, params).await.map(to_value)?? // FIXME
$rpc_fn($ctx, $mm, params).await.map(to_value)??
}};

// Without Params
Expand Down
6 changes: 3 additions & 3 deletions crates/services/web-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ serde = { version = "1", features = ["derive"] }
serde_json = "1"
serde_with = "3"
# -- Web
axum = {version = "0.6", features = ["macros"]}
tower-http = { version = "0.4", features = ["fs"] }
tower-cookies = "0.9"
axum = {version = "0.7", features = ["macros"]}
tower-http = { version = "0.5", features = ["fs"] }
tower-cookies = "0.10"
# -- Tracing
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
Expand Down
6 changes: 4 additions & 2 deletions crates/services/web-server/src/log/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@ use lib_core::ctx::Ctx;
use serde::Serialize;
use serde_json::{json, Value};
use serde_with::skip_serializing_none;
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};
use tracing::debug;
use uuid::Uuid;

// TODO: Need to cleanup the &Arc<.>
pub async fn log_request(
uuid: Uuid,
req_method: Method,
uri: Uri,
rpc_info: Option<&RpcInfo>,
rpc_info: Option<&Arc<RpcInfo>>,
ctx: Option<Ctx>,
web_error: Option<&web::Error>,
web_error: Option<&Arc<web::Error>>,
client_error: Option<ClientError>,
) -> Result<()> {
let timestamp = SystemTime::now()
Expand Down
10 changes: 5 additions & 5 deletions crates/services/web-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::web::{routes_login, routes_rpc, routes_static};
use axum::{middleware, Router};
use lib_core::_dev_utils;
use lib_core::model::ModelManager;
use std::net::SocketAddr;
use tokio::net::TcpListener;
use tower_cookies::CookieManagerLayer;
use tracing::info;
use tracing_subscriber::EnvFilter;
Expand Down Expand Up @@ -50,10 +50,10 @@ async fn main() -> Result<()> {
.fallback_service(routes_static::serve_dir());

// region: --- Start Server
let addr = SocketAddr::from(([127, 0, 0, 1], 8080));
info!("{:<12} - {addr}\n", "LISTENING");
axum::Server::bind(&addr)
.serve(routes_all.into_make_service())
// Note: For this block, ok to unwrap.
let listener = TcpListener::bind("127.0.0.1:8080").await.unwrap();
info!("{:<12} - {:?}\n", "LISTENING", listener.local_addr());
axum::serve(listener, routes_all.into_make_service())
.await
.unwrap();
// endregion: --- Start Server
Expand Down
5 changes: 3 additions & 2 deletions crates/services/web-server/src/web/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use lib_auth::{pwd, token};
use lib_core::model;
use serde::Serialize;
use serde_with::{serde_as, DisplayFromStr};
use std::sync::Arc;
use tracing::debug;

pub type Result<T> = core::result::Result<T, Error>;
Expand Down Expand Up @@ -39,7 +40,7 @@ pub enum Error {

// -- External Modules
#[from]
SerdeJson(#[serde_as(as = "DisplayFromStr")] serde_json::Error),
SerdeJson(#[serde_as(as = "DisplayFromStr")] Arc<serde_json::Error>),
}

// region: --- Axum IntoResponse
Expand All @@ -51,7 +52,7 @@ impl IntoResponse for Error {
let mut response = StatusCode::INTERNAL_SERVER_ERROR.into_response();

// Insert the Error into the reponse.
response.extensions_mut().insert(self);
response.extensions_mut().insert(Arc::new(self));

response
}
Expand Down
2 changes: 1 addition & 1 deletion crates/services/web-server/src/web/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn set_token_cookie(cookies: &Cookies, user: &str, salt: Uuid) -> Result<()> {
}

fn remove_token_cookie(cookies: &Cookies) -> Result<()> {
let mut cookie = Cookie::named(AUTH_TOKEN);
let mut cookie = Cookie::from(AUTH_TOKEN);
cookie.set_path("/");

cookies.remove(cookie);
Expand Down
15 changes: 8 additions & 7 deletions crates/services/web-server/src/web/mw_auth.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::web::{set_token_cookie, AUTH_TOKEN};
use crate::web::{Error, Result};
use async_trait::async_trait;
use axum::body::Body;
use axum::extract::{FromRequestParts, State};
use axum::http::request::Parts;
use axum::http::Request;
Expand All @@ -15,10 +16,10 @@ use tower_cookies::{Cookie, Cookies};
use tracing::debug;

#[allow(dead_code)] // For now, until we have the rpc.
pub async fn mw_ctx_require<B>(
pub async fn mw_ctx_require(
ctx: Result<CtxW>,
req: Request<B>,
next: Next<B>,
req: Request<Body>,
next: Next,
) -> Result<Response> {
debug!("{:<12} - mw_ctx_require - {ctx:?}", "MIDDLEWARE");

Expand All @@ -27,11 +28,11 @@ pub async fn mw_ctx_require<B>(
Ok(next.run(req).await)
}

pub async fn mw_ctx_resolve<B>(
pub async fn mw_ctx_resolve(
mm: State<ModelManager>,
cookies: Cookies,
mut req: Request<B>,
next: Next<B>,
mut req: Request<Body>,
next: Next,
) -> Result<Response> {
debug!("{:<12} - mw_ctx_resolve", "MIDDLEWARE");

Expand All @@ -40,7 +41,7 @@ pub async fn mw_ctx_resolve<B>(
if ctx_ext_result.is_err()
&& !matches!(ctx_ext_result, Err(CtxExtError::TokenNotInCookie))
{
cookies.remove(Cookie::named(AUTH_TOKEN))
cookies.remove(Cookie::from(AUTH_TOKEN))
}

// Store the ctx_ext_result in the request extension
Expand Down
5 changes: 3 additions & 2 deletions crates/services/web-server/src/web/mw_res_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use axum::http::{Method, Uri};
use axum::response::{IntoResponse, Response};
use axum::Json;
use serde_json::{json, to_value};
use std::sync::Arc;
use tracing::debug;
use uuid::Uuid;

Expand All @@ -20,10 +21,10 @@ pub async fn mw_reponse_map(
debug!("{:<12} - mw_reponse_map", "RES_MAPPER");
let uuid = Uuid::new_v4();

let rpc_info = res.extensions().get::<RpcInfo>();
let rpc_info = res.extensions().get::<Arc<RpcInfo>>();

// -- Get the eventual response error.
let web_error = res.extensions().get::<web::Error>();
let web_error = res.extensions().get::<Arc<web::Error>>();
let client_status_error = web_error.map(|se| se.client_status_and_error());

// -- If client error, build the new reponse.
Expand Down
3 changes: 2 additions & 1 deletion crates/services/web-server/src/web/routes_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use lib_core::ctx::Ctx;
use lib_core::model::ModelManager;
use lib_rpc::{exec_rpc, RpcRequest};
use serde_json::{json, Value};
use std::sync::Arc;
use tracing::debug;

pub fn routes(mm: ModelManager) -> Router {
Expand Down Expand Up @@ -38,7 +39,7 @@ async fn rpc_handler(

// -- Exec & Store RpcInfo in response.
let mut res = _rpc_handler(ctx, mm, rpc_req).await.into_response();
res.extensions_mut().insert(rpc_info);
res.extensions_mut().insert(Arc::new(rpc_info));

res
}
Expand Down

0 comments on commit a39cf54

Please sign in to comment.