mirror of
https://github.com/fluencelabs/examples
synced 2024-12-04 19:20:17 +00:00
Merge branch 'main' of https://github.com/fluencelabs/examples
This commit is contained in:
commit
773fd2d42c
1
.gitignore
vendored
1
.gitignore
vendored
@ -10,3 +10,4 @@ node_modules
|
||||
/artifacts
|
||||
keypair.json
|
||||
*.wasm
|
||||
logo.svg
|
||||
|
@ -1,6 +1,6 @@
|
||||
# Download & return URL via curl
|
||||
|
||||
An example repo to kick-off building services on Fluence.
|
||||
An example to kick-off building services on Fluence.
|
||||
|
||||
What it does:
|
||||
- exploits `mounted_binaries` to call `/usr/bin/curl` on the host OS
|
||||
@ -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
|
||||
|
14
url-downloader/README.md
Normal file
14
url-downloader/README.md
Normal file
@ -0,0 +1,14 @@
|
||||
# Download file to disk
|
||||
|
||||
Example to show how to work with disk + link several .wasm modules into a service.
|
||||
|
||||
# Build & deploy it
|
||||
```shell
|
||||
./deploy.sh
|
||||
```
|
||||
|
||||
# Call it
|
||||
```shell
|
||||
fldist run_air -p download.air -d '{"service": "08eba00d-ff40-4e38-bbe6-ee3646498400"}'
|
||||
```
|
||||
|
@ -1 +1,12 @@
|
||||
{"name": "local_storage"}
|
||||
{
|
||||
"name": "local_storage",
|
||||
"preopenedFiles": [
|
||||
"/tmp"
|
||||
],
|
||||
"mappedDirs": {
|
||||
"sites": "/tmp"
|
||||
},
|
||||
"mountedBinaries": {
|
||||
"curl": "/usr/bin/curl"
|
||||
}
|
||||
}
|
||||
|
13
url-downloader/compose.air
Normal file
13
url-downloader/compose.air
Normal file
@ -0,0 +1,13 @@
|
||||
(xor
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(call relay (curl "download") ["https://fluence.network/img/svg/logo_new.svg"] contents)
|
||||
(call relay (storage "put") ["logo.svg" contents.$.stdout!] ret_code)
|
||||
)
|
||||
(call relay (storage "get") ["logo.svg"] bytes)
|
||||
)
|
||||
(call %init_peer_id% (returnService "run") [ret_code bytes])
|
||||
)
|
||||
(call %init_peer_id% (returnService "run") [%last_error%])
|
||||
)
|
@ -1,7 +1,7 @@
|
||||
#!/bin/sh -euo pipefail
|
||||
|
||||
# build wasms
|
||||
sh build.sh
|
||||
./build.sh
|
||||
|
||||
(
|
||||
cd artifacts
|
||||
|
15
url-downloader/deploy_separate.sh
Executable file
15
url-downloader/deploy_separate.sh
Executable file
@ -0,0 +1,15 @@
|
||||
#!/bin/sh -euo pipefail
|
||||
|
||||
./build.sh
|
||||
|
||||
echo "Deploying storage"
|
||||
(
|
||||
cd artifacts
|
||||
fldist new_service --name "local_storage" --modules local_storage.wasm:local_storage.json
|
||||
)
|
||||
|
||||
echo "\n\nDeploying curl"
|
||||
(
|
||||
cd artifacts
|
||||
fldist new_service --name "curl_adapter" --modules curl_adapter.wasm:curl_adapter.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