Skip to content
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
16 changes: 16 additions & 0 deletions examples/integration/hyper/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 examples/integration/hyper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ serde_json = "1.0"
webui-parser = { path = "../../../crates/webui-parser" }
webui-protocol = { path = "../../../crates/webui-protocol" }
webui-handler = { path = "../../../crates/webui-handler" }
hyper = { version = "1", features = ["http2", "server"] }
hyper-util = { version = "0.1", features = ["tokio"] }
hyper = { version = "1", features = ["http1", "http2", "server"] }
hyper-util = { version = "0.1", features = ["http1", "http2", "server-auto", "tokio"] }
http-body-util = "0.1"
tokio = { version = "1", features = ["full"] }
bytes = "1"
Expand Down
2 changes: 1 addition & 1 deletion examples/integration/hyper/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ writes the result to `dist/index.html`, and serves it over HTTP at `http://127.0

## Performance highlights

- **HTTP/2 only** — multiplexed streams, header compression, and binary framing via `hyper-util`'s `http2::Builder`
- **HTTP/1.1 + HTTP/2** — auto-negotiates per connection via `hyper-util`'s `auto::Builder`; browsers use HTTP/1.1 over plaintext while HTTP/2 clients can use h2c
- **hyper 1.x** — zero-copy HTTP parsing with minimal allocations
- **`Bytes` bodies** — reference-counted buffers avoid copying response data
- **`Full<Bytes>`** — lightweight body type with no boxing overhead
Expand Down
8 changes: 6 additions & 2 deletions examples/integration/hyper/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,27 @@ use anyhow::{Context, Result};
use bytes::Bytes;
use clap::Parser;
use http_body_util::Full;
use hyper::server::conn::http2;
use hyper::service::service_fn;
use hyper::{Request, Response};
use hyper_util::rt::{TokioExecutor, TokioIo};
use hyper_util::server::conn::auto;
use tokio::net::TcpListener;

#[path = "../../../../examples/shared/rust/config.rs"]
mod config;
#[path = "../../../../examples/shared/rust/hmr.rs"]
mod hmr;
#[path = "../../../../examples/shared/rust/output.rs"]
mod output;
#[path = "../../../../examples/shared/rust/render.rs"]
mod render;
mod routes {
pub mod assets;
pub mod hmr;
pub mod index;
pub mod not_found;
}
#[path = "../../../../examples/shared/rust/watcher.rs"]
mod watcher;

use crate::config::AppPaths;
Expand Down Expand Up @@ -95,7 +99,7 @@ async fn run(cli: &Cli) -> Result<()> {
let paths = Arc::clone(&paths);

tokio::task::spawn(async move {
if let Err(err) = http2::Builder::new(TokioExecutor::new())
if let Err(err) = auto::Builder::new(TokioExecutor::new())
.serve_connection(
io,
service_fn(|req| handle_request(req, Arc::clone(&paths))),
Expand Down
21 changes: 2 additions & 19 deletions examples/integration/hyper/src/routes/hmr.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,14 @@
use std::fs;
use std::time::SystemTime;

use bytes::Bytes;
use http_body_util::Full;
use hyper::{Response, StatusCode};

use crate::config::AppPaths;
use crate::hmr::hmr_version;

/// HMR endpoint returning a version derived from the latest modification time
/// of the template or data file. The client polls this to detect changes.
pub fn handle_hmr(paths: &AppPaths) -> Response<Full<Bytes>> {
let template_mtime = fs::metadata(&paths.template)
.and_then(|m| m.modified())
.ok();
let data_mtime = fs::metadata(&paths.data).and_then(|m| m.modified()).ok();

let latest: Option<SystemTime> = match (template_mtime, data_mtime) {
(Some(t), Some(d)) => Some(if t > d { t } else { d }),
(Some(t), None) => Some(t),
(None, Some(d)) => Some(d),
(None, None) => None,
};

let version_str = latest
.and_then(|t| t.duration_since(SystemTime::UNIX_EPOCH).ok())
.map(|d| d.as_millis().to_string())
.unwrap_or_else(|| "0".to_string());
let version_str = hmr_version(&paths.template, &paths.data);

Response::builder()
.status(StatusCode::OK)
Expand Down
4 changes: 4 additions & 0 deletions examples/integration/tiny_http/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@ use tiny_http::Server;

#[path = "../../../../examples/shared/rust/config.rs"]
mod config;
#[path = "../../../../examples/shared/rust/hmr.rs"]
mod hmr;
#[path = "../../../../examples/shared/rust/output.rs"]
mod output;
#[path = "../../../../examples/shared/rust/render.rs"]
mod render;
mod routes {
pub mod assets;
pub mod hmr;
pub mod index;
pub mod not_found;
}
#[path = "../../../../examples/shared/rust/watcher.rs"]
mod watcher;

use crate::config::AppPaths;
Expand Down
61 changes: 0 additions & 61 deletions examples/integration/tiny_http/src/render.rs

This file was deleted.

28 changes: 5 additions & 23 deletions examples/integration/tiny_http/src/routes/hmr.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,13 @@
use std::fs;
use std::time::SystemTime;

use tiny_http::{Request, Response, StatusCode};

use crate::config::AppPaths;
use crate::hmr::hmr_version;

// HMR route which will refresh when the template or data file changes
// Returns a version derived from the latest modification time of
// template.html or data.json, so no shared counter is needed.
/// HMR route which will refresh when the template or data file changes.
/// Returns a version derived from the latest modification time of
/// template.html or data.json, so no shared counter is needed.
pub fn handle_hmr(request: Request, paths: &AppPaths) {
let template_mtime = fs::metadata(&paths.template)
.and_then(|m| m.modified())
.ok();
let data_mtime = fs::metadata(&paths.data).and_then(|m| m.modified()).ok();

let latest: Option<SystemTime> = match (template_mtime, data_mtime) {
(Some(t), Some(d)) => Some(if t > d { t } else { d }),
(Some(t), None) => Some(t),
(None, Some(d)) => Some(d),
(None, None) => None,
};

let version_str = latest
.and_then(|t| t.duration_since(SystemTime::UNIX_EPOCH).ok())
.map(|d| d.as_millis().to_string())
.unwrap_or_else(|| "0".to_string());

let version_str = hmr_version(&paths.template, &paths.data);
let response = Response::from_string(version_str).with_status_code(StatusCode(200));
let _ = request.respond(response);
}
92 changes: 0 additions & 92 deletions examples/integration/tiny_http/src/watcher.rs

This file was deleted.

25 changes: 25 additions & 0 deletions examples/shared/rust/hmr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use std::fs;
use std::path::Path;
use std::time::SystemTime;

/// Compute an HMR version string from the latest modification time
/// of the template and data files. Returns a millisecond timestamp
/// or `"0"` if neither file has a retrievable modification time.
pub fn hmr_version(template: &Path, data: &Path) -> String {
let template_mtime = fs::metadata(template)
.and_then(|m| m.modified())
.ok();
let data_mtime = fs::metadata(data).and_then(|m| m.modified()).ok();

let latest: Option<SystemTime> = match (template_mtime, data_mtime) {
(Some(t), Some(d)) => Some(if t > d { t } else { d }),
(Some(t), None) => Some(t),
(None, Some(d)) => Some(d),
(None, None) => None,
};

latest
.and_then(|t| t.duration_since(SystemTime::UNIX_EPOCH).ok())
.map(|d| d.as_millis().to_string())
.unwrap_or_else(|| "0".to_string())
}