Develop a multi-modules service
Each service in the Fluence network consists of at least one module, and there is a special configuration file that manages the service creation process. In particular, it controls the order in which the modules are instantiated, the permissions that each module will have, the maximum memory limit, and some other parameters. This section explains how to develop a simple multi-module service based on the url-downloader example.
Creating a url-downloader service
To demonstrate the capabilities of Wasm multi-modules in Marine, let us build a service that is able to download a site by its url and store it locally with a specified name. For demonstration purposes, this service will contain three modules: curl for downloading files, local_storage for saving files to the file system, and facade as a facade module that combines two previous ones. And let us give the whole service the name url-downloader.
Preparation
First, create an empty directory with the url-downloader name (further, we will call it the service root directory) for our service and initialize three new modules:
sh
sh
Then add marine-rs-sdk = "0.10.2" to the dependencies section of each Cargo.toml file.
Curl module
The main purpose of this module is to simply provide a wrapper for the curl CLI tool. This is done by importing the curl function from the host (in the same way as described in mounted binaries section and exporting the function called get.
Open the curl-adapter/src/main.rs file in an editor and paste the code of the curl module there:
rust
rust
Note that errors are not handled in these examples for simplicity.
Local-storage module
This module is intended for loading and storing files in the file system. It provides two exported functions: get, which returns the content of a file by its name in the sites directory, and put, which saves a file in the same directory and gives it the specified name.
Open the local-storage/src/main.rs file in an editor and paste the code of the local-storage module there:
rust
rust
Facade module
The facade module combines the logic of the previous modules in one exported function: get_n_save. This function downloads the site with the specified name using the get function from the curl_adapter module and then saves it into a file on the file system using the put function from the local-storage module. To import functions from another module, their signatures must be declared in an extern block wrapped with the #[marine] procedure macro.
Open the facade/src/main.rs file in an editor and paste the code of the facade module there:
rust
rust
Building all modules
Now the modules are ready to be built, so let us do it:
sh
sh
Url-downloader service config
To run a module with the Marine REPL, we should create a config file where some necessary module loading details will be described. For more details about the structure of this file please refer to this section.
The configuration of the url-downloader service should look as follows:
toml
toml
The important things here are:
- local_storageis allowed to use- sitesdirectory; it'll be used for storing downloaded files
- curl_adapteris allowed to use the- curlbinary in the OS
- facademodule is actually the facade module for the service because it's the last module in the list
Save this config as the Config.toml file in the service root directory.
Run url-downloader with REPL
Let's start with creating a directory called artifacts in the root of the url-downloader service and copying the three resulting .wasm file into it:
sh
sh
Alternatively, you can find the ready-to-run url-downloader service here.
Before running it, create a directory named sites, which is required by the local-storage module to store the downloaded data.
Then run the REPL with the Config.toml file and examine the url-downloader service interface:
sh
sh
sh
sh
There are three interface modules here. Note that the service interface is only represented by the facade module. However, the REPL allows all loaded modules to be called for debugging purposes.
Let us download the google.com page:
sh
sh
The downloaded page can be found at sites/google. As already mentioned, each module within a service can be called directly, for example, local_storage can be treated as a separate module:
sh
sh