use std::{
fs::{canonicalize, read},
path::PathBuf,
};
use http::Request;
use wry::{
application::{
event::{Event, StartCause, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::WindowBuilder,
},
http::{header::CONTENT_TYPE, Response},
webview::WebViewBuilder,
};
const PAGE1_HTML: &[u8] = include_bytes!("custom_protocol_page1.html");
fn main() -> wry::Result<()> {
let event_loop = EventLoop::new();
let window = WindowBuilder::new()
.with_title("Custom Protocol")
.build(&event_loop)
.unwrap();
let _webview = WebViewBuilder::new(window)
.unwrap()
.with_asynchronous_custom_protocol("wry".into(), move |request, responder| {
match get_wry_response(request) {
Ok(http_response) => responder.respond(http_response),
Err(e) => responder.respond(
http::Response::builder()
.header(CONTENT_TYPE, "text/plain")
.status(500)
.body(e.to_string().as_bytes().to_vec())
.unwrap(),
),
}
})
.with_url("wry://localhost")?
.build()?;
event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;
match event {
Event::NewEvents(StartCause::Init) => println!("Wry application started!"),
Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
} => *control_flow = ControlFlow::Exit,
_ => (),
}
});
}
fn get_wry_response(
request: Request<Vec<u8>>,
) -> Result<http::Response<Vec<u8>>, Box<dyn std::error::Error>> {
let path = request.uri().path();
let content = if path == "/" {
PAGE1_HTML.into()
} else {
read(canonicalize(PathBuf::from("examples").join(&path[1..]))?)?
};
let mimetype = if path.ends_with(".html") || path == "/" {
"text/html"
} else if path.ends_with(".js") {
"text/javascript"
} else if path.ends_with(".png") {
"image/png"
} else if path.ends_with(".wasm") {
"application/wasm"
} else {
unimplemented!();
};
Response::builder()
.header(CONTENT_TYPE, mimetype)
.body(content)
.map_err(Into::into)
}