|
| 1 | +use std::fmt::Write as _; |
| 2 | +use std::path::Path; |
| 3 | +use std::process::ExitCode; |
| 4 | + |
| 5 | +use sha2::{Digest as _, Sha256}; |
| 6 | + |
| 7 | +const OUTPUT_DIR: &str = "target/wasm-policy-examples"; |
| 8 | +const POLICY_SOURCES: &[(&str, &str)] = &[ |
| 9 | + ( |
| 10 | + "irules-access-policy", |
| 11 | + include_str!("../../../examples/wasm/irules-access-policy.wat"), |
| 12 | + ), |
| 13 | + ( |
| 14 | + "openresty-header-policy", |
| 15 | + include_str!("../../../examples/wasm/openresty-header-policy.wat"), |
| 16 | + ), |
| 17 | + ( |
| 18 | + "haproxy-spoe-routing-policy", |
| 19 | + include_str!("../../../examples/wasm/haproxy-spoe-routing-policy.wat"), |
| 20 | + ), |
| 21 | + ( |
| 22 | + "cache-lookup-policy", |
| 23 | + include_str!("../../../examples/wasm/cache-lookup-policy.wat"), |
| 24 | + ), |
| 25 | + ( |
| 26 | + "cache-store-policy", |
| 27 | + include_str!("../../../examples/wasm/cache-store-policy.wat"), |
| 28 | + ), |
| 29 | +]; |
| 30 | + |
| 31 | +fn main() -> ExitCode { |
| 32 | + match build_examples() { |
| 33 | + Ok(()) => ExitCode::SUCCESS, |
| 34 | + Err(error) => { |
| 35 | + eprintln!("wasm policy example build failed: {error}"); |
| 36 | + ExitCode::FAILURE |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +fn build_examples() -> Result<(), Box<dyn std::error::Error>> { |
| 42 | + let output = Path::new(OUTPUT_DIR); |
| 43 | + std::fs::create_dir_all(output)?; |
| 44 | + let mut checksums = String::new(); |
| 45 | + |
| 46 | + for (name, source) in POLICY_SOURCES { |
| 47 | + let bytes = wat::parse_str(source)?; |
| 48 | + let file_name = format!("{name}.wasm"); |
| 49 | + std::fs::write(output.join(&file_name), &bytes)?; |
| 50 | + let digest = Sha256::digest(&bytes); |
| 51 | + for byte in digest { |
| 52 | + write!(&mut checksums, "{byte:02x}")?; |
| 53 | + } |
| 54 | + writeln!(&mut checksums, " {file_name}")?; |
| 55 | + } |
| 56 | + |
| 57 | + std::fs::write(output.join("SHA256SUMS"), checksums)?; |
| 58 | + println!("built Wasm policy examples in {OUTPUT_DIR}"); |
| 59 | + Ok(()) |
| 60 | +} |
0 commit comments