mirror of
https://github.com/fluencelabs/examples
synced 2024-12-04 19:20:17 +00:00
upload & download works
This commit is contained in:
parent
19d5fd4db6
commit
575fff38a3
@ -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
|
||||
|
@ -1 +1,12 @@
|
||||
{"name": "local_storage"}
|
||||
{
|
||||
"name": "local_storage",
|
||||
"preopenedFiles": [
|
||||
"/tmp"
|
||||
],
|
||||
"mappedDirs": {
|
||||
"sites": "/tmp"
|
||||
},
|
||||
"mountedBinaries": {
|
||||
"curl": "/usr/bin/curl"
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
10
url-downloader/download.air
Normal file
10
url-downloader/download.air
Normal file
@ -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%])
|
||||
)
|
@ -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%])
|
||||
)
|
@ -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)
|
||||
|
@ -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<u8>) -> String {
|
||||
log::info!("put called with file name {}", name);
|
||||
pub fn put(file_name: String, file_content: Vec<u8>) -> 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<u8>) -> String {
|
||||
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);
|
||||
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())
|
||||
}
|
||||
|
21
url-downloader/repl_config.toml
Normal file
21
url-downloader/repl_config.toml
Normal file
@ -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
|
Loading…
Reference in New Issue
Block a user