Struct service_io::engine::Engine
source · [−]pub struct Engine { /* private fields */ }
Expand description
Main entity of service-io
.
It defines the following schema that runs asynchronously: Input -> n Services -> Output
A message received by the InputConnector
will be sent to a specific Service
based on the
Message::service_name
. The Service
will process the message and optionally can sent any
number of output messages that will be delivered by the OutputConnector
.
Example
use service_io::connectors::{ImapClient, SmtpClient, imap};
use service_io::engine::Engine;
use service_io::services::{Echo, Alarm};
use service_io::secret_manager::PasswordManager;
#[tokio::main]
async fn main() {
Engine::default()
.input(
ImapClient::default()
.domain("imap.domain.com")
.email("service@domain.com")
.secret_manager(PasswordManager::new("1234")),
)
.output(
SmtpClient::default()
.domain("smtp.domain.com")
.email("service@domain.com")
.secret_manager(PasswordManager::new("1234")),
)
.add_service("s-echo", Echo)
.add_service("s-alarm", Alarm)
.run()
.await;
}
Implementations
sourceimpl Engine
impl Engine
sourcepub fn input(self, input: impl InputConnector + Send + 'static) -> Engine
pub fn input(self, input: impl InputConnector + Send + 'static) -> Engine
Set an input connector for this engine that will be run after calling Engine::run()
.
Default connectors can be found in connectors
.
This call is mandatory in order to run the engine.
sourcepub fn output(self, output: impl OutputConnector + Send + 'static) -> Engine
pub fn output(self, output: impl OutputConnector + Send + 'static) -> Engine
Set an output connector for this engine that will be run after calling Engine::run()
.
Default connectors can be found in connectors
.
This call is mandatory in order to run the engine.
sourcepub fn map_input(
self,
mapping: impl Fn(Message) -> Message + Send + 'static
) -> Engine
pub fn map_input(
self,
mapping: impl Fn(Message) -> Message + Send + 'static
) -> Engine
Maps the message processed by the input connector into other message before checking the destination service the message is for.
Example
use service_io::connectors::{ImapClient, SmtpClient, imap};
use service_io::engine::Engine;
use service_io::services::Echo;
use service_io::message::util;
use service_io::secret_manager::PasswordManager;
#[tokio::main]
async fn main() {
Engine::default()
.input(
ImapClient::default()
.domain("imap.domain.com")
.email("service@domain.com")
.secret_manager(PasswordManager::new("1234")),
)
.output(
SmtpClient::default()
.domain("smtp.domain.com")
.email("service@domain.com")
.secret_manager(PasswordManager::new("1234")),
)
// Now, if the user writes "S-echo", it will found the service "s-echo"
.map_input(util::service_name_first_char_to_lowercase)
.add_service("s-echo", Echo)
.run()
.await;
}
sourcepub fn filter_input(
self,
filtering: impl Fn(&Message) -> bool + Send + 'static
) -> Engine
pub fn filter_input(
self,
filtering: impl Fn(&Message) -> bool + Send + 'static
) -> Engine
Allow or disallow passing the message to the service based of the message itself.
This filter method is applied just after the mapping method set by Engine::map_input
.
Example
use service_io::connectors::{ImapClient, SmtpClient, imap};
use service_io::engine::Engine;
use service_io::services::Echo;
use service_io::secret_manager::PasswordManager;
#[tokio::main]
async fn main() {
Engine::default()
.input(
ImapClient::default()
.domain("imap.domain.com")
.email("service@domain.com")
.secret_manager(PasswordManager::new("1234")),
)
.output(
SmtpClient::default()
.domain("smtp.domain.com")
.email("service@domain.com")
.secret_manager(PasswordManager::new("1234")),
)
// Now, only input messages from gmail are allowed
.filter_input(|message| message.user.ends_with("gmail.com"))
.add_service("s-echo", Echo)
.run()
.await;
}
sourcepub fn add_service(
self,
name: impl Into<String>,
service: impl Service + Send + 'static
) -> Engine
pub fn add_service(
self,
name: impl Into<String>,
service: impl Service + Send + 'static
) -> Engine
Add a service to the engine registered with a name
. If the Message::service_name
value
matches with this name
, the message will be redirected to the service.
Note that the service will not run until you call Engine::run()
Default services can be found in services
sourcepub fn add_service_for<S: Into<String>>(
self,
name: impl Into<String>,
service: impl Service + Send + 'static,
whitelist: impl IntoIterator<Item = S>
) -> Engine
pub fn add_service_for<S: Into<String>>(
self,
name: impl Into<String>,
service: impl Service + Send + 'static,
whitelist: impl IntoIterator<Item = S>
) -> Engine
Similar to Engine::add_service()
but service only allow receive message for a whitelist of users.
If the Message::user
of the incoming message not belong to that list, the message is
discarded.
Example
use service_io::connectors::{ImapClient, SmtpClient, imap};
use service_io::engine::Engine;
use service_io::services::Process;
use service_io::secret_manager::PasswordManager;
#[tokio::main]
async fn main() {
Engine::default()
.input(
ImapClient::default()
.domain("imap.domain.com")
.email("service@domain.com")
.secret_manager(PasswordManager::new("1234")),
)
.output(
SmtpClient::default()
.domain("smtp.domain.com")
.email("service@domain.com")
.secret_manager(PasswordManager::new("1234")),
)
// We only want messages comming from the admin user
// to go to s-process service to avoid attacks.
.add_service_for("s-process", Process, ["admin@domain.com"])
.run()
.await;
}
Trait Implementations
Auto Trait Implementations
impl !RefUnwindSafe for Engine
impl Send for Engine
impl !Sync for Engine
impl Unpin for Engine
impl !UnwindSafe for Engine
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Instruments this type with the provided Span
, returning an
Instrumented
wrapper. Read more
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<T> IntoOption<T> for T
impl<T> IntoOption<T> for T
impl<V, T> VZip<V> for Twhere
V: MultiLane<T>,
impl<V, T> VZip<V> for Twhere
V: MultiLane<T>,
fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more