Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workers Assets Binding #656

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
17 changes: 17 additions & 0 deletions worker-sandbox/src/assets.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#[cfg(not(feature = "http"))]
pub async fn handle_asset(
req: worker::Request,
env: worker::Env,
_data: crate::SomeSharedData,
) -> worker::Result<worker::Response> {
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?)
}
2 changes: 2 additions & 0 deletions worker-sandbox/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion worker-sandbox/src/router.rs
Original file line number Diff line number Diff line change
@@ -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")]
Expand Down Expand Up @@ -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))
Expand Down
4 changes: 4 additions & 0 deletions worker-sandbox/wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ kv_namespaces = [

vars = { SOME_VARIABLE = "some value" }

[assets]
binding = "ASSETS"
directory = "./public/"

[[services]]
binding = "remote"
service = "remote-service"
Expand Down
5 changes: 5 additions & 0 deletions worker/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Fetcher> {
self.get_binding(binding)
}

pub fn hyperdrive(&self, binding: &str) -> Result<Hyperdrive> {
self.get_binding(binding)
}
Expand Down