comprehensive_http 0.5.1

A harness for creating consistently-shaped servers will less boilerplate
Documentation
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
//! [`comprehensive`] [`Resource`] types for HTTP serving
//!
//! This module provides:
//! * A trait [`HttpServingInstance`] for resources that answer HTTP requests.
//! * [`HttpServer`], a generic [`Resource`] for serving such resources over
//!   HTTP and HTTPS.
//!
//! There can be several [`HttpServer`] resources in the same [`comprehensive::Assembly`],
//! each parameterised with a different [`HttpServingInstance`]. This is
//! expected to be used to have an internal server for metrics and
//! diagnostics and a different one for public serving. See
//! [`comprehensive_http::diag::HttpServer`] for the packaged instance of the
//! former.
//!
//! # Usage
//!
//! ```
//! use axum::Router;
//! use axum::response::IntoResponse;
//! use comprehensive::{NoArgs, NoDependencies, ResourceDependencies};
//! use comprehensive::v1::{AssemblyRuntime, Resource, resource};
//! use comprehensive_http::{HttpServer, HttpServingInstance};
//! use std::sync::Arc;
//!
//! async fn demo_page() -> impl axum::response::IntoResponse {
//!     "hello".into_response()
//! }
//!
//! #[derive(HttpServingInstance)]
//! #[flag_prefix = "foo-"]
//! pub struct FooServer(#[router] Router);
//!
//! #[resource]
//! impl Resource for FooServer {
//!     const NAME: &str = "Test HTTP server";
//!
//!     fn new(
//!         _: NoDependencies,
//!         _: NoArgs,
//!         _: &mut AssemblyRuntime<'_>,
//!     ) -> Result<Arc<Self>, std::convert::Infallible> {
//!         let app = Router::new()
//!             .route("/fooz", axum::routing::get(demo_page));
//!         Ok(Arc::new(Self(app)))
//!     }
//! }
//!
//! #[derive(ResourceDependencies)]
//! struct JustAServer {
//!     server: std::sync::Arc<HttpServer<FooServer>>,
//! }
//!
//! #[cfg(feature = "tls")]
//! let assembly = comprehensive::Assembly::<JustAServer>::new().unwrap();
//! ```

use axum::Router;
#[cfg(feature = "tls")]
use axum_server::tls_rustls::RustlsConfig;
use comprehensive::v1::{AssemblyRuntime, Resource, StopSignal, resource};
use comprehensive::{NoArgs, NoDependencies, ResourceDependencies};
use futures::future::Either;
use futures::pin_mut;
use pin_project_lite::pin_project;
use std::marker::PhantomData;
use std::net::IpAddr;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};
use tower_service::Service;
use tracing::instrument::Instrument as _;
use tracing::{Span, info, warn};

#[derive(Clone)]
struct SpannedService<S>(S, Span);

impl<R, S: Service<R>> Service<R> for SpannedService<S> {
    type Response = S::Response;
    type Error = S::Error;
    type Future = tracing::instrument::Instrumented<S::Future>;

    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        let _g = self.1.enter();
        self.0.poll_ready(cx)
    }

    fn call(&mut self, r: R) -> Self::Future {
        {
            let _g = self.1.enter();
            self.0.call(r)
        }
        .instrument(self.1.clone())
    }
}

pin_project! {
    struct MakeSpannedService<F> {
        #[pin] inner: F,
        span: Span,
    }
}

impl<F, S, E> Future for MakeSpannedService<F>
where
    F: Future<Output = Result<S, E>>,
{
    type Output = Result<SpannedService<S>, E>;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let this = self.project();
        match this.inner.poll(cx) {
            Poll::Ready(Ok(s)) => Poll::Ready(Ok(SpannedService(s, this.span.clone()))),
            Poll::Ready(Err(e)) => Poll::Ready(Err(e)),
            Poll::Pending => Poll::Pending,
        }
    }
}

struct MakeService<S>(S, Span);

impl<R, S: Service<R>> Service<R> for MakeService<S> {
    type Response = SpannedService<S::Response>;
    type Error = S::Error;
    type Future = MakeSpannedService<S::Future>;

    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        self.0.poll_ready(cx)
    }

    fn call(&mut self, r: R) -> Self::Future {
        MakeSpannedService {
            inner: self.0.call(r),
            span: self.1.clone(),
        }
    }
}

async fn run_in_task<A>(
    b: axum_server::Server<A>,
    term: StopSignal,
    router: Router,
) -> Result<(), Box<dyn std::error::Error>>
where
    A: axum_server::accept::Accept<tokio::net::TcpStream, SpannedService<Router>>
        + Clone
        + Send
        + Sync
        + 'static,
    A::Stream: tokio::io::AsyncRead + tokio::io::AsyncWrite + Unpin + Send,
    A::Service: axum_server::service::SendService<http::Request<hyper::body::Incoming>> + Send,
    A::Future: Send,
{
    let handle = axum_server::Handle::new();
    let handle_for_task = handle.clone();
    let make_service = MakeService(router.into_make_service(), Span::current());
    let task = tokio::spawn(
        async move { b.handle(handle_for_task).serve(make_service).await }.in_current_span(),
    );
    pin_mut!(term);
    pin_mut!(task);
    match futures::future::select(term, task).await {
        Either::Left(((), task)) => {
            handle.graceful_shutdown(None);
            task.await
        }
        Either::Right((result, _)) => result,
    }??;
    Ok(())
}

/// A trait indicating a [`Resource`] that can back an [`HttpServer`].
///
/// This trait is normally derived like this:
///
/// ```
/// use axum::Router;
/// use comprehensive::{NoArgs, NoDependencies};
/// use comprehensive::v1::{AssemblyRuntime, Resource, resource};
/// use comprehensive_http::HttpServingInstance;
///
/// #[derive(HttpServingInstance)]
/// #[flag_prefix = "foo-"]
/// pub struct FooServer(#[router] Router);
/// # #[resource]
/// # impl Resource for FooServer {
/// #     const NAME: &str = "Test HTTP server";
/// #
/// #     fn new(
/// #         _: NoDependencies, _: NoArgs, _: &mut AssemblyRuntime<'_>,
/// #     ) -> Result<std::sync::Arc<Self>, std::convert::Infallible> {
/// #         Ok(std::sync::Arc::new(Self(Router::new())))
/// #     }
/// # }
/// ```
///
/// There are 2 required derive attributes:
///
/// * `#[flag_prefix = string literal]`: prefix that will be prepended to all
///   flags that the [`HttpServer`] installs, to disambiguate this instance.
/// * `#[router]` should be attached to exactly one of the fields of the struct
///   on which this trait is derived. That field must be of type [`axum::Router`]
///   and identifies the service that the HTTP server will dispatch to.
pub trait HttpServingInstance: Resource {
    #[doc(hidden)]
    const HTTP_PORT_FLAG_NAME: &str;
    #[doc(hidden)]
    const HTTP_BIND_ADDR_FLAG_NAME: &str;
    #[doc(hidden)]
    const HTTPS_PORT_FLAG_NAME: &str;
    #[doc(hidden)]
    const HTTPS_BIND_ADDR_FLAG_NAME: &str;

    #[doc(hidden)]
    fn get_router(&self) -> Router;
}

pub use comprehensive_macros::HttpServingInstance;

struct Conveyor<T> {
    data: std::sync::Mutex<Option<T>>,
}

impl<T> Conveyor<T> {
    fn new() -> Self {
        Self {
            data: std::sync::Mutex::new(None),
        }
    }

    fn put(&self, data: T) {
        *(self.data.lock().unwrap()) = Some(data);
    }

    fn get(&self) -> T {
        self.data
            .lock()
            .unwrap()
            .take()
            .expect("Conveyor unexpectedly empty")
    }
}

mod insecure_server {
    use super::*;

    #[derive(clap::Args, Debug)]
    #[group(skip)]
    pub(super) struct InsecureHttpServerArgs<I: HttpServingInstance> {
        #[arg(
            long(I::HTTP_PORT_FLAG_NAME),
            id(I::HTTP_PORT_FLAG_NAME),
            help = "TCP port number for insecure HTTP server. If unset, plain HTTP is not served."
        )]
        http_port: Option<u16>,

        #[arg(
            long(I::HTTP_BIND_ADDR_FLAG_NAME),
            id(I::HTTP_BIND_ADDR_FLAG_NAME),
            default_value = "::",
            help = "Binding IP address for HTTP. Used only if the corresponding port is set."
        )]
        http_bind_addr: IpAddr,

        #[clap(skip = PhantomData)]
        _i: PhantomData<I>,
    }

    pub(super) struct InsecureHttpServer<I>
    where
        I: HttpServingInstance,
    {
        pub(super) conf: Option<Conveyor<Router>>,
        _i: PhantomData<I>,
    }

    #[resource]
    impl<I: HttpServingInstance> Resource for InsecureHttpServer<I> {
        const NAME: &str = "Plaintext HTTP server";

        fn new(
            _: NoDependencies,
            args: InsecureHttpServerArgs<I>,
            api: &mut AssemblyRuntime<'_>,
        ) -> Result<Arc<Self>, std::convert::Infallible> {
            Ok(match args.http_port {
                Some(port) => {
                    let addr = (args.http_bind_addr, port).into();
                    let server = axum_server::bind(addr);
                    info!("{}: Insecure HTTP server listening on {}", I::NAME, addr);
                    let shared = Arc::new(Self {
                        conf: Some(Conveyor::new()),
                        _i: PhantomData,
                    });
                    let task_shared = Arc::clone(&shared);
                    let stop = api.self_stop();
                    api.set_task(async move {
                        let s = task_shared.conf.as_ref().unwrap().get();
                        drop(task_shared);
                        run_in_task(server, stop, s).await?;
                        Ok(())
                    });
                    shared
                }
                None => Arc::new(Self {
                    conf: None,
                    _i: PhantomData,
                }),
            })
        }
    }
}

#[cfg(feature = "tls")]
mod secure_server {
    use super::*;

    use thiserror::Error;

    /// Error type returned by Comprehensive HTTPS server
    #[derive(Debug, Error)]
    pub enum ComprehensiveHttpsError {
        /// An error from [`comprehensive_tls`].
        #[error("{0}")]
        ComprehensiveTlsError(#[from] comprehensive_tls::ComprehensiveTlsError),
        /// HTTPS serving is requested but no TLS parameters are available.
        #[error("HTTPS serving is requested but no TLS parameters are available")]
        NoTlsProvider,
    }

    #[cfg(not(test))]
    type TlsConfig = comprehensive_tls::TlsConfig;
    #[cfg(test)]
    type TlsConfig = crate::testutil::tls::MockTlsConfig;

    #[derive(clap::Args, Debug)]
    #[group(skip)]
    pub(super) struct SecureHttpServerArgs<I: HttpServingInstance> {
        #[arg(
            long(I::HTTPS_PORT_FLAG_NAME),
            id(I::HTTPS_PORT_FLAG_NAME),
            help = "TCP port number for HTTPS server. If unset, HTTPS is not served."
        )]
        https_port: Option<u16>,

        #[arg(
            long(I::HTTPS_BIND_ADDR_FLAG_NAME),
            id(I::HTTPS_BIND_ADDR_FLAG_NAME),
            default_value = "::",
            help = "Binding IP address for HTTPS. Used only if the corresponding port is set."
        )]
        https_bind_addr: IpAddr,

        #[clap(skip = PhantomData)]
        _i: PhantomData<I>,
    }

    #[derive(ResourceDependencies)]
    pub(super) struct SecureHttpServerDependencies {
        tls: Option<Arc<TlsConfig>>,
    }

    pub(super) struct SecureHttpServer<I>
    where
        I: HttpServingInstance,
    {
        pub(super) conf: Option<Conveyor<Router>>,
        _i: PhantomData<I>,
    }

    #[resource]
    impl<I: HttpServingInstance> Resource for SecureHttpServer<I> {
        const NAME: &str = "HTTPS server";

        fn new(
            d: SecureHttpServerDependencies,
            args: SecureHttpServerArgs<I>,
            api: &mut AssemblyRuntime<'_>,
        ) -> Result<Arc<Self>, ComprehensiveHttpsError> {
            let Some(port) = args.https_port else {
                return Ok(Arc::new(Self {
                    conf: None,
                    _i: PhantomData,
                }));
            };
            let Some(tlsc) = d.tls else {
                return Err(ComprehensiveHttpsError::NoTlsProvider);
            };
            let addr = (args.https_bind_addr, port).into();
            let mut sc = tlsc.server_config::<comprehensive_tls::ClientAuthDisabled>();
            sc.alpn_protocols = vec![b"h2".to_vec(), b"http/1.1".to_vec()];
            let config = RustlsConfig::from_config(Arc::new(sc));
            let server = axum_server::bind_rustls(addr, config);
            info!("{}: Secure HTTP server listening on {}", I::NAME, addr);
            let shared = Arc::new(Self {
                conf: Some(Conveyor::new()),
                _i: PhantomData,
            });
            let task_shared = Arc::clone(&shared);
            let stop = api.self_stop();
            api.set_task(async move {
                let s = task_shared.conf.as_ref().unwrap().get();
                drop(task_shared);
                run_in_task(server, stop, s).await?;
                Ok(())
            });
            Ok(shared)
        }
    }
}

#[doc(hidden)]
#[derive(ResourceDependencies)]
pub struct HttpServerDependencies<I>
where
    I: HttpServingInstance,
{
    #[old_style]
    instance: Arc<I>,
    http: Arc<insecure_server::InsecureHttpServer<I>>,
    #[cfg(feature = "tls")]
    https: Arc<secure_server::SecureHttpServer<I>>,
}

/// HTTP and HTTPS server [`Resource`].
///
/// Accepts another [`Resource`] as a parameter which must implement
/// [`HttpServingInstance`], and dispatches requests to the `axum`
/// server therein.
///
/// Each instance accepts flags with a different prefix:
///
/// | Flag                       | Default    | Meaning                 |
/// |----------------------------|------------|-------------------------|
/// | `--PREFIX-http_port`       | *none*     | TCP port number for insecure HTTP server. If unset, plain HTTP is not served. |
/// | `--PREFIX-http_bind_addr`  | `::`       | Binding IP address for HTTP. Used only if `--http_port` is set. |
/// | `--PREFIX-https_port`      | *none*     | TCP port number for secure HTTP server. If unset, HTTPS is not served. |
/// | `--PREFIX-https_bind_addr` | `::`       | Binding IP address for HTTPS. Used only if `--https_port` is set. |
pub struct HttpServer<I>
where
    I: HttpServingInstance,
{
    _instance: PhantomData<I>,
}

#[resource]
impl<I: HttpServingInstance> Resource for HttpServer<I> {
    const NAME: &str = "HTTP server common";

    fn new(
        d: HttpServerDependencies<I>,
        _: NoArgs,
        api: &mut AssemblyRuntime<'_>,
    ) -> Result<Arc<Self>, std::convert::Infallible> {
        api.set_task(ConfigureServers { deps: d });
        Ok(Arc::new(Self {
            _instance: PhantomData,
        }))
    }
}

struct ConfigureServers<I: HttpServingInstance> {
    deps: HttpServerDependencies<I>,
}

struct NoTask;

impl Future for NoTask {
    type Output = Result<(), Box<dyn std::error::Error>>;

    fn poll(
        self: std::pin::Pin<&mut Self>,
        _: &mut std::task::Context<'_>,
    ) -> std::task::Poll<Self::Output> {
        std::task::Poll::Ready(Ok(()))
    }
}

impl<I: HttpServingInstance> IntoFuture for ConfigureServers<I> {
    type IntoFuture = NoTask;
    type Output = Result<(), Box<dyn std::error::Error>>;

    fn into_future(self) -> Self::IntoFuture {
        #[cfg(not(feature = "tls"))]
        if self.deps.http.conf.is_none() {
            warn!(
                "{}: No insecure HTTP listener, and secure HTTP is not available because feature \"tls\" is not built.",
                I::NAME
            );
            return NoTask;
        }

        #[cfg(feature = "tls")]
        if self.deps.http.conf.is_none() && self.deps.https.conf.is_none() {
            warn!("{}: No insecure or secure HTTP listener.", I::NAME);
            return NoTask;
        }

        let router = self.deps.instance.get_router();

        #[cfg(not(feature = "tls"))]
        self.deps.http.conf.as_ref().unwrap().put(router);

        #[cfg(feature = "tls")]
        match (self.deps.http.conf.as_ref(), self.deps.https.conf.as_ref()) {
            (None, None) => (),
            (Some(s), None) => s.put(router),
            (None, Some(s)) => s.put(router),
            (Some(s1), Some(s2)) => {
                s1.put(router.clone());
                s2.put(router);
            }
        }

        NoTask
    }
}

#[cfg(test)]
mod tests {
    use axum::response::IntoResponse;
    use futures::FutureExt;
    use std::sync::Arc;

    use super::*;
    use crate::testutil;

    async fn demo_page() -> impl axum::response::IntoResponse {
        "hello".into_response()
    }

    #[derive(HttpServingInstance)]
    #[flag_prefix = "foo-"]
    pub struct FooServer(#[router] Router);

    #[resource]
    impl Resource for FooServer {
        const NAME: &str = "Test HTTP server";

        fn new(
            _: NoDependencies,
            _: NoArgs,
            _: &mut AssemblyRuntime<'_>,
        ) -> Result<Arc<Self>, std::convert::Infallible> {
            let app = Router::new().route("/fooz", axum::routing::get(demo_page));
            Ok(Arc::new(Self(app)))
        }
    }

    #[derive(ResourceDependencies)]
    struct JustAServer {
        _server: Arc<HttpServer<FooServer>>,
        http: Arc<insecure_server::InsecureHttpServer<FooServer>>,
        #[cfg(feature = "tls")]
        https: Arc<secure_server::SecureHttpServer<FooServer>>,
    }

    fn test_args(
        http_port: Option<u16>,
        https_port: Option<u16>,
    ) -> impl IntoIterator<Item = std::ffi::OsString> {
        let mut v = vec!["cmd".into()];
        if let Some(port) = http_port {
            v.push(format!("--foo-http-port={}", port).into());
            v.push("--foo-http-bind-addr=::1".into());
        }
        #[cfg(feature = "tls")]
        if let Some(port) = https_port {
            v.push(format!("--foo-https-port={}", port).into());
            v.push("--foo-https-bind-addr=::1".into());
        }
        #[cfg(not(feature = "tls"))]
        let _ = https_port;
        v
    }

    #[tokio::test]
    async fn nothing_enabled() {
        let argv = vec!["cmd"];
        let assembly = comprehensive::Assembly::<JustAServer>::new_from_argv(argv).unwrap();

        assert!(assembly.top.http.conf.is_none());
        #[cfg(feature = "tls")]
        assert!(assembly.top.https.conf.is_none());

        assert!(
            assembly
                .run_with_termination_signal(futures::stream::pending())
                .await
                .is_ok()
        );
    }

    #[tokio::test]
    async fn http_only() {
        let port = testutil::pick_unused_port(None);
        let argv = test_args(Some(port), None);
        let assembly = comprehensive::Assembly::<JustAServer>::new_from_argv(argv).unwrap();

        assert!(assembly.top.http.conf.is_some());
        #[cfg(feature = "tls")]
        assert!(assembly.top.https.conf.is_none());

        let (tx, rx) = tokio::sync::oneshot::channel();
        let j = tokio::spawn(async move {
            let _ = assembly
                .run_with_termination_signal(futures::stream::once(rx.map(|_| ())))
                .await;
        });
        let addr = testutil::localhost(port);
        testutil::wait_until_serving(&addr).await;

        let url = format!("http://[::1]:{}/", port);
        let resp = reqwest::get(&url).await.expect(&url);
        assert_eq!(resp.status(), reqwest::StatusCode::NOT_FOUND);

        let url = format!("http://[::1]:{}/fooz", port);
        let text = reqwest::get(&url)
            .await
            .expect(&url)
            .text()
            .await
            .expect(&url);
        assert!(text.contains("hello"));

        let _ = tx.send(());
        let _ = j.await;
    }

    #[cfg(feature = "tls")]
    #[tokio::test]
    async fn https_only() {
        let port = testutil::pick_unused_port(None);
        let argv = test_args(None, Some(port));
        let assembly = comprehensive::Assembly::<JustAServer>::new_from_argv(argv).unwrap();

        assert!(assembly.top.http.conf.is_none());
        assert!(assembly.top.https.conf.is_some());

        let (tx, rx) = tokio::sync::oneshot::channel();
        let j = tokio::spawn(async move {
            let _ = assembly
                .run_with_termination_signal(futures::stream::once(rx.map(|_| ())))
                .await;
        });
        let addr = testutil::localhost(port);
        testutil::wait_until_serving(&addr).await;

        let cacert = reqwest::Certificate::from_pem(crate::tls_testdata::CACERT).expect("cacert");
        let client = reqwest::ClientBuilder::new()
            .add_root_certificate(cacert)
            .resolve("user1", addr.clone())
            .build()
            .expect("cacert");

        let url = format!("https://user1:{}/", addr.port());
        let resp = client.get(&url).send().await.expect(&url);
        assert_eq!(resp.status(), reqwest::StatusCode::NOT_FOUND);

        let _ = tx.send(());
        let _ = j.await;
    }

    #[cfg(feature = "tls")]
    #[tokio::test]
    async fn http_and_https() {
        let port_http = testutil::pick_unused_port(None);
        let port_https = testutil::pick_unused_port(Some(port_http));
        let argv = test_args(Some(port_http), Some(port_https));
        let assembly = comprehensive::Assembly::<JustAServer>::new_from_argv(argv).unwrap();

        assert!(assembly.top.http.conf.is_some());
        assert!(assembly.top.https.conf.is_some());

        let (tx, rx) = tokio::sync::oneshot::channel();
        let j = tokio::spawn(async move {
            let _ = assembly
                .run_with_termination_signal(futures::stream::once(rx.map(|_| ())))
                .await;
        });
        let addr_http = testutil::localhost(port_http);
        let addr_https = testutil::localhost(port_https);
        testutil::wait_until_serving(&addr_http).await;
        testutil::wait_until_serving(&addr_https).await;

        let cacert = reqwest::Certificate::from_pem(crate::tls_testdata::CACERT).expect("cacert");
        let client = reqwest::ClientBuilder::new()
            .add_root_certificate(cacert)
            .resolve("user1", addr_https.clone())
            .build()
            .expect("cacert");

        let url = format!("http://user1:{}/", addr_http.port());
        let resp = client.get(&url).send().await.expect(&url);
        assert_eq!(resp.status(), reqwest::StatusCode::NOT_FOUND);

        let url = format!("https://user1:{}/", addr_https.port());
        let resp = client.get(&url).send().await.expect(&url);
        assert_eq!(resp.status(), reqwest::StatusCode::NOT_FOUND);

        let _ = tx.send(());
        let _ = j.await;
    }

    #[derive(HttpServingInstance)]
    #[flag_prefix = "bar-"]
    pub struct BarServer(#[router] Router);

    #[resource]
    impl Resource for BarServer {
        const NAME: &str = "Second Test HTTP server";

        fn new(
            _: NoDependencies,
            _: NoArgs,
            _: &mut AssemblyRuntime<'_>,
        ) -> Result<Arc<Self>, std::convert::Infallible> {
            Ok(Arc::new(Self(Router::new())))
        }
    }

    #[derive(ResourceDependencies)]
    struct JustTwoServers {
        _server1: Arc<HttpServer<FooServer>>,
        _server2: Arc<HttpServer<BarServer>>,
    }

    #[test]
    fn two_servers() {
        let argv = vec!["cmd"];
        let _ = comprehensive::Assembly::<JustTwoServers>::new_from_argv(argv).unwrap();
    }
}