diff --git a/Cargo.lock b/Cargo.lock index 3b1b6b6c..7d249ba5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -356,6 +356,14 @@ dependencies = [ "syn", ] +[[package]] +name = "curl" +version = "0.1.0" +dependencies = [ + "fluence", + "log", +] + [[package]] name = "darling" version = "0.10.2" @@ -644,7 +652,7 @@ dependencies = [ [[package]] name = "fluence" version = "0.2.2" -source = "git+https://github.com/fluencelabs/rust-sdk#ff447a32586b3f088a57fe51b1e5baf3e17b5c12" +source = "git+https://github.com/fluencelabs/rust-sdk#dcb6cfddf95978e777a8a53710bda88aec7d4baf" dependencies = [ "fluence-sdk-macro 0.2.2 (git+https://github.com/fluencelabs/rust-sdk)", "fluence-sdk-main 0.2.2 (git+https://github.com/fluencelabs/rust-sdk)", @@ -680,14 +688,6 @@ dependencies = [ "wasmer-wasi-fl", ] -[[package]] -name = "fluence-sdk-macro" -version = "0.2.2" -source = "git+https://github.com/fluencelabs/rust-sdk#ff447a32586b3f088a57fe51b1e5baf3e17b5c12" -dependencies = [ - "fluence-sdk-wit 0.2.2 (git+https://github.com/fluencelabs/rust-sdk)", -] - [[package]] name = "fluence-sdk-macro" version = "0.2.2" @@ -698,13 +698,11 @@ dependencies = [ ] [[package]] -name = "fluence-sdk-main" +name = "fluence-sdk-macro" version = "0.2.2" -source = "git+https://github.com/fluencelabs/rust-sdk#ff447a32586b3f088a57fe51b1e5baf3e17b5c12" +source = "git+https://github.com/fluencelabs/rust-sdk#dcb6cfddf95978e777a8a53710bda88aec7d4baf" dependencies = [ - "fluence-sdk-macro 0.2.2 (git+https://github.com/fluencelabs/rust-sdk)", - "log", - "serde", + "fluence-sdk-wit 0.2.2 (git+https://github.com/fluencelabs/rust-sdk)", ] [[package]] @@ -718,10 +716,21 @@ dependencies = [ "serde", ] +[[package]] +name = "fluence-sdk-main" +version = "0.2.2" +source = "git+https://github.com/fluencelabs/rust-sdk#dcb6cfddf95978e777a8a53710bda88aec7d4baf" +dependencies = [ + "fluence-sdk-macro 0.2.2 (git+https://github.com/fluencelabs/rust-sdk)", + "log", + "serde", +] + [[package]] name = "fluence-sdk-wit" version = "0.2.2" -source = "git+https://github.com/fluencelabs/rust-sdk#ff447a32586b3f088a57fe51b1e5baf3e17b5c12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93c94643121eb234778b7f8f0e12fd29ec7c70ac71d84329ae25daf5d263074d" dependencies = [ "proc-macro2", "quote", @@ -734,8 +743,7 @@ dependencies = [ [[package]] name = "fluence-sdk-wit" version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "93c94643121eb234778b7f8f0e12fd29ec7c70ac71d84329ae25daf5d263074d" +source = "git+https://github.com/fluencelabs/rust-sdk#dcb6cfddf95978e777a8a53710bda88aec7d4baf" dependencies = [ "proc-macro2", "quote", @@ -884,9 +892,9 @@ dependencies = [ [[package]] name = "getrandom" -version = "0.1.14" +version = "0.1.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7abc8dd8451921606d809ba32e95b6111925cd2906060d2dcc29c070220503eb" +checksum = "fc587bc0ec293155d5bfa6b9891ec18a1e330c234f896ea47fbada4cadbe47e6" dependencies = [ "cfg-if", "libc", @@ -1146,9 +1154,9 @@ checksum = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6" [[package]] name = "js-sys" -version = "0.3.44" +version = "0.3.45" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85a7e2c92a4804dd459b86c339278d0fe87cf93757fae222c3fa3ae75458bc73" +checksum = "ca059e81d9486668f12d455a4ea6daa600bd408134cd17e3d3fb5a32d1f016f8" dependencies = [ "wasm-bindgen", ] @@ -1190,9 +1198,17 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.76" +version = "0.2.77" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "755456fae044e6fa1ebbbd1b3e902ae19e73097ed4ed87bb79934a867c007bc3" +checksum = "f2f96b10ec2560088a8e76961b00d47107b3a625fecb76dedb29ee7ccbf98235" + +[[package]] +name = "local_storage" +version = "0.1.0" +dependencies = [ + "fluence", + "log", +] [[package]] name = "lock_api" @@ -1559,9 +1575,9 @@ checksum = "c36fa947111f5c62a733b652544dd0016a43ce89619538a8ef92724a6f501a20" [[package]] name = "proc-macro2" -version = "1.0.20" +version = "1.0.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "175c513d55719db99da20232b06cda8bab6b83ec2d04e3283edf0213c37c1a29" +checksum = "36e28516df94f3dd551a587da5357459d9b36d945a7c37c3557928c1c2ff2a2c" dependencies = [ "unicode-xid", ] @@ -1961,6 +1977,15 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7fdf1b9db47230893d76faad238fd6097fd6d6a9245cd7a4d90dbd639536bbd2" +[[package]] +name = "site-storage" +version = "0.1.0" +dependencies = [ + "anyhow", + "fluence", + "log", +] + [[package]] name = "skim" version = "0.7.0" @@ -2000,9 +2025,9 @@ checksum = "fbee7696b84bbf3d89a1c2eccff0850e3047ed46bfcd2e92c29a2d074d57e252" [[package]] name = "socket2" -version = "0.3.12" +version = "0.3.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "03088793f677dce356f3ccc2edb1b314ad191ab702a5de3faf49304f7e104918" +checksum = "b1fa70dc5c8104ec096f4fe7ede7a221d35ae13dcd19ba1ad9a81d2cab9a1c44" dependencies = [ "cfg-if", "libc", @@ -2030,9 +2055,9 @@ checksum = "6446ced80d6c486436db5c078dde11a9f73d42b57fb273121e160b84f63d894c" [[package]] name = "subtle" -version = "2.2.3" +version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "502d53007c02d7605a05df1c1a73ee436952781653da5d0bf57ad608f66932c1" +checksum = "343f3f510c2915908f155e94f17220b19ccfacf2a64a2a5d8004f2c3e311e7fd" [[package]] name = "syn" @@ -2249,9 +2274,9 @@ dependencies = [ [[package]] name = "tracing-core" -version = "0.1.15" +version = "0.1.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4f0e00789804e99b20f12bc7003ca416309d28a6f495d6af58d1e2c2842461b5" +checksum = "5bcf46c1f1f06aeea2d6b81f3c863d0930a596c86ad1920d4e5bad6dd1d7119a" dependencies = [ "lazy_static", ] @@ -2466,9 +2491,9 @@ checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" [[package]] name = "wasm-bindgen" -version = "0.2.67" +version = "0.2.68" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0563a9a4b071746dd5aedbc3a28c6fe9be4586fb3fbadb67c400d4f53c6b16c" +checksum = "1ac64ead5ea5f05873d7c12b545865ca2b8d28adfc50a49b84770a3a97265d42" dependencies = [ "cfg-if", "serde", @@ -2478,9 +2503,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-backend" -version = "0.2.67" +version = "0.2.68" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bc71e4c5efa60fb9e74160e89b93353bc24059999c0ae0fb03affc39770310b0" +checksum = "f22b422e2a757c35a73774860af8e112bff612ce6cb604224e8e47641a9e4f68" dependencies = [ "bumpalo", "lazy_static", @@ -2493,9 +2518,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-futures" -version = "0.4.17" +version = "0.4.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95f8d235a77f880bcef268d379810ea6c0af2eacfa90b1ad5af731776e0c4699" +checksum = "b7866cab0aa01de1edf8b5d7936938a7e397ee50ce24119aef3e1eaa3b6171da" dependencies = [ "cfg-if", "js-sys", @@ -2505,9 +2530,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.67" +version = "0.2.68" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97c57cefa5fa80e2ba15641578b44d36e7a64279bc5ed43c6dbaf329457a2ed2" +checksum = "6b13312a745c08c469f0b292dd2fcd6411dba5f7160f593da6ef69b64e407038" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -2515,9 +2540,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.67" +version = "0.2.68" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "841a6d1c35c6f596ccea1f82504a192a60378f64b3bb0261904ad8f2f5657556" +checksum = "f249f06ef7ee334cc3b8ff031bfc11ec99d00f34d86da7498396dc1e3b1498fe" dependencies = [ "proc-macro2", "quote", @@ -2528,9 +2553,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.67" +version = "0.2.68" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "93b162580e34310e5931c4b792560108b10fd14d64915d7fff8ff00180e70092" +checksum = "1d649a3145108d7d3fbcde896a468d1bd636791823c9921135218ad89be08307" [[package]] name = "wasm-greeting" @@ -2731,9 +2756,9 @@ dependencies = [ [[package]] name = "web-sys" -version = "0.3.44" +version = "0.3.45" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dda38f4e5ca63eda02c059d243aa25b5f35ab98451e518c51612cd0f1bd19a47" +checksum = "4bf6ef87ad7ae8008e15a355ce696bed26012b7caa21605188cfd8214ab51e2d" dependencies = [ "js-sys", "wasm-bindgen", diff --git a/Cargo.toml b/Cargo.toml index 6cac8ed0..a2856ee7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,9 @@ members = [ "examples/records/effector", "examples/records/pure", "examples/records/test-record", + "examples/site-storage/wasm/site-storage", + "examples/site-storage/wasm/curl", + "examples/site-storage/wasm/local_storage", "fluence-app-service", "fluence-faas", "tools/cli", diff --git a/README.md b/README.md index c5fb9d7f..3a8a8fb9 100644 --- a/README.md +++ b/README.md @@ -46,19 +46,19 @@ pub fn get(url: String) -> String { - Build project with `fce build` (supports --release and all other cargo flags now) -- Copy wasm file from `target/wasm32-wasi/debug` to directory with other modules +- Copy wasm file from `target/wasm32-wasi/debug` or `target/wasm32-wasi/release` to directory with other modules - To import other wasm modules to your project use similar code: ```rust #[fce] -#[link(wasm_import_module = "wasm_curl.wasm")] +#[link(wasm_import_module = "curl")] extern "C" { #[link_name = "get"] pub fn curl_get(url: String) -> String; } #[fce] -#[link(wasm_import_module = "wasm_local_storage.wasm")] +#[link(wasm_import_module = "local_storage")] extern "C" { #[link_name = "get"] pub fn curl_get(url: String) -> String; @@ -70,19 +70,29 @@ extern "C" { - Create simple Rust project - Create `Config.toml` to describe existed wasm modules and give accesses to host binaries and local storage if needed: ```toml -modules_dir = "wasm/artifacts/modules" +modules_dir = "wasm/artifacts/modules/" [[module]] - name = "wasm_local_storage_with_curl.wasm" - mem_pages_count = 100 - - [module.imports] - curl = "/usr/bin/curl" + name = "local_storage" + logger_enabled = true [module.wasi] - envs = [] preopened_files = ["./wasm/artifacts"] - mapped_dirs = { "tmp" = "./wasm/artifacts" } + mapped_dirs = { "sites" = "./sites" } + +[[module]] + name = "curl" + logger_enabled = true + + [module.imports] + curl = "/usr/bin/curl" + +[[module]] + name = "site-storage" + mem_pages_count = 10000 + logger_enabled = true + [module.wasi] + envs = ["ENV_ONE=parameter-one"] ``` `modules_dir` - path to directory with all modules. All subsequent paths will be relative to this path @@ -99,6 +109,7 @@ modules_dir = "wasm/artifacts/modules" Import example: ```rust +#[fce] #[link(wasm_import_module = "host")] extern "C" { fn curl(args: String) -> String; diff --git a/examples/site-storage/Config.toml b/examples/site-storage/Config.toml new file mode 100644 index 00000000..ed6c15b2 --- /dev/null +++ b/examples/site-storage/Config.toml @@ -0,0 +1,21 @@ +modules_dir = "wasm/artifacts/modules/" + +[[module]] + name = "local_storage" + logger_enabled = true + + [module.wasi] + preopened_files = ["./../sites"] + # this is where files will be stored + mapped_dirs = { "sites" = "./../sites" } + +[[module]] + name = "curl" + logger_enabled = true + + [module.imports] + curl = "/usr/bin/curl" + +[[module]] + name = "site-storage" + logger_enabled = true diff --git a/examples/site-storage/build.sh b/examples/site-storage/build.sh new file mode 100755 index 00000000..81ecf1d2 --- /dev/null +++ b/examples/site-storage/build.sh @@ -0,0 +1,18 @@ +#!/bin/sh + +# This script builds all subprojects and puts all created Wasm modules in one dir +cd wasm/site-storage +cargo update +fce build --release +cd ../curl +cargo update +fce build --release +cd ../local_storage +cargo update +fce build --release + +cd .. +rm artifacts/modules/* +cp ../../../target/wasm32-wasi/release/curl.wasm artifacts/modules +cp ../../../target/wasm32-wasi/release/local_storage.wasm artifacts/modules +cp ../../../target/wasm32-wasi/release/site-storage.wasm artifacts/modules diff --git a/examples/site-storage/wasm/artifacts/modules/site-storage.wasm b/examples/site-storage/wasm/artifacts/modules/site-storage.wasm new file mode 100755 index 00000000..b954f04f Binary files /dev/null and b/examples/site-storage/wasm/artifacts/modules/site-storage.wasm differ diff --git a/examples/site-storage/wasm/curl/Cargo.toml b/examples/site-storage/wasm/curl/Cargo.toml new file mode 100644 index 00000000..6a951b9c --- /dev/null +++ b/examples/site-storage/wasm/curl/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "curl" +version = "0.1.0" +authors = ["Fluence Labs"] +edition = "2018" + +[[bin]] +path = "src/main.rs" +name = "curl" + +[dependencies] +fluence = { git = "https://github.com/fluencelabs/rust-sdk", features = ["logger"] } +log = "0.4.8" diff --git a/examples/site-storage/wasm/curl/src/main.rs b/examples/site-storage/wasm/curl/src/main.rs new file mode 100644 index 00000000..c392645c --- /dev/null +++ b/examples/site-storage/wasm/curl/src/main.rs @@ -0,0 +1,37 @@ +/* + * Copyright 2020 Fluence Labs Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +use fluence::fce; +use fluence::WasmLogger; + +/// Log level can be changed by `RUST_LOG` env as well. +pub fn main() { + WasmLogger::init_with_level(log::Level::Info).unwrap(); +} + +#[fce] +pub fn get(cmd: String) -> String { + log::info!("get called with url {}", cmd); + + unsafe { curl(cmd) } +} + +/// Permissions in `Config.toml` should exist to use host functions. +#[fce] +#[link(wasm_import_module = "host")] +extern "C" { + fn curl(cmd: String) -> String; +} diff --git a/examples/site-storage/wasm/local_storage/Cargo.toml b/examples/site-storage/wasm/local_storage/Cargo.toml new file mode 100644 index 00000000..8c04809e --- /dev/null +++ b/examples/site-storage/wasm/local_storage/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "local_storage" +version = "0.1.0" +authors = ["Fluence Labs"] +edition = "2018" + +[[bin]] +name = "local_storage" +path = "src/main.rs" + +[dependencies] +fluence = { git = "https://github.com/fluencelabs/rust-sdk", features = ["logger"] } +log = "0.4.8" diff --git a/examples/site-storage/wasm/local_storage/src/main.rs b/examples/site-storage/wasm/local_storage/src/main.rs new file mode 100644 index 00000000..1dfe1266 --- /dev/null +++ b/examples/site-storage/wasm/local_storage/src/main.rs @@ -0,0 +1,51 @@ +/* + * Copyright 2020 Fluence Labs Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +use std::fs; +use fluence::fce; +use fluence::WasmLogger; +use std::path::PathBuf; + +const RPC_TMP_FILEPATH: &str = "/sites/"; + +/// Log level can be changed by `RUST_LOG` env as well. +pub fn main() { + WasmLogger::init_with_level(log::Level::Info).unwrap(); +} + +/// You can read or write files from the file system if there is permission to use directories described in `Config.toml`. +#[fce] +pub fn put(name: String, file_content: Vec) -> String { + log::info!("put called with {:?}", file_content); + + let rpc_tmp_filepath = format!("{}{}", RPC_TMP_FILEPATH, name); + + let result = fs::write(PathBuf::from(rpc_tmp_filepath.clone()), file_content); + if let Err(e) = result { + return format!("file can't be written: {}", e); + } + + return "Ok".to_string(); +} + +#[fce] +pub fn get(file_name: String) -> Vec { + log::info!("get called with file name: {}", file_name); + + let tmp_filepath = format!("{}{}", RPC_TMP_FILEPATH, file_name); + + fs::read(tmp_filepath).unwrap_or_else(|_| b"error while reading file".to_vec()) +} diff --git a/examples/site-storage/wasm/site-storage/Cargo.toml b/examples/site-storage/wasm/site-storage/Cargo.toml new file mode 100644 index 00000000..dea8018a --- /dev/null +++ b/examples/site-storage/wasm/site-storage/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "site-storage" +version = "0.1.0" +authors = ["Fluence Labs"] +edition = "2018" + +[[bin]] +name = "site-storage" +path = "src/main.rs" + +[dependencies] +fluence = { git = "https://github.com/fluencelabs/rust-sdk", features = ["logger"] } +anyhow = "1.0.31" +log = "0.4.8" diff --git a/examples/site-storage/wasm/site-storage/src/main.rs b/examples/site-storage/wasm/site-storage/src/main.rs new file mode 100644 index 00000000..ebad3f33 --- /dev/null +++ b/examples/site-storage/wasm/site-storage/src/main.rs @@ -0,0 +1,53 @@ +/* + * Copyright 2020 Fluence Labs Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +use fluence::fce; +use fluence::WasmLogger; + +pub fn main() { + WasmLogger::init_with_level(log::Level::Info).unwrap(); +} + +/// Combining of modules: `curl` and `local_storage`. +/// Calls `curl` and stores returned result into a file. +#[fce] +fn get_n_save(url: String, file_name: String) -> String { + let result = unsafe { curl(url) }; + println!("execution result {:?}", result); + let a = unsafe { file_put(file_name, result.into_bytes()) }; + println!("{}", a); + + "Ok".to_string() +} + +/// Importing `curl` module +#[fce] +#[link(wasm_import_module = "curl")] +extern "C" { + #[link_name = "get"] + pub fn curl(url: String) -> String; +} + +/// Importing `local_storage` module +#[fce] +#[link(wasm_import_module = "local_storage")] +extern "C" { + #[link_name = "get"] + pub fn file_get(file_name: String) -> Vec; + + #[link_name = "put"] + pub fn file_put(name: String, file_content: Vec) -> String; +}