ntex_h2/server/
builder.rs1use 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#[derive(Clone, Debug)]
19pub struct ServerBuilder<E, Ctl = DefaultControlService> {
20 control: Ctl,
21 config: Config,
22 _t: marker::PhantomData<E>,
23}
24
25impl<E> ServerBuilder<E> {
28 pub fn new() -> ServerBuilder<E> {
33 ServerBuilder {
34 config: Config::server(),
35 control: DefaultControlService,
36 _t: marker::PhantomData,
37 }
38 }
39
40 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 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 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}