Skip to main content

meio_connect/server/
link.rs

1//! The link to interact with a server instance.
2
3use super::{
4    route::{BoxedRoute, Route},
5    HttpServer, HttpServerLink,
6};
7use anyhow::Error;
8use async_trait::async_trait;
9use meio::prelude::{Action, ActionHandler, Context};
10
11impl HttpServerLink {
12    /// Adds a route to the `HttpServer`.
13    pub fn add_route<T>(&mut self, route: T) -> Result<(), Error>
14    where
15        T: Route,
16    {
17        let msg = AddRoute {
18            route: Box::new(route),
19        };
20        self.address.act(msg)
21    }
22}
23
24struct AddRoute {
25    pub route: BoxedRoute,
26}
27
28impl Action for AddRoute {}
29
30#[async_trait]
31impl ActionHandler<AddRoute> for HttpServer {
32    async fn handle(&mut self, msg: AddRoute, _ctx: &mut Context<Self>) -> Result<(), Error> {
33        self.routing_table.insert_route(msg.route).await;
34        Ok(())
35    }
36}