use crate::endpoint;
use crate::endpoint::BoxedService;
use std::future::Future;
pub trait Service {
type Future: Future<Output = Result<(), endpoint::Error>> + Send + 'static;
fn handle(&self, req: endpoint::ContextInternal) -> Self::Future;
}
pub trait Discoverable {
fn discover() -> crate::discovery::Service;
}
pub struct ServiceDefinition {
pub(crate) discovery: crate::discovery::Service,
pub(crate) dispatcher: BoxedService,
}
impl ServiceDefinition {
pub fn options(mut self, options: crate::endpoint::ServiceOptions) -> Self {
self.discovery.apply_options(options);
self
}
}
pub trait IntoServiceDefinition {
fn into_service_definition(self) -> ServiceDefinition;
}
impl IntoServiceDefinition for ServiceDefinition {
fn into_service_definition(self) -> ServiceDefinition {
self
}
}
#[doc(hidden)]
pub mod macro_support {
use super::{Service, ServiceDefinition};
use crate::endpoint::{self, BoxedService};
use futures::future::BoxFuture;
pub type ServiceBoxFuture = BoxFuture<'static, Result<(), endpoint::Error>>;
pub fn service_definition<S>(
dispatcher: S,
discovery: crate::discovery::Service,
) -> ServiceDefinition
where
S: Service<Future = ServiceBoxFuture> + Send + Sync + 'static,
{
ServiceDefinition {
discovery,
dispatcher: BoxedService::new(dispatcher),
}
}
}