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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//! Actor for binding application to the node.

mod app_route;
mod assets;

use anyhow::Error;
use assets::AssetsMode;
use async_trait::async_trait;
use meio::all::{Actor, Context, InterruptedBy, StartedBy};
use meio_connect::server::HttpServerLink;
use rate_melt::PackedAssets;
use reqwest::Url;

/// Options of assets.
pub struct AssetsOptions {
    /// Prefix to strip.
    pub prefix: &'static str,
    /// Environment variable to get assets from.
    pub env_var: Option<&'static str>,
    /// URL for fetching assets.
    pub url: Option<Url>,
    /// Embedded assets.
    pub embedded: Option<PackedAssets>,
}

/// Application binding to the node.
///
/// Adds and manages corresponding routes and assets.
pub struct AppBind {
    log_target: String,
    server: HttpServerLink,
    assets: AssetsMode,
    options: AssetsOptions,
}

impl AppBind {
    /// Creates a new application binding.
    pub fn new(server: HttpServerLink, options: AssetsOptions) -> Self {
        let log_target = format!("AppBind::{}", options.prefix);
        Self {
            log_target,
            server,
            assets: AssetsMode::Loading,
            options,
        }
    }
}

impl Actor for AppBind {
    type GroupBy = ();

    fn log_target(&self) -> &str {
        &self.log_target
    }
}

#[async_trait]
impl<T: Actor> StartedBy<T> for AppBind {
    async fn handle(&mut self, ctx: &mut Context<Self>) -> Result<(), Error> {
        self.app_bind_route(ctx).await?;
        self.init_assets(ctx).await?;
        Ok(())
    }
}

#[async_trait]
impl<T: Actor> InterruptedBy<T> for AppBind {
    async fn handle(&mut self, ctx: &mut Context<Self>) -> Result<(), Error> {
        log::info!(target: &self.log_target, "Terminating the app...");
        ctx.shutdown();
        Ok(())
    }
}