Skip to main content

trillium/
info.rs

1use crate::{Listener, Listeners};
2use std::net::SocketAddr;
3use trillium_http::{HttpConfig, HttpContext, Swansong, TypeSet, type_set::entry::Entry};
4
5/// This struct represents information about the currently connected
6/// server.
7///
8/// It is passed to [`Handler::init`](crate::Handler::init).
9
10#[derive(Debug, Default)]
11pub struct Info(HttpContext);
12impl From<HttpContext> for Info {
13    fn from(value: HttpContext) -> Self {
14        Self(value)
15    }
16}
17impl From<Info> for HttpContext {
18    fn from(value: Info) -> Self {
19        value.0
20    }
21}
22
23impl AsRef<TypeSet> for Info {
24    fn as_ref(&self) -> &TypeSet {
25        self.0.as_ref()
26    }
27}
28impl AsMut<TypeSet> for Info {
29    fn as_mut(&mut self) -> &mut TypeSet {
30        self.0.as_mut()
31    }
32}
33
34impl Info {
35    /// Returns the `local_addr` of a bound tcp listener, if such a
36    /// thing exists for this server
37    pub fn tcp_socket_addr(&self) -> Option<&SocketAddr> {
38        self.shared_state()
39    }
40
41    /// Returns every [`Listener`] this server is bound to, in registration order.
42    ///
43    /// Empty until the server populates its listener set during startup.
44    pub fn listeners(&self) -> &[Listener] {
45        self.shared_state::<Listeners>().map_or(&[], |l| &l.0)
46    }
47
48    /// Returns the `local_addr` of a bound unix listener, if such a
49    /// thing exists for this server
50    #[cfg(unix)]
51    pub fn unix_socket_addr(&self) -> Option<&std::os::unix::net::SocketAddr> {
52        self.shared_state()
53    }
54
55    /// Borrow a type from the shared state [`TypeSet`] on this `Info`.
56    pub fn shared_state<T: Send + Sync + 'static>(&self) -> Option<&T> {
57        self.0.shared_state().get()
58    }
59
60    /// Insert a type into the shared state typeset, returning the previous value if any
61    pub fn insert_shared_state<T: Send + Sync + 'static>(&mut self, value: T) -> Option<T> {
62        self.0.shared_state_mut().insert(value)
63    }
64
65    /// Mutate a type in the shared state typeset
66    pub fn shared_state_mut<T: Send + Sync + 'static>(&mut self) -> Option<&mut T> {
67        self.0.shared_state_mut().get_mut()
68    }
69
70    /// Returns an [`Entry`] into the shared state typeset.
71    pub fn shared_state_entry<T: Send + Sync + 'static>(&mut self) -> Entry<'_, T> {
72        self.0.shared_state_mut().entry()
73    }
74
75    /// chainable interface to insert a type into the shared state typeset
76    #[must_use]
77    pub fn with_shared_state<T: Send + Sync + 'static>(mut self, value: T) -> Self {
78        self.insert_shared_state(value);
79        self
80    }
81
82    /// borrow the http config
83    pub fn config(&self) -> &HttpConfig {
84        self.0.config()
85    }
86
87    /// mutate the http config
88    pub fn config_mut(&mut self) -> &mut HttpConfig {
89        self.0.config_mut()
90    }
91
92    /// Borrow the [`Swansong`] graceful shutdown interface for this server
93    pub fn swansong(&self) -> &Swansong {
94        self.0.swansong()
95    }
96}