-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated to require workers 0.0.20. (#24)
* 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
Showing
8 changed files
with
78 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |