From 575fff38a31885d92e831b68bee7954c06ecc2fd Mon Sep 17 00:00:00 2001 From: folex <0xdxdy@gmail.com> Date: Fri, 26 Feb 2021 02:57:17 +0300 Subject: [PATCH] upload & download works --- curl_template/README.md | 2 +- curl_template/{air.clj => request.air} | 0 url-downloader/artifacts/local_storage.json | 13 ++++++++++++- url-downloader/deploy.sh | 4 ++-- url-downloader/download.air | 10 ++++++++++ url-downloader/download.clj | 7 ------- url-downloader/facade/src/main.rs | 10 +++++----- url-downloader/local_storage/src/main.rs | 16 ++++++++-------- url-downloader/repl_config.toml | 21 +++++++++++++++++++++ 9 files changed, 59 insertions(+), 24 deletions(-) rename curl_template/{air.clj => request.air} (100%) create mode 100644 url-downloader/download.air delete mode 100644 url-downloader/download.clj create mode 100644 url-downloader/repl_config.toml diff --git a/curl_template/README.md b/curl_template/README.md index 739b676..20d362e 100644 --- a/curl_template/README.md +++ b/curl_template/README.md @@ -29,7 +29,7 @@ P.S. JSON5 has comments! yaaay! # Call it ```shell -fldist run_air -p air.clj -d '{"service": "e90bfbaf-ede7-4fbe-b45a-6250bf36ed3e"}' +fldist run_air -p request.air -d '{"service": "e90bfbaf-ede7-4fbe-b45a-6250bf36ed3e"}' ``` # Run frontend diff --git a/curl_template/air.clj b/curl_template/request.air similarity index 100% rename from curl_template/air.clj rename to curl_template/request.air diff --git a/url-downloader/artifacts/local_storage.json b/url-downloader/artifacts/local_storage.json index abc5f5f..c4c3ad2 100644 --- a/url-downloader/artifacts/local_storage.json +++ b/url-downloader/artifacts/local_storage.json @@ -1 +1,12 @@ -{"name": "local_storage"} +{ + "name": "local_storage", + "preopenedFiles": [ + "/tmp" + ], + "mappedDirs": { + "sites": "/tmp" + }, + "mountedBinaries": { + "curl": "/usr/bin/curl" + } +} diff --git a/url-downloader/deploy.sh b/url-downloader/deploy.sh index 6df7021..e46af7b 100755 --- a/url-downloader/deploy.sh +++ b/url-downloader/deploy.sh @@ -1,11 +1,11 @@ #!/bin/sh -euo pipefail # build wasms -sh build.sh +./build.sh ( cd artifacts - fldist new_service --name "url_downloader" --modules \ + fldist new_service --env dev --name "url_downloader" --modules \ curl_adapter.wasm:curl_adapter.json \ local_storage.wasm:local_storage.json \ facade.wasm:facade.json diff --git a/url-downloader/download.air b/url-downloader/download.air new file mode 100644 index 0000000..d8a1334 --- /dev/null +++ b/url-downloader/download.air @@ -0,0 +1,10 @@ +(xor + (seq + (seq + (call relay (service "get_n_save") ["https://fluence.network/img/svg/logo_new.svg" "logo.svg"] ret_code) + (call relay (service "load_file") ["logo.svg"] bytes) + ) + (call %init_peer_id% (returnService "run") [ret_code bytes]) + ) + (call %init_peer_id% (returnService "run") [%last_error%]) +) diff --git a/url-downloader/download.clj b/url-downloader/download.clj deleted file mode 100644 index 1ec37b1..0000000 --- a/url-downloader/download.clj +++ /dev/null @@ -1,7 +0,0 @@ -(xor - (seq - (call relay (service "request") ["https://api.duckduckgo.com/?q=homotopy&format=json"] result) - (call %init_peer_id% (returnService "run") [result]) - ) - (call %init_peer_id% (returnService "run") [%last_error%]) -) diff --git a/url-downloader/facade/src/main.rs b/url-downloader/facade/src/main.rs index 1a5e04c..cc76c5d 100644 --- a/url-downloader/facade/src/main.rs +++ b/url-downloader/facade/src/main.rs @@ -26,19 +26,19 @@ pub fn main() { /// 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 { +pub fn get_n_save(url: String, file_name: String) -> String { let result = unsafe { download(url) }; if result.is_success() { log::info!("saving file {}", file_name); - unsafe { file_put(file_name, result.stdout) }; + unsafe { file_put(file_name, result.stdout) } } else { - log::error!("download failed: {:#?}", result.as_std()) + log::error!("download failed: {:#?}", result.as_std()); + format!("download failed: {:#?}", result.as_std()) } - - result.ret_code } #[fce] +/// Loads file from disk and returns its content as base64 pub fn load_file(file_name: String) -> String { let bytes = unsafe { file_get(file_name) }; base64::encode(bytes) diff --git a/url-downloader/local_storage/src/main.rs b/url-downloader/local_storage/src/main.rs index 015ddc9..0f50966 100644 --- a/url-downloader/local_storage/src/main.rs +++ b/url-downloader/local_storage/src/main.rs @@ -17,7 +17,7 @@ use std::fs; use fluence::fce; use fluence::WasmLoggerBuilder; -use std::path::PathBuf; +use std::path::Path; const SITES_DIR: &str = "/sites/"; @@ -28,14 +28,14 @@ pub fn main() { /// 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 name {}", name); +pub fn put(file_name: String, file_content: Vec) -> String { + log::info!("put called with file name {}", file_name); - let rpc_tmp_filepath = format!("{}{}", SITES_DIR, name); + let path = Path::new(SITES_DIR).join(file_name); - let result = fs::write(PathBuf::from(rpc_tmp_filepath.clone()), file_content); + let result = fs::write(&path, file_content); if let Err(e) = result { - return format!("file can't be written: {}", e); + return format!("file {} can't be written: {}", path.to_string_lossy(), e); } String::from("Ok") @@ -45,7 +45,7 @@ pub fn put(name: String, file_content: Vec) -> String { pub fn get(file_name: String) -> Vec { log::info!("get called with file name: {}", file_name); - let tmp_filepath = format!("{}{}", SITES_DIR, file_name); + let path = Path::new(SITES_DIR).join(file_name); - fs::read(tmp_filepath).unwrap_or_else(|_| b"error while reading file".to_vec()) + fs::read(&path).unwrap_or_else(|err| format!("error while reading file {}: {}", path.to_string_lossy(), err).into_bytes()) } diff --git a/url-downloader/repl_config.toml b/url-downloader/repl_config.toml new file mode 100644 index 0000000..6701450 --- /dev/null +++ b/url-downloader/repl_config.toml @@ -0,0 +1,21 @@ +modules_dir = "artifacts/" + +[[module]] +name = "local_storage" +logger_enabled = true + +[module.wasi] +preopened_files = ["."] +# this is where files will be stored +mapped_dirs = { "sites" = "." } + +[[module]] +name = "curl_adapter" +logger_enabled = true + +[module.mounted_binaries] +curl = "/usr/bin/curl" + +[[module]] +name = "facade" +logger_enabled = true