Skip to content

Commit

Permalink
Upgrade axum
Browse files Browse the repository at this point in the history
  • Loading branch information
rouge8 committed Feb 2, 2025
1 parent 5778a60 commit 91e89f8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 105 deletions.
114 changes: 16 additions & 98 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ edition = "2021"

[dependencies]
anyhow = "1.0.75"
axum = { version = "0.7.1", features = ["macros"] }
axum-extra = { version = "0.9.0", features = ["cookie-private"] }
axum = { version = "0.8.1", features = ["macros"] }
axum-extra = { version = "0.10.0", features = ["cookie-private"] }
chrono = { version = "0.4.31", features = ["serde"] }
chrono-humanize = "0.2.3"
dotenvy = "0.15.7"
Expand Down
33 changes: 28 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use std::fmt;
use std::net::SocketAddr;

use axum::body::Body;
use axum::extract::{FromRef, FromRequestParts, Path, State};
use axum::extract::{FromRef, FromRequestParts, OptionalFromRequestParts, Path, State};
use axum::http::request::Parts;
use axum::http::{header, HeaderMap, HeaderValue, StatusCode};
use axum::response::{Html, IntoResponse, Redirect, Response};
use axum::routing::{get, post};
use axum::{async_trait, Form, Router};
use axum::{Form, Router};
use axum_extra::extract::cookie::{Cookie, Key, PrivateCookieJar};
use chrono::{DateTime, Utc};
use chrono_humanize::{Accuracy, HumanTime, Tense};
Expand Down Expand Up @@ -281,7 +281,6 @@ impl fmt::Display for WaniKaniAPIKey {
}
}

#[async_trait]
impl<S> FromRequestParts<S> for WaniKaniAPIKey
where
S: Send + Sync,
Expand All @@ -303,6 +302,30 @@ where
}
}

impl<S> OptionalFromRequestParts<S> for WaniKaniAPIKey
where
S: Send + Sync,
Key: FromRef<S>,
{
type Rejection = (StatusCode, Redirect);

async fn from_request_parts(
parts: &mut Parts,
state: &S,
) -> Result<Option<Self>, Self::Rejection> {
let jar = PrivateCookieJar::<Key>::from_request_parts(parts, state)
.await
.map_err(|err| err.into_response());

if let Ok(jar) = jar {
if let Some(cookie) = jar.get(COOKIE_NAME) {
return Ok(Some(WaniKaniAPIKey(cookie.value().to_string())));
}
}
Ok(None)
}
}

#[derive(Debug, Clone)]
struct WaniKaniAPIURL(String);

Expand Down Expand Up @@ -337,8 +360,8 @@ fn create_app(config: Config, db: Database, http_client: reqwest::Client) -> Rou
.route("/login", post(login_post))
.route("/logout", get(logout))
.route("/assignments", get(assignments))
.route("/radical-svg/:path", get(radical_svg))
.route("/static/:path", get(static_file))
.route("/radical-svg/{path}", get(radical_svg))
.route("/static/{path}", get(static_file))
.route("/test-500", get(test_500))
.layer(
ServiceBuilder::new()
Expand Down

0 comments on commit 91e89f8

Please sign in to comment.