Skip to content

Commit

Permalink
Updated to require workers 0.0.20. (#24)
Browse files Browse the repository at this point in the history
* Updated to require workers 0.0.19.

* Updated to latest stable rust tool chain 1.76.

* Updated to latest stable wrangler 3.31.0.

* Migrated example.

* Updated to version workers-rs 0.0.20.

* Added wasm-bindgen-test-runner to the cargo config.

* Updated to the latest wrangler 3.32.0.

* Added resolver and edition to the workspace configuration.
  • Loading branch information
spigaz authored Mar 13, 2024
1 parent bca9594 commit e31419e
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 74 deletions.
3 changes: 3 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
[build]
target = "wasm32-unknown-unknown"

[target.wasm32-unknown-unknown]
runner = 'wasm-bindgen-test-runner'
4 changes: 2 additions & 2 deletions .github/workflows/build-worker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ jobs:
node-version-file: '.nvmrc'

- name: Install Wrangler
run: npm install -g wrangler@2.20.1
run: npm install -g wrangler@3.32.0

- name: Install Rust
uses: actions-rs/toolchain@v1
with:
toolchain: 1.70.0
toolchain: 1.76.0
target: wasm32-unknown-unknown
override: true

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- name: Install Rust
uses: actions-rs/toolchain@v1
with:
toolchain: 1.66.0
toolchain: 1.76.0
target: wasm32-unknown-unknown
override: true

Expand All @@ -34,4 +34,4 @@ jobs:
- name: Test
run: |
cd adapter
wasm-pack test --firefox --headless
wasm-pack test --firefox --headless
8 changes: 3 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
[workspace]
edition = "2021"
resolver = "2"

members = [
"adapter",
"example",
"macros"
]
members = ["adapter", "example", "macros"]
2 changes: 1 addition & 1 deletion adapter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ crate-type = ["cdylib", "lib"]

[dependencies]
axum = { version = "^0.7.1", default-features = false }
worker = { version = "^0.0.18" }
worker = { version = "^0.0.20" }
axum-wasm-macros = "^0.1.0"
futures = "0.3.29"

Expand Down
16 changes: 6 additions & 10 deletions adapter/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
use axum::http::uri::InvalidUri;
use axum::http::method::InvalidMethod;
use axum::http::Error as HttpError;
use axum::http::header::InvalidHeaderName;
use axum::http::header::InvalidHeaderValue;
use axum::http::header::ToStrError;
use axum::{
Error as AxumError,
};
use worker::{
Error as WorkerError,
};
use axum::http::method::InvalidMethod;
use axum::http::uri::InvalidUri;
use axum::http::Error as HttpError;
use axum::Error as AxumError;
use worker::Error as WorkerError;

#[derive(Debug)]
// #[non_exhaustive]
Expand Down Expand Up @@ -70,4 +66,4 @@ impl From<InvalidHeaderValue> for Error {
fn from(err: InvalidHeaderValue) -> Error {
Error::InvalidHeaderValue(err)
}
}
}
2 changes: 1 addition & 1 deletion example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ serde_json = "1.0.108"
tower-service = "0.3.2"
url = "2.3.1"
wasm-bindgen-futures = "0.4.34"
worker = "0.0.18"
worker = "0.0.20"

# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
Expand Down
113 changes: 60 additions & 53 deletions example/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,86 +1,93 @@
use std::ops::Deref;
use std::str::FromStr;
use axum::http::header::CONTENT_TYPE;
use axum::{
extract::{Path, State},
routing::get,
Router as AxumRouter,
response::IntoResponse,
extract::{Path, State},
response::IntoResponse,
routing::get,
Router as AxumRouter,
};
use axum::http::header::CONTENT_TYPE;
use axum_cloudflare_adapter::{EnvWrapper, to_axum_request, to_worker_response, wasm_compat};
use axum_cloudflare_adapter::{to_axum_request, to_worker_response, wasm_compat, EnvWrapper};
use std::ops::Deref;
use std::str::FromStr;
use tower_service::Service;
use worker::{console_log, Env, Request, Response, Date, Result, event, Var};
use worker::{console_log, event, Date, Env, Request, Response, Result, Var};

mod utils;

fn log_request(req: &Request) {
console_log!(
"{} - [{}], located at: {:?}, within: {}",
Date::now().to_string(),
req.path(),
req.cf().coordinates().unwrap_or_default(),
req.cf().region().unwrap_or_else(|| "unknown region".into())
);
if let Some(cf) = req.cf() {
console_log!(
"{} - [{}], located at: {:?}, within: {}",
Date::now().to_string(),
req.path(),
cf.coordinates().unwrap_or_default(),
cf.region().unwrap_or_else(|| "unknown region".into())
);
} else {
console_log!(
"{} - [{}], from unknown location",
Date::now().to_string(),
req.path(),
);
}
}

use url::Url;

#[wasm_compat]
pub async fn index(State(state): State<AxumState>) -> impl IntoResponse {
let url = Url::from_str("https://logankeenan.com").unwrap();
let mut response = worker::Fetch::Url(url).send().await.unwrap();
let body_text = response.text().await.unwrap();
let url = Url::from_str("https://logankeenan.com").unwrap();
let mut response = worker::Fetch::Url(url).send().await.unwrap();
let body_text = response.text().await.unwrap();

let env: &Env = state.env_wrapper.env.deref();
let worker_rs_version: Var = env.var("WORKERS_RS_VERSION").unwrap();
let env: &Env = state.env_wrapper.env.deref();
let worker_rs_version: Var = env.var("WORKERS_RS_VERSION").unwrap();

console_log!("WORKERS_RS_VERSION: {}", worker_rs_version.to_string());
console_log!("WORKERS_RS_VERSION: {}", worker_rs_version.to_string());

let content_type = response.headers().get("content-type").unwrap().unwrap();
axum::response::Response::builder()
.header(CONTENT_TYPE, content_type)
.body(body_text)
.unwrap()
let content_type = response.headers().get("content-type").unwrap().unwrap();
axum::response::Response::builder()
.header(CONTENT_TYPE, content_type)
.body(body_text)
.unwrap()
}

#[wasm_compat]
pub async fn with_pathname(Path(path): Path<String>) -> impl IntoResponse {
let mut url = Url::from_str("https://logankeenan.com").unwrap();
url.set_path(path.as_str());
let mut response = worker::Fetch::Url(url).send().await.unwrap();
let body_text = response.text().await.unwrap();

let content_type = response.headers().get("content-type").unwrap().unwrap();
axum::response::Response::builder()
.header(CONTENT_TYPE, content_type)
.body(body_text)
.unwrap()
let mut url = Url::from_str("https://logankeenan.com").unwrap();
url.set_path(path.as_str());
let mut response = worker::Fetch::Url(url).send().await.unwrap();
let body_text = response.text().await.unwrap();

let content_type = response.headers().get("content-type").unwrap().unwrap();
axum::response::Response::builder()
.header(CONTENT_TYPE, content_type)
.body(body_text)
.unwrap()
}

#[derive(Clone)]
pub struct AxumState {
pub env_wrapper: EnvWrapper,
pub env_wrapper: EnvWrapper,
}

#[event(fetch)]
pub async fn main(req: Request, env: Env, _ctx: worker::Context) -> Result<Response> {
log_request(&req);
// Optionally, get more helpful error messages written to the console in the case of a panic.
utils::set_panic_hook();

let axum_state = AxumState {
env_wrapper: EnvWrapper::new(env),
};
log_request(&req);
// Optionally, get more helpful error messages written to the console in the case of a panic.
utils::set_panic_hook();

let mut _router: AxumRouter = AxumRouter::new()
.route("/", get(index))
.route("/*path", get(with_pathname))
.with_state(axum_state);
let axum_state = AxumState {
env_wrapper: EnvWrapper::new(env),
};

let axum_request = to_axum_request(req).await.unwrap();
let axum_response = _router.call(axum_request).await.unwrap();
let response = to_worker_response(axum_response).await.unwrap();
let mut _router: AxumRouter = AxumRouter::new()
.route("/", get(index))
.route("/*path", get(with_pathname))
.with_state(axum_state);

let axum_request = to_axum_request(req).await.unwrap();
let axum_response = _router.call(axum_request).await.unwrap();
let response = to_worker_response(axum_response).await.unwrap();

Ok(response)
Ok(response)
}

0 comments on commit e31419e

Please sign in to comment.