ntex_h2/server/
builder.rs

1use std::{fmt, marker};
2
3use ntex_service::{IntoServiceFactory, ServiceFactory};
4
5use crate::control::{Control, ControlAck};
6use crate::{config::Config, default::DefaultControlService, message::Message, server::Server};
7
8/// Builds server with custom configuration values.
9///
10/// Methods can be chained in order to set the configuration values.
11///
12/// New instances of `Builder` are obtained via [`Builder::new`].
13///
14/// See function level documentation for details on the various server
15/// configuration settings.
16///
17/// [`Builder::new`]: struct.ServerBuilder.html#method.new
18#[derive(Clone, Debug)]
19pub struct ServerBuilder<E, Ctl = DefaultControlService> {
20    control: Ctl,
21    config: Config,
22    _t: marker::PhantomData<E>,
23}
24
25// ===== impl Builder =====
26
27impl<E> ServerBuilder<E> {
28    /// Returns a new server builder instance initialized with default
29    /// configuration values.
30    ///
31    /// Configuration methods can be chained on the return value.
32    pub fn new() -> ServerBuilder<E> {
33        ServerBuilder {
34            config: Config::server(),
35            control: DefaultControlService,
36            _t: marker::PhantomData,
37        }
38    }
39
40    /// Configure connection settings
41    pub fn configure<'a, F, R>(&'a self, f: F) -> &'a Self
42    where
43        F: FnOnce(&'a Config) -> R + 'a,
44    {
45        let _ = f(&self.config);
46        self
47    }
48}
49
50impl<E: fmt::Debug, Ctl> ServerBuilder<E, Ctl> {
51    /// Service to call with control frames
52    pub fn control<S, F>(&self, service: F) -> ServerBuilder<E, S>
53    where
54        F: IntoServiceFactory<S, Control<E>>,
55        S: ServiceFactory<Control<E>, Response = ControlAck> + 'static,
56        S::Error: fmt::Debug,
57        S::InitError: fmt::Debug,
58    {
59        ServerBuilder {
60            control: service.into_factory(),
61            config: self.config.clone(),
62            _t: marker::PhantomData,
63        }
64    }
65}
66
67impl<E, Ctl> ServerBuilder<E, Ctl>
68where
69    E: fmt::Debug,
70    Ctl: ServiceFactory<Control<E>, Response = ControlAck> + 'static,
71    Ctl::Error: fmt::Debug,
72    Ctl::InitError: fmt::Debug,
73{
74    /// Creates a new configured HTTP/2 server.
75    pub fn finish<F, Pub>(self, service: F) -> Server<Ctl, Pub>
76    where
77        F: IntoServiceFactory<Pub, Message>,
78        Pub: ServiceFactory<Message, Response = (), Error = E> + 'static,
79        Pub::InitError: fmt::Debug,
80    {
81        Server::new(self.config, self.control, service.into_factory())
82    }
83}
84
85impl<E> Default for ServerBuilder<E> {
86    fn default() -> ServerBuilder<E> {
87        ServerBuilder::new()
88    }
89}