mirror of
https://github.com/fluencelabs/marine.git
synced 2025-03-15 14:00:50 +00:00
Use last module as a facade in AppService (#34)
This commit is contained in:
parent
22ce705684
commit
0bf13fcdc6
@ -77,9 +77,7 @@ impl std::fmt::Display for FCEError {
|
|||||||
FCEError::NoSuchFunction(msg) => {
|
FCEError::NoSuchFunction(msg) => {
|
||||||
write!(f, "FCE doesn't have a function with such a name: {}", msg)
|
write!(f, "FCE doesn't have a function with such a name: {}", msg)
|
||||||
}
|
}
|
||||||
FCEError::NoSuchModule(module_name) => {
|
FCEError::NoSuchModule(err_msg) => write!(f, "{}", err_msg),
|
||||||
write!(f, "FCE doesn't have a module with name {}", module_name)
|
|
||||||
}
|
|
||||||
FCEError::HostImportError(host_import_error) => write!(f, "{}", host_import_error),
|
FCEError::HostImportError(host_import_error) => write!(f, "{}", host_import_error),
|
||||||
FCEError::WITParseError(err) => write!(f, "{}", err),
|
FCEError::WITParseError(err) => write!(f, "{}", err),
|
||||||
FCEError::IncorrectWIT(err_msg) => write!(f, "{}", err_msg),
|
FCEError::IncorrectWIT(err_msg) => write!(f, "{}", err_msg),
|
||||||
|
@ -33,6 +33,7 @@ const SERVICE_TMP_DIR_NAME: &str = "tmp";
|
|||||||
|
|
||||||
pub struct AppService {
|
pub struct AppService {
|
||||||
faas: FluenceFaaS,
|
faas: FluenceFaaS,
|
||||||
|
facade_module_name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AppService {
|
impl AppService {
|
||||||
@ -44,25 +45,44 @@ impl AppService {
|
|||||||
AppServiceError: From<C::Error>,
|
AppServiceError: From<C::Error>,
|
||||||
{
|
{
|
||||||
let mut config: AppServiceConfig = config.try_into()?;
|
let mut config: AppServiceConfig = config.try_into()?;
|
||||||
|
let facade_module_name = config
|
||||||
|
.faas_config
|
||||||
|
.modules_config
|
||||||
|
.last()
|
||||||
|
.ok_or_else(|| {
|
||||||
|
AppServiceError::ConfigParseError(String::from(
|
||||||
|
"config should contain at least one module",
|
||||||
|
))
|
||||||
|
})?
|
||||||
|
.0
|
||||||
|
.clone();
|
||||||
|
|
||||||
let service_id = service_id.into();
|
let service_id = service_id.into();
|
||||||
Self::set_env_and_dirs(&mut config, service_id, envs)?;
|
Self::set_env_and_dirs(&mut config, service_id, envs)?;
|
||||||
|
|
||||||
let faas = FluenceFaaS::with_raw_config(config.faas_config)?;
|
let faas = FluenceFaaS::with_raw_config(config.faas_config)?;
|
||||||
|
|
||||||
Ok(Self { faas })
|
Ok(Self {
|
||||||
|
faas,
|
||||||
|
facade_module_name,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Call a specified function of loaded module by its name.
|
/// Call a specified function of loaded module by its name.
|
||||||
// TODO: replace serde_json::Value with Vec<u8>?
|
// TODO: replace serde_json::Value with Vec<u8>?
|
||||||
pub fn call<MN: AsRef<str>, FN: AsRef<str>>(
|
pub fn call<S: AsRef<str>>(
|
||||||
&mut self,
|
&mut self,
|
||||||
module_name: MN,
|
func_name: S,
|
||||||
func_name: FN,
|
|
||||||
arguments: serde_json::Value,
|
arguments: serde_json::Value,
|
||||||
call_parameters: crate::CallParameters,
|
call_parameters: crate::CallParameters,
|
||||||
) -> Result<Vec<IValue>> {
|
) -> Result<Vec<IValue>> {
|
||||||
self.faas
|
self.faas
|
||||||
.call_with_json(module_name, func_name, arguments, call_parameters)
|
.call_with_json(
|
||||||
|
&self.facade_module_name,
|
||||||
|
func_name,
|
||||||
|
arguments,
|
||||||
|
call_parameters,
|
||||||
|
)
|
||||||
.map_err(Into::into)
|
.map_err(Into::into)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -131,6 +151,18 @@ impl AppService {
|
|||||||
// This API is intended for testing purposes (mostly in FCE REPL)
|
// This API is intended for testing purposes (mostly in FCE REPL)
|
||||||
#[cfg(feature = "raw-module-api")]
|
#[cfg(feature = "raw-module-api")]
|
||||||
impl AppService {
|
impl AppService {
|
||||||
|
pub fn call_with_module_name<MN: AsRef<str>, FN: AsRef<str>>(
|
||||||
|
&mut self,
|
||||||
|
module_name: MN,
|
||||||
|
func_name: FN,
|
||||||
|
arguments: serde_json::Value,
|
||||||
|
call_parameters: crate::CallParameters,
|
||||||
|
) -> Result<Vec<IValue>> {
|
||||||
|
self.faas
|
||||||
|
.call_with_json(module_name, func_name, arguments, call_parameters)
|
||||||
|
.map_err(Into::into)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn load_module<S, C>(&mut self, name: S, wasm_bytes: &[u8], config: Option<C>) -> Result<()>
|
pub fn load_module<S, C>(&mut self, name: S, wasm_bytes: &[u8], config: Option<C>) -> Result<()>
|
||||||
where
|
where
|
||||||
S: Into<String>,
|
S: Into<String>,
|
||||||
|
@ -115,17 +115,18 @@ impl REPL {
|
|||||||
|
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
// TODO: add support of call parameters
|
// TODO: add support of call parameters
|
||||||
let result =
|
let result = match self.app_service.call_with_module_name(
|
||||||
match self
|
module_name,
|
||||||
.app_service
|
func_name,
|
||||||
.call(module_name, func_name, module_arg, <_>::default())
|
module_arg,
|
||||||
{
|
<_>::default(),
|
||||||
Ok(result) => {
|
) {
|
||||||
let elapsed_time = start.elapsed();
|
Ok(result) => {
|
||||||
format!("result: {:?}\n elapsed time: {:?}", result, elapsed_time)
|
let elapsed_time = start.elapsed();
|
||||||
}
|
format!("result: {:?}\n elapsed time: {:?}", result, elapsed_time)
|
||||||
Err(e) => format!("execution failed with {:?}", e),
|
}
|
||||||
};
|
Err(e) => format!("execution failed with {:?}", e),
|
||||||
|
};
|
||||||
println!("{}", result);
|
println!("{}", result);
|
||||||
}
|
}
|
||||||
Some("envs") => {
|
Some("envs") => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user