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
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use anyhow::Error;
use async_trait::async_trait;
use meio::{
    Actor, Address, Interaction, InteractionHandler, InteractionPerformer, LiteTask,
    ShutdownReceiver,
};
use std::convert::Infallible;
use std::net::SocketAddr;
use warp::{
    filters::BoxedFilter,
    http::StatusCode,
    path::Tail,
    ws::{WebSocket, Ws},
    Filter, Reply, Server,
};

pub trait ToReply {
    fn to_reply(self) -> Result<Box<dyn Reply>, Infallible>;
}

impl ToReply for Result<Box<dyn Reply>, Error> {
    fn to_reply(self) -> Result<Box<dyn Reply>, Infallible> {
        match self {
            Ok(reply) => Ok(reply),
            Err(err) => {
                log::warn!("Request failed with: {}", err);
                Ok(Box::new(StatusCode::BAD_REQUEST))
            }
        }
    }
}

#[derive(Debug)]
pub struct IndexRequest;

impl Interaction for IndexRequest {
    type Output = Box<dyn Reply>;
}

impl IndexRequest {
    pub fn filter<A>(address: Address<A>) -> BoxedFilter<(impl Reply,)>
    where
        A: Actor + InteractionHandler<IndexRequest>,
    {
        warp::path::end()
            .and_then(move || index_handler(address.clone()))
            .boxed()
    }
}

async fn index_handler<A>(mut address: Address<A>) -> Result<Box<dyn Reply>, Infallible>
where
    A: Actor + InteractionHandler<IndexRequest>,
{
    let msg = IndexRequest;
    address.interact(msg).await.to_reply()
}

#[derive(Debug)]
pub struct TailRequest {
    pub tail: Tail,
}

impl Interaction for TailRequest {
    type Output = Box<dyn Reply>;
}

impl TailRequest {
    pub fn filter<A>(address: Address<A>) -> BoxedFilter<(impl Reply,)>
    where
        A: Actor + InteractionHandler<TailRequest>,
    {
        warp::path::tail()
            .and_then(move |tail| tail_handler(tail, address.clone()))
            .boxed()
    }
}

async fn tail_handler<A>(tail: Tail, mut address: Address<A>) -> Result<Box<dyn Reply>, Infallible>
where
    A: Actor + InteractionHandler<TailRequest>,
{
    let msg = TailRequest { tail };
    address.interact(msg).await.to_reply()
}

/// This struct received by an `Actor` when a client connected.
#[derive(Debug)]
pub struct WsRequest {
    pub addr: SocketAddr,
    pub websocket: WebSocket,
}

impl Interaction for WsRequest {
    type Output = ();
}

impl WsRequest {
    pub fn filter<A>(
        section: &'static str,
        method: &'static str,
        actor: Address<A>,
    ) -> BoxedFilter<(impl Reply,)>
    where
        A: Actor + InteractionHandler<WsRequest>,
    {
        warp::path(section)
            .and(warp::path(method))
            .and(warp::ws())
            .and(warp::addr::remote())
            .map(move |ws, addr| ws_handler(ws, addr, actor.clone()))
            .boxed()
    }
}

fn ws_handler<A>(ws: Ws, addr: Option<SocketAddr>, actor: Address<A>) -> Box<dyn Reply>
where
    A: Actor + InteractionHandler<WsRequest>,
{
    match addr {
        Some(address) => {
            let upgrade =
                ws.on_upgrade(move |websocket| ws_entrypoint(address, websocket, actor.clone()));
            Box::new(upgrade)
        }
        None => {
            log::error!("Can't get peer address: {:?}", ws);
            Box::new("Peer address not provided")
        }
    }
}

async fn ws_entrypoint<A>(addr: SocketAddr, websocket: WebSocket, mut actor: Address<A>)
where
    A: Actor + InteractionHandler<WsRequest>,
{
    let msg = WsRequest { addr, websocket };
    if let Err(err) = actor.interact(msg).await {
        log::error!("Can't send notification about ws connection: {}", err);
    }
}

// TODO: Move this struct to another module
pub struct WebServer<F> {
    addr: SocketAddr,
    server: Server<F>,
}

impl<F> WebServer<F>
where
    F: Filter + Clone + Send + Sync + 'static,
    F::Extract: Reply,
{
    pub fn new(addr: SocketAddr, routes: F) -> Self {
        let server = warp::serve(routes);
        Self { addr, server }
    }
}

#[async_trait]
impl<F> LiteTask for WebServer<F>
where
    F: Filter + Clone + Send + Sync + 'static,
    F::Extract: Reply,
{
    fn name(&self) -> String {
        format!("WebServer({})", self.addr)
    }

    async fn routine(self, signal: ShutdownReceiver) -> Result<(), Error> {
        self.server
            .bind_with_graceful_shutdown(self.addr, signal.just_done())
            .1
            .await;
        Ok(())
    }
}