From ff6cf5ace5baa94627961beb2d394a24c66f97e2 Mon Sep 17 00:00:00 2001 From: Brandon Dyer Date: Thu, 17 Oct 2024 21:28:26 -0600 Subject: [PATCH 1/2] Added env.assets binding --- worker/src/env.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/worker/src/env.rs b/worker/src/env.rs index fe79d03a..0bd49908 100644 --- a/worker/src/env.rs +++ b/worker/src/env.rs @@ -86,6 +86,11 @@ impl Env { self.get_binding(binding) } + /// Access the worker assets by the binding name configured in your wrangler.toml file. + pub fn assets(&self, binding: &str) -> Result { + self.get_binding(binding) + } + pub fn hyperdrive(&self, binding: &str) -> Result { self.get_binding(binding) } From 625970367085f01e4fe9e2aa571fe5af02106f67 Mon Sep 17 00:00:00 2001 From: Brandon Dyer Date: Sat, 19 Oct 2024 11:44:03 -0600 Subject: [PATCH 2/2] Assets test in worker-sandbox --- worker-sandbox/src/assets.rs | 17 +++++++++++++++++ worker-sandbox/src/lib.rs | 2 ++ worker-sandbox/src/router.rs | 3 ++- worker-sandbox/wrangler.toml | 4 ++++ 4 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 worker-sandbox/src/assets.rs diff --git a/worker-sandbox/src/assets.rs b/worker-sandbox/src/assets.rs new file mode 100644 index 00000000..4792d1ed --- /dev/null +++ b/worker-sandbox/src/assets.rs @@ -0,0 +1,17 @@ +#[cfg(not(feature = "http"))] +pub async fn handle_asset( + req: worker::Request, + env: worker::Env, + _data: crate::SomeSharedData, +) -> worker::Result { + use worker::Url; + + let url: Url = req.url()?; + let name: String = url.path_segments().unwrap().nth(1).unwrap().to_string(); + let url: String = ["https://dummyurl.com/", &name].concat(); + Ok(env + .assets("ASSETS") + .expect("ASSETS BINDING") + .fetch(url, None) + .await?) +} diff --git a/worker-sandbox/src/lib.rs b/worker-sandbox/src/lib.rs index 0441e969..24113c49 100644 --- a/worker-sandbox/src/lib.rs +++ b/worker-sandbox/src/lib.rs @@ -6,7 +6,9 @@ use std::sync::{ #[cfg(feature = "http")] use tower_service::Service; use worker::*; + mod alarm; +mod assets; mod cache; mod counter; mod d1; diff --git a/worker-sandbox/src/router.rs b/worker-sandbox/src/router.rs index bcbd7a89..9e2b567a 100644 --- a/worker-sandbox/src/router.rs +++ b/worker-sandbox/src/router.rs @@ -1,5 +1,5 @@ use crate::{ - alarm, cache, d1, fetch, form, kv, queue, r2, request, service, socket, user, ws, + alarm, assets, cache, d1, fetch, form, kv, queue, r2, request, service, socket, user, ws, SomeSharedData, GLOBAL_STATE, }; #[cfg(feature = "http")] @@ -228,6 +228,7 @@ pub fn make_router<'a>(data: SomeSharedData) -> Router<'a, SomeSharedData> { Router::with_data(data) .get("/request", handler_sync!(request::handle_a_request)) // can pass a fn pointer to keep routes tidy .get_async("/async-request", handler!(request::handle_async_request)) + .get_async("/asset/:name", handler!(assets::handle_asset)) .get_async("/websocket", handler!(ws::handle_websocket)) .get_async("/got-close-event", handler!(handle_close_event)) .get_async("/ws-client", handler!(ws::handle_websocket_client)) diff --git a/worker-sandbox/wrangler.toml b/worker-sandbox/wrangler.toml index 017b7ce0..18bd962c 100644 --- a/worker-sandbox/wrangler.toml +++ b/worker-sandbox/wrangler.toml @@ -11,6 +11,10 @@ kv_namespaces = [ vars = { SOME_VARIABLE = "some value" } +[assets] +binding = "ASSETS" +directory = "./public/" + [[services]] binding = "remote" service = "remote-service"