Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to axum v0.8 #112

Merged
merged 1 commit into from
Feb 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## WIP

- Update to `axum` v0.8.


## v0.6.4

- Guarantee the internal `SessionActor` is dropped before `ServerExt::on_disconnect` is called.
Expand Down
17 changes: 9 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,24 @@ base64 = "0.21.0"
enfync = "0.1.0"
futures = "0.3.21"
futures-util = { version = "0.3.25", default-features = false }
http = "0.2.8"
http = "1.2.0"
tracing = "0.1.31"
tungstenite = "0.20.0"
tungstenite = "0.24.0" # Locked to tokio-tungstenite-wasm version.
url = "2.2.2"
cfg-if = "1.0.0"

axum = { version = "0.6.1", optional = true }
axum-core = { version = "0.3.0", optional = true }
axum = { version = "0.8.1", optional = true }
axum-core = { version = "0.5.0", optional = true }
bytes = { version = "1.3.0", optional = true }
fragile = { version = "2.0", optional = true }
http-body = { version = "0.4.5", optional = true }
http-body = { version = "1.0.1", optional = true }
hyper = { version = "0.14.23", optional = true }
sha-1 = { version = "0.10.1", optional = true }
tokio-tungstenite = { version = "0.20.0", optional = true }
tokio-tungstenite = { version = "0.24.0", optional = true }

tokio-tungstenite-wasm = { version = "0.2.1", optional = true }
tokio-tungstenite-wasm = { version = "0.4.0", optional = true }

tokio-rustls = { version = "0.24.1", optional = true }
tokio-rustls = { version = "0.26.1", optional = true }
tokio-native-tls = { version = "0.3.1", optional = true }

[target.'cfg(not(target_family = "wasm"))'.dependencies]
Expand Down Expand Up @@ -99,3 +99,4 @@ required-features = ["tungstenite"]
[[bench]]
name = "my_benchmark"
harness = false
required-features = ["server", "tungstenite"]
2 changes: 1 addition & 1 deletion examples/chat-server-axum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"

[dependencies]
async-trait = "0.1.52"
axum = "0.6.1"
axum = "0.8.1"
ezsockets = { path = "../../", features = ["axum"] }
tokio = { version = "1.17.0", features = ["full"] }
tracing = "0.1.32"
Expand Down
12 changes: 8 additions & 4 deletions examples/chat-server-axum/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use ezsockets::Server;
use std::collections::HashMap;
use std::io::BufRead;
use std::net::SocketAddr;
use tokio::net::TcpListener;

type SessionID = u16;
type Session = ezsockets::Session<SessionID, ()>;
Expand Down Expand Up @@ -123,10 +124,13 @@ async fn main() {

tokio::spawn(async move {
tracing::debug!("listening on {}", address);
axum::Server::bind(&address)
.serve(app.into_make_service_with_connect_info::<SocketAddr>())
.await
.unwrap();
let listener = TcpListener::bind("127.0.0.1:3000").await.unwrap();
axum::serve(
listener,
app.into_make_service_with_connect_info::<SocketAddr>(),
)
.await
.unwrap();
});

let stdin = std::io::stdin();
Expand Down
24 changes: 11 additions & 13 deletions src/server_runners/axum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ use crate::socket::SocketConfig;
use crate::Server;
use crate::ServerExt;
use crate::Socket;
use async_trait::async_trait;
use axum::extract::ConnectInfo;
use axum::extract::FromRequest;
use axum::extract::FromRequestParts;
use axum::response::Response;
use enfync::TryAdopt;
use http::request::Parts;
use std::net::SocketAddr;

/// Extractor for establishing WebSocket connections.
Expand All @@ -95,34 +95,32 @@ impl Upgrade {
}
}

#[async_trait]
impl<S, B> FromRequest<S, B> for Upgrade
impl<S> FromRequestParts<S> for Upgrade
where
S: Send + Sync,
B: Send + 'static,
{
type Rejection = WebSocketUpgradeRejection;

async fn from_request(req: http::Request<B>, state: &S) -> Result<Self, Self::Rejection> {
let ConnectInfo(address) = req
.extensions()
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
let ConnectInfo(address) = parts
.extensions
.get::<ConnectInfo<SocketAddr>>()
.expect("Axum Server must be created with `axum::Router::into_make_service_with_connect_info::<SocketAddr, _>()`")
.to_owned();

let mut pure_req = crate::Request::builder()
.method(req.method())
.uri(req.uri())
.version(req.version());
for (k, v) in req.headers() {
.method(parts.method.clone())
.uri(parts.uri.clone())
.version(parts.version);
for (k, v) in parts.headers.iter() {
pure_req = pure_req.header(k, v);
}
let Ok(pure_req) = pure_req.body(()) else {
return Err(InvalidConnectionHeader {}.into());
};

Ok(Self {
ws: WebSocketUpgrade::from_request(req, state).await?,
ws: WebSocketUpgrade::from_request_parts(parts, state).await?,
address,
request: pure_req,
})
Expand Down
2 changes: 0 additions & 2 deletions src/server_runners/axum_tungstenite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#![cfg_attr(test, allow(clippy::float_cmp))]

use self::rejection::*;
use async_trait::async_trait;
use axum_core::{
extract::FromRequestParts,
response::{IntoResponse, Response},
Expand Down Expand Up @@ -238,7 +237,6 @@ impl<C> WebSocketUpgrade<C> {
}
}

#[async_trait]
impl<S> FromRequestParts<S> for WebSocketUpgrade
where
S: Sync,
Expand Down
10 changes: 7 additions & 3 deletions tests/axum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use ezsockets::axum::Upgrade;
use ezsockets::Server;
use ezsockets::ServerExt;
use std::net::SocketAddr;
use tokio::net::TcpListener;

async fn websocket_handler<E>(
Extension(server): Extension<Server<E>>,
Expand All @@ -35,9 +36,12 @@ where
let address = SocketAddr::from(([127, 0, 0, 1], 0));

tracing::debug!("listening on {}", address);
let future =
axum::Server::bind(&address).serve(app.into_make_service_with_connect_info::<SocketAddr>());
let address = future.local_addr();
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let future = axum::serve(
listener,
app.into_make_service_with_connect_info::<SocketAddr>(),
);
let address = future.local_addr().unwrap();
tokio::spawn(async move {
future.await.unwrap();
});
Expand Down
Loading