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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/// This module implements integration with `tide`.
use cfg_if::cfg_if;
use tide_websockets as tide_ws;

use super::Server;
use crate::transport::ws::WebSocketConn;

cfg_if! {
    if #[cfg(any(
        any(feature = "docs", doc),
        all(
            feature = "serde_bincode",
            not(feature = "serde_json"),
            not(feature = "serde_cbor"),
            not(feature = "serde_rmp"),
        ),
        all(
            feature = "serde_cbor",
            not(feature = "serde_json"),
            not(feature = "serde_bincode"),
            not(feature = "serde_rmp"),
        ),
        all(
            feature = "serde_json",
            not(feature = "serde_bincode"),
            not(feature = "serde_cbor"),
            not(feature = "serde_rmp"),
        ),
        all(
            feature = "serde_rmp",
            not(feature = "serde_cbor"),
            not(feature = "serde_json"),
            not(feature = "serde_bincode"),
        ),
    ))] {
        use crate::codec::DefaultCodec;
        use super::DEFAULT_RPC_PATH;

        /// The following impl block is controlled by feature flag. It is enabled
        /// if and only if **exactly one** of the the following feature flag is turned on
        /// - `serde_bincode`
        /// - `serde_json`
        /// - `serde_cbor`
        /// - `serde_rmp`
        impl Server {
            #[cfg(any(feature = "http_tide", feature = "docs"))]
            #[cfg_attr(feature = "docs", doc(cfg(feature = "http_tide")))]
            /// Creates a `tide::Endpoint` that handles http connections.
            /// A convienient function `handle_http` can be used to achieve the same thing
            /// with `tide` feature turned on
            ///
            /// The endpoint will be created with `DEFAULT_RPC_PATH` appended to the
            /// end of the nested `tide` endpoint.
            ///
            /// This is enabled
            /// if and only if **exactly one** of the the following feature flag is turned on
            /// - `serde_bincode`
            /// - `serde_json`
            /// - `serde_cbor`
            /// - `serde_rmp`
            ///
            /// # Example
            /// ```
            /// use toy_rpc::server::Server;
            /// use toy_rpc::macros::{export_impl, service};
            /// use async_std::sync::Arc;
            ///
            /// struct FooService { }
            ///
            /// #[export_impl]
            /// impl FooService {
            ///     // define some "exported" functions
            /// }
            ///
            /// #[async_std::main]
            /// async fn main() -> tide::Result<()> {
            ///     let addr = "127.0.0.1:8080";
            ///     let foo_service = Arc::new(FooService { });
            ///
            ///     let server = Server::builder()
            ///         .register(foo_service)
            ///         .build();
            ///
            ///     let mut app = tide::new();
            ///
            ///     // If a network path were to be supplied,
            ///     // the network path must end with a slash "/"
            ///     app.at("/rpc/").nest(server.into_endpoint());
            ///
            ///     // `handle_http` is a conenient function that calls `into_endpoint`
            ///     // with the `tide` feature turned on
            ///     //app.at("/rpc/").nest(server.handle_http());
            ///
            ///     app.listen(addr).await?;
            ///     Ok(())
            /// }
            /// ```
            ///
            pub fn into_endpoint(self) -> tide::Server<Server> {
                let mut app = tide::Server::with_state(self);
                // let mut app = tide::Server::new();
                app.at(DEFAULT_RPC_PATH)
                    // .connect(|_| async move { Ok("CONNECT request is received") })
                    .get(tide_ws::WebSocket::new(
                        |req: tide::Request<Server>, ws_stream| async move {
                            let ws_stream = WebSocketConn::new_without_sink(ws_stream);
                            let codec = DefaultCodec::with_tide_websocket(ws_stream);
                            let services = req.state().services.clone();

                            let fut = Self::serve_codec_loop(codec, services);

                            log::trace!("Client disconnected.");

                            fut.await?;
                            Ok(())
                        },
                    ));

                app
            }

            #[cfg(any(
                all(
                    feature = "http_tide",
                    not(feature = "http_actix_web"),
                    not(feature = "http_warp"),
                ),
                feature = "docs"
            ))]
            #[cfg_attr(
                feature = "docs",
                doc(cfg(all(
                    feature = "http_tide",
                    not(feature = "http_actix_web"),
                    not(feature = "http_warp"),
                )))
            )]
            /// A conevience function that calls the corresponding http handling
            /// function depending on the enabled feature flag
            ///
            /// | feature flag | function name  |
            /// | ------------ |---|
            /// | `http_tide`| [`into_endpoint`](#method.into_endpoint) |
            /// | `http_actix_web` | [`scope_config`](#method.scope_config) |
            /// | `http_warp` | [`into_boxed_filter`](#method.into_boxed_filter) |
            ///
            /// This is enabled
            /// if and only if **exactly one** of the the following feature flag is turned on
            /// - `serde_bincode`
            /// - `serde_json`
            /// - `serde_cbor`
            /// - `serde_rmp`
            ///
            /// # Example
            /// ```
            /// use toy_rpc::server::Server;
            /// use toy_rpc::macros::{export_impl, service};
            /// use async_std::sync::Arc;
            ///
            /// struct FooService { }
            ///
            /// #[export_impl]
            /// impl FooService {
            ///     // define some "exported" functions
            /// }
            ///
            /// #[async_std::main]
            /// async fn main() -> tide::Result<()> {
            ///     let addr = "127.0.0.1:8080";
            ///     let foo_service = Arc::new(FooService { });
            ///
            ///     let server = Server::builder()
            ///         .register(foo_service)
            ///         .build();
            ///
            ///     let mut app = tide::new();
            ///
            ///     // If a network path were to be supplied,
            ///     // the network path must end with a slash "/"
            ///     // `handle_http` is a conenience function that calls `into_endpoint`
            ///     // with the `tide` feature turned on
            ///     app.at("/rpc/").nest(server.handle_http());
            ///
            ///     app.listen(addr).await?;
            ///     Ok(())
            /// }
            /// ```
            pub fn handle_http(self) -> tide::Server<Server> {
                self.into_endpoint()
            }
        }
    }
}