1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use super::{Group, Way, WayLink};
use anyhow::Error;
use async_trait::async_trait;
use meio::all::{Action, ActionHandler, Context, Eliminated, IdOf};
use rate_melt::PackedAssets;
use rill_app::app_bind::{AppBind, AssetsOptions};

struct InstallApp {
    assets: PackedAssets,
}

impl Action for InstallApp {}

impl WayLink {
    /// Installs the assets as the app.
    ///
    /// When the app is added the actors automatically creates
    /// all necessary routes for the app.
    pub fn install_app(&mut self, assets: PackedAssets) -> Result<(), Error> {
        let msg = InstallApp { assets };
        self.address.act(msg)
    }
}

#[async_trait]
impl ActionHandler<InstallApp> for Way {
    async fn handle(&mut self, msg: InstallApp, ctx: &mut Context<Self>) -> Result<(), Error> {
        let env_var = cfg!(debug_assertions).then(|| "RR_APP_PATH");
        let options = AssetsOptions {
            prefix: "/app/",
            env_var,
            url: None,
            embedded: Some(msg.assets),
        };
        let server = self
            .server
            .clone()
            .ok_or_else(|| Error::msg("Server not started"))?;
        let app_bind = AppBind::new(server, options);
        ctx.spawn_actor(app_bind, Group::Assets);
        Ok(())
    }
}

#[async_trait]
impl Eliminated<AppBind> for Way {
    async fn handle(&mut self, _id: IdOf<AppBind>, _ctx: &mut Context<Self>) -> Result<(), Error> {
        Ok(())
    }
}