diff --git a/worker/.gitignore b/worker/.gitignore new file mode 100644 index 0000000..d27315f --- /dev/null +++ b/worker/.gitignore @@ -0,0 +1,3 @@ +target +node_modules +.wrangler diff --git a/worker/Cargo.toml b/worker/Cargo.toml new file mode 100644 index 0000000..8457439 --- /dev/null +++ b/worker/Cargo.toml @@ -0,0 +1,22 @@ +[package] +name = "zhconv-worker" +version = "0.1.0" +edition = "2021" +authors = [ "Hung-I Wang " ] + +[package.metadata.release] +release = false + +# https://github.com/rustwasm/wasm-pack/issues/1247 +[package.metadata.wasm-pack.profile.release] +wasm-opt = false + +[lib] +crate-type = ["cdylib"] + +[dependencies] +worker = { version="0.4.2", features=['http', 'axum'] } +worker-macros = { version="0.4.2", features=['http'] } +axum = { version = "0.7", default-features = false } +tower-service = "0.3.2" +console_error_panic_hook = { version = "0.1.1" } diff --git a/worker/src/lib.rs b/worker/src/lib.rs new file mode 100644 index 0000000..08a2fc2 --- /dev/null +++ b/worker/src/lib.rs @@ -0,0 +1,21 @@ +use axum::{routing::get, Router}; +use tower_service::Service; +use worker::*; + +fn router() -> Router { + Router::new().route("/", get(root)) +} + +#[event(fetch)] +async fn fetch( + req: HttpRequest, + _env: Env, + _ctx: Context, +) -> Result> { + console_error_panic_hook::set_once(); + Ok(router().call(req).await?) +} + +pub async fn root() -> &'static str { + "Hello Axum!" +} diff --git a/worker/wrangler.toml b/worker/wrangler.toml new file mode 100644 index 0000000..d5ca8ee --- /dev/null +++ b/worker/wrangler.toml @@ -0,0 +1,6 @@ +name = "worker" +main = "build/worker/shim.mjs" +compatibility_date = "2024-11-29" + +[build] +command = "cargo install -q worker-build && worker-build --release" \ No newline at end of file