From 8730ce51d211dcc1f060516f61b01b45aeed8b91 Mon Sep 17 00:00:00 2001 From: simonsan <14062932+simonsan@users.noreply.github.com> Date: Fri, 29 Jul 2022 12:21:57 +0200 Subject: [PATCH 1/2] Added more Clippy lints and fix (most of) them --- Cargo.toml | 7 ++++++- src/main.rs | 14 ++++++++++---- src/middlewares/authenticator.rs | 13 +++++++------ src/middlewares/usersecure.rs | 3 ++- src/routes.rs | 6 +++--- src/routes/api.rs | 3 ++- src/routes/auth.rs | 5 ++++- src/routes/notimplemented.rs | 1 + src/routes/session.rs | 6 ++++-- src/store.rs | 2 +- 10 files changed, 40 insertions(+), 20 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2898c7d..b8b8245 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,12 @@ name = "svelte-axum-project" version = "0.1.0" edition = "2021" - +repository = "https://github.com/jbertovic/svelte-axum-project" +keywords = ["template", "backend", "frontend", "axum", "svelte"] +license = "" +categories = [] +description = "" +readme = "README.md" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] diff --git a/src/main.rs b/src/main.rs index e281c64..3bca9e2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,8 @@ +#![deny(clippy::all)] +#![warn(clippy::pedantic)] +#![warn(clippy::nursery)] +#![allow(missing_docs)] + use axum::{ http::StatusCode, middleware, @@ -64,7 +69,7 @@ async fn main() { // NON-AUTH AREA routes: login, logout and session // `/test` shows example of a non implemented route let non_auth_backend = Router::new() - .route("/auth/session", get(routes::session_data_handler)) // gets session data + .route("/auth/session", get(routes::data_handler)) // gets session data .route("/auth/login", post(routes::login)) // sets username in session .route("/auth/logout", get(routes::logout)) // deletes username in session .route("/test", get(routes::not_implemented_route)); @@ -73,10 +78,10 @@ async fn main() { // `/secure` shows an example of checking session information for user_id to allow access // `/api` can be accessed using an authorization header and with no session let auth_backend_using_token = Router::new() - .route("/api", get(routes::api_handler)) + .route("/api", get(routes::handler)) .route_layer(middleware::from_fn(middlewares::auth)); let auth_backend_using_session = Router::new() - .route("/secure", get(routes::session_out_handler)) + .route("/secure", get(routes::out_handler)) .route_layer(middleware::from_fn(middlewares::user_secure)); // could add tower::ServiceBuilder here to group layers, especially if you add more layers. @@ -99,6 +104,7 @@ async fn main() { .unwrap(); } +#[allow(clippy::unused_async)] async fn handle_error(_err: io::Error) -> impl IntoResponse { ( StatusCode::INTERNAL_SERVER_ERROR, @@ -107,7 +113,7 @@ async fn handle_error(_err: io::Error) -> impl IntoResponse { } /// Tokio signal handler that will wait for a user to press CTRL+C. -/// We use this in our hyper `Server` method `with_graceful_shutdown`. +/// We use this in our `Server` method `with_graceful_shutdown`. async fn shutdown_signal() { tokio::signal::ctrl_c() .await diff --git a/src/middlewares/authenticator.rs b/src/middlewares/authenticator.rs index ca86817..f67be84 100644 --- a/src/middlewares/authenticator.rs +++ b/src/middlewares/authenticator.rs @@ -12,10 +12,11 @@ use crate::store::Store; /// middleware function to authenticate authorization token /// check store that contains token and see if it matches authorization header starting with "Bearer" -/// used example in axum docs on middleware https://docs.rs/axum/latest/axum/middleware/index.html +/// used example in axum docs on middleware /// /// Returns Error's in JSON format. -pub async fn auth( +#[allow(clippy::missing_errors_doc)] +pub async fn auth( req: Request, next: Next, ) -> Result)> { @@ -58,18 +59,18 @@ pub struct JsonError { } impl JsonError { - pub fn new(error: String) -> Self { - JsonError { error } + pub const fn new(error: String) -> Self { + Self { error } } pub fn unauthorized() -> Self { - JsonError { + Self { error: "Unauthorized".into(), } } pub fn internal() -> Self { - JsonError { + Self { error: "Internal Server Error".into(), } } diff --git a/src/middlewares/usersecure.rs b/src/middlewares/usersecure.rs index f7cbfc4..41318c2 100644 --- a/src/middlewares/usersecure.rs +++ b/src/middlewares/usersecure.rs @@ -5,7 +5,8 @@ use axum::{ }; use axum_sessions::async_session::Session; -pub async fn user_secure(req: Request, next: Next) -> Result { +#[allow(clippy::missing_errors_doc)] +pub async fn user_secure(req: Request, next: Next) -> Result { tracing::info!("Middleware: checking if user exists"); let session = req .extensions() diff --git a/src/routes.rs b/src/routes.rs index 693ae32..4ef0358 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -3,9 +3,9 @@ mod auth; mod notimplemented; mod session; -pub use api::api_handler; +pub use api::handler; pub use auth::login; pub use auth::logout; pub use notimplemented::not_implemented_route; -pub use session::session_data_handler; -pub use session::session_out_handler; +pub use session::data_handler; +pub use session::out_handler; diff --git a/src/routes/api.rs b/src/routes/api.rs index 6933554..6d7da48 100644 --- a/src/routes/api.rs +++ b/src/routes/api.rs @@ -2,7 +2,8 @@ use axum::{response::IntoResponse, Json}; use axum_sessions::async_session::serde_json::json; /// imitating an API response -pub async fn api_handler() -> impl IntoResponse { +#[allow(clippy::unused_async)] +pub async fn handler() -> impl IntoResponse { tracing::info!("Seeking api data"); Json( json!({"result": "ok", "message": "You've reached the backend API by using a valid token."}), diff --git a/src/routes/auth.rs b/src/routes/auth.rs index 959fc78..c38beb7 100644 --- a/src/routes/auth.rs +++ b/src/routes/auth.rs @@ -3,6 +3,8 @@ use axum_sessions::async_session::{serde_json::json, Session}; use serde::Deserialize; /// route to handle log in +#[allow(clippy::unused_async)] +#[allow(clippy::missing_panics_doc)] pub async fn login( Json(login): Json, Extension(mut session): Extension, @@ -18,6 +20,7 @@ pub async fn login( } /// route to handle log out +#[allow(clippy::unused_async)] pub async fn logout(Extension(mut session): Extension) -> impl IntoResponse { let user = session.get_raw("user_id").unwrap_or_default(); tracing::info!("Logging out user: {}", user); @@ -27,7 +30,7 @@ pub async fn logout(Extension(mut session): Extension) -> impl IntoResp } // assume all passwords work -fn check_password(_username: &str, _password: &str) -> bool { +const fn check_password(_username: &str, _password: &str) -> bool { true } diff --git a/src/routes/notimplemented.rs b/src/routes/notimplemented.rs index 7aa6bb7..8219b99 100644 --- a/src/routes/notimplemented.rs +++ b/src/routes/notimplemented.rs @@ -1,5 +1,6 @@ use axum::{body::Body, http::Request, response::IntoResponse}; +#[allow(clippy::unused_async)] pub async fn not_implemented_route(req: Request) -> impl IntoResponse { // add which route is requesting this? format!("Route is planned but not yet implemented for {}", req.uri()) diff --git a/src/routes/session.rs b/src/routes/session.rs index a7346bc..bc9d7f8 100644 --- a/src/routes/session.rs +++ b/src/routes/session.rs @@ -4,13 +4,15 @@ use axum::{response::IntoResponse, Extension, Json}; use axum_sessions::async_session::{serde_json::json, Session}; /// output entire session object -pub async fn session_out_handler(Extension(session): Extension) -> impl IntoResponse { +#[allow(clippy::unused_async)] +pub async fn out_handler(Extension(session): Extension) -> impl IntoResponse { tracing::info!("Seeking session info"); Json(json!({ "session": format!("{:?}", session) })) } /// output session data in json -pub async fn session_data_handler(Extension(session): Extension) -> impl IntoResponse { +#[allow(clippy::unused_async)] +pub async fn data_handler(Extension(session): Extension) -> impl IntoResponse { tracing::info!("Seeking session data"); let user_id = session.get("user_id").unwrap_or_else(|| "".to_string()); Json(json!({ "user_id": user_id })) diff --git a/src/store.rs b/src/store.rs index f987c5d..879f887 100644 --- a/src/store.rs +++ b/src/store.rs @@ -5,7 +5,7 @@ pub struct Store { impl Store { pub fn new(api_token: &str) -> Self { - Store { + Self { api_token: api_token.to_string(), } } From f5563e673d27107667a4dad74335adcc402c6e8a Mon Sep 17 00:00:00 2001 From: Jas Bertovic Date: Sun, 31 Jul 2022 09:09:17 -0400 Subject: [PATCH 2/2] route names modified to make clearer --- src/main.rs | 6 +++--- src/routes.rs | 7 ++----- src/routes/session.rs | 2 +- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index 3bca9e2..5e47239 100644 --- a/src/main.rs +++ b/src/main.rs @@ -69,7 +69,7 @@ async fn main() { // NON-AUTH AREA routes: login, logout and session // `/test` shows example of a non implemented route let non_auth_backend = Router::new() - .route("/auth/session", get(routes::data_handler)) // gets session data + .route("/auth/session", get(routes::session::data_handler)) // gets session data .route("/auth/login", post(routes::login)) // sets username in session .route("/auth/logout", get(routes::logout)) // deletes username in session .route("/test", get(routes::not_implemented_route)); @@ -78,10 +78,10 @@ async fn main() { // `/secure` shows an example of checking session information for user_id to allow access // `/api` can be accessed using an authorization header and with no session let auth_backend_using_token = Router::new() - .route("/api", get(routes::handler)) + .route("/api", get(routes::api::handler)) .route_layer(middleware::from_fn(middlewares::auth)); let auth_backend_using_session = Router::new() - .route("/secure", get(routes::out_handler)) + .route("/secure", get(routes::session::handler)) .route_layer(middleware::from_fn(middlewares::user_secure)); // could add tower::ServiceBuilder here to group layers, especially if you add more layers. diff --git a/src/routes.rs b/src/routes.rs index 4ef0358..473b741 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -1,11 +1,8 @@ -mod api; +pub mod api; mod auth; mod notimplemented; -mod session; +pub mod session; -pub use api::handler; pub use auth::login; pub use auth::logout; pub use notimplemented::not_implemented_route; -pub use session::data_handler; -pub use session::out_handler; diff --git a/src/routes/session.rs b/src/routes/session.rs index bc9d7f8..75b9f2b 100644 --- a/src/routes/session.rs +++ b/src/routes/session.rs @@ -5,7 +5,7 @@ use axum_sessions::async_session::{serde_json::json, Session}; /// output entire session object #[allow(clippy::unused_async)] -pub async fn out_handler(Extension(session): Extension) -> impl IntoResponse { +pub async fn handler(Extension(session): Extension) -> impl IntoResponse { tracing::info!("Seeking session info"); Json(json!({ "session": format!("{:?}", session) })) }