Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions examples/integration/hyper/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@ 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
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())
}