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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
mod discovery;
pub use discovery::{BoundServer as BoundDiscoveryServer, Server as DiscoveryServer};

mod transaction;
pub(crate) use transaction::*;

mod case_insensitive_str;

mod params;
pub(crate) use params::ActionParams;

mod response;
pub(crate) use response::Response;

mod error;
pub(crate) use error::{Error, Result};

#[cfg(feature = "camera")]
use crate::api::Camera;
use crate::api::{CargoServerInfo, DevicePath, DeviceType, ServerInfo};
use crate::discovery::DEFAULT_DISCOVERY_PORT;
use crate::response::ValueResponse;
use crate::Devices;
use axum::body::HttpBody;
use axum::extract::{FromRequest, Path};
use axum::http::Request;
use axum::response::IntoResponse;
use axum::routing::MethodFilter;
use axum::{BoxError, Router};
use net_literals::addr;
use sailfish::TemplateOnce;
use std::collections::BTreeMap;
use std::future::Future;
use std::net::SocketAddr;
use std::pin::Pin;
use std::sync::Arc;
use tracing::Instrument;

/// The Alpaca server.
#[derive(Debug)]
pub struct Server {
    /// Registered devices.
    pub devices: Devices,
    /// General server information.
    pub info: ServerInfo,
    /// Address for the server to listen on.
    pub listen_addr: SocketAddr,
    /// Port for the discovery server to listen on.
    pub discovery_port: u16,
}

impl Default for Server {
    fn default() -> Self {
        Self {
            devices: Devices::default(),
            info: CargoServerInfo!(),
            listen_addr: addr!("[::]:0"),
            discovery_port: DEFAULT_DISCOVERY_PORT,
        }
    }
}

struct ServerHandler {
    path: String,
    params: ActionParams,
}

#[async_trait::async_trait]
impl<S, B> FromRequest<S, B> for ServerHandler
where
    B: HttpBody + Send + Sync + 'static,
    B::Data: Send,
    B::Error: Into<BoxError>,
    S: Send + Sync,
{
    type Rejection = axum::response::Response;

    async fn from_request(
        req: Request<B>,
        state: &S,
    ) -> std::result::Result<Self, Self::Rejection> {
        let path = req.uri().path().to_owned();
        let params = ActionParams::from_request(req, state).await?;
        Ok(Self { path, params })
    }
}

impl ServerHandler {
    async fn exec<Resp: Response, RespFut: Future<Output = Resp> + Send>(
        mut self,
        make_response: impl FnOnce(ActionParams) -> RespFut + Send,
    ) -> axum::response::Response {
        let request_transaction = match RequestTransaction::extract(&mut self.params) {
            Ok(transaction) => transaction,
            Err(err) => {
                return (axum::http::StatusCode::BAD_REQUEST, format!("{err:#}")).into_response();
            }
        };
        let response_transaction =
            ResponseTransaction::new(request_transaction.client_transaction_id);

        let span = tracing::error_span!(
            "handle_alpaca_request",
            path = self.path,
            client_id = request_transaction.client_id,
            client_transaction_id = request_transaction.client_transaction_id,
            server_transaction_id = response_transaction.server_transaction_id,
        );

        async move {
            tracing::debug!(params = ?self.params, "Received request");

            make_response(self.params)
                .await
                .into_axum(response_transaction)
        }
        .instrument(span)
        .await
    }
}

/// Alpaca servers bound to their respective ports and ready to listen.
#[derive(custom_debug::Debug)]
pub struct BoundServer {
    // Axum types are a bit complicated, so just Box it for now.
    #[debug(skip)]
    axum: Pin<Box<dyn Future<Output = eyre::Result<std::convert::Infallible>> + Send>>,
    axum_listen_addr: SocketAddr,
    discovery: BoundDiscoveryServer,
}

impl BoundServer {
    /// Returns the address the main Alpaca server is listening on.
    #[allow(clippy::missing_const_for_fn)] // we don't want to guarantee this will be always const
    pub fn listen_addr(&self) -> SocketAddr {
        self.axum_listen_addr
    }

    /// Returns the address the discovery server is listening on.
    pub fn discovery_listen_addr(&self) -> SocketAddr {
        self.discovery.listen_addr()
    }

    /// Starts the Alpaca and discovery servers.
    ///
    /// Note: this function starts an infinite async loop and it's your responsibility to spawn it off
    /// via [`tokio::spawn`] if necessary.
    pub async fn start(self) -> eyre::Result<std::convert::Infallible> {
        match tokio::select! {
            axum = self.axum => axum?,
            discovery = self.discovery.start() => discovery,
        } {}
    }
}

impl Server {
    /// Binds the Alpaca and discovery servers to local ports.
    pub async fn bind(self) -> eyre::Result<BoundServer> {
        let addr = self.listen_addr;

        tracing::debug!(%addr, "Binding Alpaca server");

        // Like in discovery, use dual stack (IPv4+IPv6) consistently on all platforms.
        //
        // This is usually what user wants when setting IPv6 address like `[::]`
        // and this is what happens by default on popular Linux distros but not on Windows.
        //
        // For that, we can't use the standard `TcpListener::bind` and need to build our own socket.
        let socket = socket2::Socket::new(
            socket2::Domain::for_address(addr),
            socket2::Type::STREAM,
            Some(socket2::Protocol::TCP),
        )?;

        if addr.is_ipv6() {
            socket.set_only_v6(false)?;
        }

        socket.bind(&addr.into())?;
        socket.listen(128)?;

        let server = axum::Server::from_tcp(socket.into())?.serve(
            self.into_router()
                // .layer(TraceLayer::new_for_http())
                .into_make_service(),
        );

        // The address can differ e.g. when using port 0 (auto-assigned).
        let bound_addr = server.local_addr();

        tracing::info!(%bound_addr, "Bound Alpaca server");

        // Bind discovery server only once the Alpaca server is bound successfully.
        // We need to know the bound address & the port to advertise.
        let discovery_server = DiscoveryServer::for_alpaca_server_at(bound_addr)
            .bind()
            .await?;

        tracing::debug!("Bound Alpaca discovery server");

        Ok(BoundServer {
            axum: Box::pin(
                async move {
                    server.await?;
                    unreachable!("Alpaca server should never stop without an error")
                }
                .instrument(tracing::error_span!("alpaca_server_loop")),
            ),
            axum_listen_addr: bound_addr,
            discovery: discovery_server,
        })
    }

    /// Binds the Alpaca and discovery servers to local ports and starts them.
    ///
    /// This is a convenience method that is equivalent to calling [`Self::bind`] and [`BoundServer::start`].
    pub async fn start(self) -> eyre::Result<std::convert::Infallible> {
        self.bind().await?.start().await
    }

    #[allow(clippy::too_many_lines)]
    fn into_router(self) -> Router {
        let devices = Arc::new(self.devices);
        let server_info = Arc::new(self.info);

        Router::new()
            .route(
                "/management/apiversions",
                axum::routing::get(|server_handler: ServerHandler| {
                    server_handler.exec(|_params| async move { ValueResponse::from([1_u32]) })
                }),
            )
            .route("/management/v1/configureddevices", {
                let this = Arc::clone(&devices);

                axum::routing::get(|server_handler: ServerHandler| {
                    server_handler.exec(|_params| async move {
                        let devices = this
                            .iter_all()
                            .map(|(device, number)| device.to_configured_device(number))
                            .collect::<Vec<_>>();
                        ValueResponse::from(devices)
                    })
                })
            })
            .route("/management/v1/description", {
                let server_info = Arc::clone(&server_info);

                axum::routing::get(move |server_handler: ServerHandler| {
                    server_handler.exec(|_params| async move {
                        ValueResponse::from(Arc::clone(&server_info))
                    })
                })
            })
            .route("/setup", {
                let this = Arc::clone(&devices);
                let server_info = Arc::clone(&server_info);

                axum::routing::get(|| async move {
                    #[derive(TemplateOnce)]
                    #[template(path = "setup_template.html")]
                    struct TemplateContext {
                        server_info: Arc<ServerInfo>,
                        grouped_devices: BTreeMap<DeviceType, Vec<(usize, String)>>,
                    }

                    let mut ctx = TemplateContext {
                        server_info: Arc::clone(&server_info),
                        grouped_devices: BTreeMap::new(),
                    };

                    for (device, number) in this.iter_all() {
                        let device = device.to_configured_device(number);

                        ctx.grouped_devices
                            .entry(device.ty)
                            .or_insert_with(Vec::new)
                            .push((number, device.name));
                    }

                    match ctx.render_once() {
                        Ok(html) => Ok(axum::response::Html(html)),
                        Err(err) => {
                            tracing::error!(%err, "Failed to render setup page");
                            Err((
                                axum::http::StatusCode::INTERNAL_SERVER_ERROR,
                                err.to_string(),
                            ))
                        }
                    }
                })
            })
            .route(
                "/api/v1/:device_type/:device_number/:action",
                axum::routing::on(
                    MethodFilter::GET | MethodFilter::PUT,
                    move |Path((DevicePath(device_type), device_number, action)): Path<(
                        DevicePath,
                        usize,
                        String,
                    )>,
                          #[cfg(feature = "camera")] headers: axum::http::HeaderMap,
                          server_handler: ServerHandler| async move {
                        if action == "setup" {
                            #[derive(TemplateOnce)]
                            #[template(path = "device_setup_template.html")]
                            struct TemplateContext {
                                // TODO: figure out a good API to implement device configuration only on the server-side.
                            }

                            let ctx = TemplateContext {};

                            return match ctx.render_once() {
                                Ok(html) => (
                                    axum::http::StatusCode::NOT_IMPLEMENTED,
                                    axum::response::Html(html),
                                )
                                    .into_response(),
                                Err(err) => {
                                    tracing::error!(%err, "Failed to render device setup page");
                                    (
                                        axum::http::StatusCode::INTERNAL_SERVER_ERROR,
                                        err.to_string(),
                                    )
                                        .into_response()
                                }
                            };
                        }

                        #[cfg(feature = "camera")]
                        let mut action = action;

                        #[cfg(feature = "camera")]
                        if device_type == DeviceType::Camera {
                            // imagearrayvariant is soft-deprecated; we should accept it but
                            // forward to the imagearray handler instead.
                            if action == "imagearrayvariant" {
                                action.truncate("imagearray".len());
                            }

                            if matches!(server_handler.params, ActionParams::Get { .. })
                                && action == "imagearray"
                                && crate::api::ImageArray::is_accepted(&headers)
                            {
                                return server_handler
                                    .exec(|_params| async move {
                                        Ok::<_, Error>(crate::api::ImageBytesResponse(
                                            devices
                                                .get_for_server::<dyn Camera>(device_number)?
                                                .image_array()
                                                .await?,
                                        ))
                                    })
                                    .await;
                            }
                        }

                        server_handler
                            .exec(|params| {
                                devices.handle_action(device_type, device_number, &action, params)
                            })
                            .await
                    },
                ),
            )
    }
}