Skip to content

Commit

Permalink
refactor(http): api
Browse files Browse the repository at this point in the history
  • Loading branch information
lennartkloock committed Jan 29, 2025
1 parent 1e8dbde commit 4619bdf
Show file tree
Hide file tree
Showing 10 changed files with 476 additions and 294 deletions.
66 changes: 66 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 crates/http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ keywords = ["http", "server", "http1", "http2", "http3"]
tokio = { version = "1.43.0" }
tower = { version = "0.5.2", features = ["make"] }
thiserror = "2.0.11"
itertools = "0.14.0"

# Config
serde = { version = "1.0.217", features = ["derive"] }
Expand Down
2 changes: 1 addition & 1 deletion crates/http/examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ name = "scuffle-http-axum"
path = "src/axum.rs"

[dev-dependencies]
axum = { version = "0.8.1", features = ["macros"] }
axum = { version = "0.8.1", features = ["macros", "ws"] }
tokio = { version = "1.43.0", features = ["macros", "rt-multi-thread"] }
tracing = "0.1.41"
tracing-subscriber = { version = "0.3.19", features = ["env-filter"] }
Expand Down
66 changes: 34 additions & 32 deletions crates/http/examples/src/axum.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
use std::net::SocketAddr;
use std::{fs, io};

use axum::body::Body;
use axum::http::Request;
use axum::response::Response;
use rustls::pki_types::{CertificateDer, PrivateKeyDer};
use scuffle_http::backend::h3::Http3Backend;
use scuffle_http::backend::hyper::secure::SecureBackend;
use tracing::level_filters::LevelFilter;
use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter};

pub async fn hello_world(req: Request<axum::body::Body>) -> axum::response::Response<String> {
async fn hello_world(req: Request<axum::body::Body>) -> axum::response::Response<String> {
tracing::info!("received request: {} {}", req.method(), req.uri());

axum::response::Response::new("Hello, World!\n".to_string())
let mut resp = axum::response::Response::new("Hello, World!\n".to_string());

// TODO: this has to be part of the library somehow
resp.headers_mut().insert("Alt-Svc", "h3=\":443\"; ma=3600, h2=\":443\"; ma=3600".parse().unwrap());

resp
}

async fn ws(ws: axum::extract::ws::WebSocketUpgrade) -> Response<Body> {
ws.on_upgrade(|mut socket| async move {
while let Some(msg) = socket.recv().await {
let msg = msg.unwrap();
socket.send(msg).await.unwrap();
}
})
}

#[tokio::main]
Expand All @@ -23,27 +41,23 @@ async fn main() -> io::Result<()> {
)
.init();

let config = scuffle_http::config::Config::secure_only("[::]:443".parse().unwrap());

let make_service = axum::Router::<()>::new()
.route("/", axum::routing::get(hello_world))
.route("/ws", axum::routing::get(ws))
.into_make_service_with_connect_info::<SocketAddr>();

let server = scuffle_http::server::Server {
config,
tls_config: Some(get_tls_config()?),
make_service,
};

server.run().await?;
scuffle_http::server::Server::new()
.with_rustls_config(get_tls_config()?)
.with_backend(Http3Backend::default())
.with_backend(SecureBackend::default())
.run(make_service)
.await?;

Ok(())
}

pub fn get_tls_config() -> io::Result<rustls::ServerConfig> {
rustls::crypto::aws_lc_rs::default_provider()
.install_default()
.unwrap();
rustls::crypto::aws_lc_rs::default_provider().install_default().unwrap();

let certs = load_certs("fullchain.pem")?;
let key = load_private_key("privkey.pem")?;
Expand All @@ -59,12 +73,8 @@ pub fn get_tls_config() -> io::Result<rustls::ServerConfig> {
// Load public certificate from file.
fn load_certs(filename: &str) -> io::Result<Vec<CertificateDer<'static>>> {
// Open certificate file.
let certfile = fs::File::open(filename).map_err(|e| {
io::Error::new(
io::ErrorKind::Other,
format!("failed to open {}: {}", filename, e),
)
})?;
let certfile = fs::File::open(filename)
.map_err(|e| io::Error::new(io::ErrorKind::Other, format!("failed to open {}: {}", filename, e)))?;
let mut reader = io::BufReader::new(certfile);

// Load and return certificate.
Expand All @@ -74,19 +84,11 @@ fn load_certs(filename: &str) -> io::Result<Vec<CertificateDer<'static>>> {
// Load private key from file.
fn load_private_key(filename: &str) -> io::Result<PrivateKeyDer<'static>> {
// Open keyfile.
let keyfile = fs::File::open(filename).map_err(|e| {
io::Error::new(
io::ErrorKind::Other,
format!("failed to open {}: {}", filename, e),
)
})?;
let keyfile = fs::File::open(filename)
.map_err(|e| io::Error::new(io::ErrorKind::Other, format!("failed to open {}: {}", filename, e)))?;
let mut reader = io::BufReader::new(keyfile);

// Load and return a single private key.
rustls_pemfile::private_key(&mut reader)?.ok_or_else(|| {
io::Error::new(
io::ErrorKind::Other,
format!("no private key found in {}", filename),
)
})
rustls_pemfile::private_key(&mut reader)?
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, format!("no private key found in {}", filename)))
}
Loading

0 comments on commit 4619bdf

Please sign in to comment.