mirror of
https://github.com/fluencelabs/examples
synced 2025-06-12 09:31:20 +00:00
replace url-downloader with a new one
This commit is contained in:
@ -1,21 +0,0 @@
|
|||||||
modules_dir = "artifacts/"
|
|
||||||
|
|
||||||
[[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_adapter"
|
|
||||||
logger_enabled = true
|
|
||||||
|
|
||||||
[module.mounted_binaries]
|
|
||||||
curl = "/usr/bin/curl"
|
|
||||||
|
|
||||||
[[module]]
|
|
||||||
name = "facade"
|
|
||||||
logger_enabled = true
|
|
@ -1,24 +0,0 @@
|
|||||||
#!/bin/sh -euo pipefail
|
|
||||||
|
|
||||||
# This script builds all subprojects and puts all created Wasm modules in one dir
|
|
||||||
(
|
|
||||||
cd local_storage
|
|
||||||
cargo update
|
|
||||||
fce build --release
|
|
||||||
)
|
|
||||||
(
|
|
||||||
cd curl_adapter
|
|
||||||
cargo update
|
|
||||||
fce build --release
|
|
||||||
)
|
|
||||||
(
|
|
||||||
cd facade
|
|
||||||
cargo update
|
|
||||||
fce build --release
|
|
||||||
)
|
|
||||||
|
|
||||||
mkdir -p artifacts
|
|
||||||
rm -f artifacts/*.wasm
|
|
||||||
cp local_storage/target/wasm32-wasi/release/local_storage.wasm artifacts/
|
|
||||||
cp curl_adapter/target/wasm32-wasi/release/curl_adapter.wasm artifacts/
|
|
||||||
cp facade/target/wasm32-wasi/release/facade.wasm artifacts/
|
|
@ -1,14 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "curl_adapter"
|
|
||||||
version = "0.1.0"
|
|
||||||
authors = ["Fluence Labs"]
|
|
||||||
edition = "2018"
|
|
||||||
publish = false
|
|
||||||
|
|
||||||
[[bin]]
|
|
||||||
path = "src/main.rs"
|
|
||||||
name = "curl_adapter"
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
fluence = { version = "=0.3.3", features = ["logger"] }
|
|
||||||
log = "0.4.8"
|
|
@ -1,48 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#![allow(improper_ctypes)]
|
|
||||||
|
|
||||||
use fluence::fce;
|
|
||||||
|
|
||||||
use fluence::WasmLoggerBuilder;
|
|
||||||
use fluence::MountedBinaryResult as Result;
|
|
||||||
use fluence::MountedBinaryStringResult as StringResult;
|
|
||||||
|
|
||||||
/// Log level can be changed by `RUST_LOG` env as well.
|
|
||||||
pub fn main() {
|
|
||||||
WasmLoggerBuilder::new().build().unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
#[fce]
|
|
||||||
pub fn request(url: String) -> StringResult {
|
|
||||||
unsafe { curl(vec![url]) }.stringify().unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[fce]
|
|
||||||
pub fn download(url: String) -> Result {
|
|
||||||
log::info!("download called with url {}", url);
|
|
||||||
|
|
||||||
unsafe { curl(vec![url]) }
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// Permissions in `Config.toml` should exist to use host functions.
|
|
||||||
#[fce]
|
|
||||||
#[link(wasm_import_module = "host")]
|
|
||||||
extern "C" {
|
|
||||||
fn curl(cmd: Vec<String>) -> Result;
|
|
||||||
}
|
|
@ -1,15 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "facade"
|
|
||||||
version = "0.1.0"
|
|
||||||
authors = ["Fluence Labs"]
|
|
||||||
edition = "2018"
|
|
||||||
publish = false
|
|
||||||
|
|
||||||
[[bin]]
|
|
||||||
name = "facade"
|
|
||||||
path = "src/main.rs"
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
fluence = { version = "=0.3.3", features = ["logger"]}
|
|
||||||
log = "0.4.8"
|
|
||||||
base64 = "0.13.0"
|
|
@ -1,63 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#![allow(improper_ctypes)]
|
|
||||||
|
|
||||||
use fluence::fce;
|
|
||||||
use fluence::MountedBinaryResult as Result;
|
|
||||||
use fluence::WasmLoggerBuilder;
|
|
||||||
|
|
||||||
pub fn main() {
|
|
||||||
WasmLoggerBuilder::new().build().unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Combining of modules: `curl` and `local_storage`.
|
|
||||||
/// Calls `curl` and stores returned result into a file.
|
|
||||||
#[fce]
|
|
||||||
pub fn get_n_save(url: String, file_name: String) -> i32 {
|
|
||||||
let result = unsafe { download(url) };
|
|
||||||
if result.is_success() {
|
|
||||||
log::info!("saving file {}", file_name);
|
|
||||||
unsafe { file_put(file_name, result.stdout) };
|
|
||||||
} else {
|
|
||||||
log::error!("download failed: {:#?}", result.as_std())
|
|
||||||
}
|
|
||||||
|
|
||||||
result.ret_code
|
|
||||||
}
|
|
||||||
|
|
||||||
#[fce]
|
|
||||||
pub fn load_file(file_name: String) -> String {
|
|
||||||
let bytes = unsafe { file_get(file_name) };
|
|
||||||
base64::encode(bytes)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Import `curl_adapter` module
|
|
||||||
#[fce]
|
|
||||||
#[link(wasm_import_module = "curl_adapter")]
|
|
||||||
extern "C" {
|
|
||||||
pub fn download(url: String) -> Result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Import `local_storage` module
|
|
||||||
#[fce]
|
|
||||||
#[link(wasm_import_module = "local_storage")]
|
|
||||||
extern "C" {
|
|
||||||
#[link_name = "get"]
|
|
||||||
pub fn file_get(file_name: String) -> Vec<u8>;
|
|
||||||
|
|
||||||
#[link_name = "put"]
|
|
||||||
pub fn file_put(name: String, file_content: Vec<u8>) -> String;
|
|
||||||
}
|
|
@ -1,14 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "local_storage"
|
|
||||||
version = "0.1.0"
|
|
||||||
authors = ["Fluence Labs"]
|
|
||||||
edition = "2018"
|
|
||||||
publish = false
|
|
||||||
|
|
||||||
[[bin]]
|
|
||||||
name = "local_storage"
|
|
||||||
path = "src/main.rs"
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
fluence = { version = "=0.3.3", features = ["logger"]}
|
|
||||||
log = "0.4.8"
|
|
@ -1,51 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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::WasmLoggerBuilder;
|
|
||||||
use std::path::PathBuf;
|
|
||||||
|
|
||||||
const SITES_DIR: &str = "/sites/";
|
|
||||||
|
|
||||||
/// Log level can be changed by `RUST_LOG` env as well.
|
|
||||||
pub fn main() {
|
|
||||||
WasmLoggerBuilder::new().build().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<u8>) -> String {
|
|
||||||
log::info!("put called with file name {}", name);
|
|
||||||
|
|
||||||
let rpc_tmp_filepath = format!("{}{}", SITES_DIR, 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);
|
|
||||||
}
|
|
||||||
|
|
||||||
String::from("Ok")
|
|
||||||
}
|
|
||||||
|
|
||||||
#[fce]
|
|
||||||
pub fn get(file_name: String) -> Vec<u8> {
|
|
||||||
log::info!("get called with file name: {}", file_name);
|
|
||||||
|
|
||||||
let tmp_filepath = format!("{}{}", SITES_DIR, file_name);
|
|
||||||
|
|
||||||
fs::read(tmp_filepath).unwrap_or_else(|_| b"error while reading file".to_vec())
|
|
||||||
}
|
|
@ -1,18 +1,24 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh -euo pipefail
|
||||||
|
|
||||||
# This script builds all subprojects and puts all created Wasm modules in one dir
|
# This script builds all subprojects and puts all created Wasm modules in one dir
|
||||||
cd local_storage
|
(
|
||||||
cargo update
|
cd local_storage
|
||||||
fce build --release
|
cargo update
|
||||||
cd ../curl_adapter
|
fce build --release
|
||||||
cargo update
|
)
|
||||||
fce build --release
|
(
|
||||||
cd ../facade
|
cd curl_adapter
|
||||||
cargo update
|
cargo update
|
||||||
fce build --release
|
fce build --release
|
||||||
|
)
|
||||||
|
(
|
||||||
|
cd facade
|
||||||
|
cargo update
|
||||||
|
fce build --release
|
||||||
|
)
|
||||||
|
|
||||||
cd ..
|
mkdir -p artifacts
|
||||||
rm -f artifacts/*
|
rm -f artifacts/*.wasm
|
||||||
cp ../../target/wasm32-wasi/release/local_storage.wasm artifacts/
|
cp local_storage/target/wasm32-wasi/release/local_storage.wasm artifacts/
|
||||||
cp ../../target/wasm32-wasi/release/curl_adapter.wasm artifacts/
|
cp curl_adapter/target/wasm32-wasi/release/curl_adapter.wasm artifacts/
|
||||||
cp ../../target/wasm32-wasi/release/facade.wasm artifacts/
|
cp facade/target/wasm32-wasi/release/facade.wasm artifacts/
|
||||||
|
@ -10,5 +10,5 @@ path = "src/main.rs"
|
|||||||
name = "curl_adapter"
|
name = "curl_adapter"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
fluence = { version = "=0.3.2", features = ["logger"] }
|
fluence = { version = "=0.3.3", features = ["logger"] }
|
||||||
log = "0.4.8"
|
log = "0.4.8"
|
||||||
|
@ -19,7 +19,8 @@
|
|||||||
use fluence::fce;
|
use fluence::fce;
|
||||||
|
|
||||||
use fluence::WasmLoggerBuilder;
|
use fluence::WasmLoggerBuilder;
|
||||||
use fluence::MountedBinaryResult;
|
use fluence::MountedBinaryResult as Result;
|
||||||
|
use fluence::MountedBinaryStringResult as StringResult;
|
||||||
|
|
||||||
/// Log level can be changed by `RUST_LOG` env as well.
|
/// Log level can be changed by `RUST_LOG` env as well.
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
@ -27,16 +28,21 @@ pub fn main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[fce]
|
#[fce]
|
||||||
pub fn download(url: String) -> String {
|
pub fn request(url: String) -> StringResult {
|
||||||
log::info!("get called with url {}", url);
|
unsafe { curl(vec![url]) }.stringify().unwrap()
|
||||||
|
|
||||||
let result = unsafe { curl(vec![url]) };
|
|
||||||
String::from_utf8(result.stdout).unwrap()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[fce]
|
||||||
|
pub fn download(url: String) -> Result {
|
||||||
|
log::info!("download called with url {}", url);
|
||||||
|
|
||||||
|
unsafe { curl(vec![url]) }
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Permissions in `Config.toml` should exist to use host functions.
|
/// Permissions in `Config.toml` should exist to use host functions.
|
||||||
#[fce]
|
#[fce]
|
||||||
#[link(wasm_import_module = "host")]
|
#[link(wasm_import_module = "host")]
|
||||||
extern "C" {
|
extern "C" {
|
||||||
fn curl(cmd: Vec<String>) -> MountedBinaryResult;
|
fn curl(cmd: Vec<String>) -> Result;
|
||||||
}
|
}
|
||||||
|
@ -10,6 +10,6 @@ name = "facade"
|
|||||||
path = "src/main.rs"
|
path = "src/main.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
fluence = { version = "=0.3.2", features = ["logger"]}
|
fluence = { version = "=0.3.3", features = ["logger"]}
|
||||||
anyhow = "1.0.31"
|
|
||||||
log = "0.4.8"
|
log = "0.4.8"
|
||||||
|
base64 = "0.13.0"
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
#![allow(improper_ctypes)]
|
#![allow(improper_ctypes)]
|
||||||
|
|
||||||
use fluence::fce;
|
use fluence::fce;
|
||||||
|
use fluence::MountedBinaryResult as Result;
|
||||||
use fluence::WasmLoggerBuilder;
|
use fluence::WasmLoggerBuilder;
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
@ -25,21 +26,32 @@ pub fn main() {
|
|||||||
/// Combining of modules: `curl` and `local_storage`.
|
/// Combining of modules: `curl` and `local_storage`.
|
||||||
/// Calls `curl` and stores returned result into a file.
|
/// Calls `curl` and stores returned result into a file.
|
||||||
#[fce]
|
#[fce]
|
||||||
pub fn get_n_save(url: String, file_name: String) -> String {
|
pub fn get_n_save(url: String, file_name: String) -> i32 {
|
||||||
let result = unsafe { download(url) };
|
let result = unsafe { download(url) };
|
||||||
unsafe { file_put(file_name, result.into_bytes()) };
|
if result.is_success() {
|
||||||
|
log::info!("saving file {}", file_name);
|
||||||
|
unsafe { file_put(file_name, result.stdout) };
|
||||||
|
} else {
|
||||||
|
log::error!("download failed: {:#?}", result.as_std())
|
||||||
|
}
|
||||||
|
|
||||||
String::from("Ok")
|
result.ret_code
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Importing `curl` module
|
#[fce]
|
||||||
|
pub fn load_file(file_name: String) -> String {
|
||||||
|
let bytes = unsafe { file_get(file_name) };
|
||||||
|
base64::encode(bytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Import `curl_adapter` module
|
||||||
#[fce]
|
#[fce]
|
||||||
#[link(wasm_import_module = "curl_adapter")]
|
#[link(wasm_import_module = "curl_adapter")]
|
||||||
extern "C" {
|
extern "C" {
|
||||||
pub fn download(url: String) -> String;
|
pub fn download(url: String) -> Result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Importing `local_storage` module
|
/// Import `local_storage` module
|
||||||
#[fce]
|
#[fce]
|
||||||
#[link(wasm_import_module = "local_storage")]
|
#[link(wasm_import_module = "local_storage")]
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
@ -10,5 +10,5 @@ name = "local_storage"
|
|||||||
path = "src/main.rs"
|
path = "src/main.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
fluence = { version = "=0.3.2", features = ["logger"]}
|
fluence = { version = "=0.3.3", features = ["logger"]}
|
||||||
log = "0.4.8"
|
log = "0.4.8"
|
||||||
|
Reference in New Issue
Block a user