volo_http/context/
server.rs1use chrono::{DateTime, Local};
4use volo::{
5 context::{Context, Reusable, Role, RpcCx, RpcInfo},
6 net::Address,
7 newtype_impl_context,
8};
9
10use crate::{
11 server::param::PathParamsVec,
12 utils::macros::{impl_deref_and_deref_mut, impl_getter, stat_impl},
13};
14
15#[derive(Debug)]
17pub struct ServerContext(pub(crate) RpcCx<ServerCxInner, Config>);
18
19impl ServerContext {
20 pub fn new(peer: Address) -> Self {
22 let mut cx = RpcCx::new(
23 RpcInfo::<Config>::with_role(Role::Server),
24 ServerCxInner::default(),
25 );
26 cx.rpc_info_mut().caller_mut().set_address(peer);
27 Self(cx)
28 }
29}
30
31impl_deref_and_deref_mut!(ServerContext, RpcCx<ServerCxInner, Config>, 0);
32
33newtype_impl_context!(ServerContext, Config, 0);
34
35#[derive(Clone, Debug, Default)]
37pub struct ServerCxInner {
38 pub params: PathParamsVec,
48
49 pub stats: ServerStats,
51}
52
53impl ServerCxInner {
54 impl_getter!(params, PathParamsVec);
55 impl_getter!(stats, ServerStats);
56}
57
58#[derive(Debug, Default, Clone)]
60pub struct ServerStats {
61 read_header_start: Option<DateTime<Local>>,
62 read_header_finish: Option<DateTime<Local>>,
63 read_body_start: Option<DateTime<Local>>,
64 read_body_finish: Option<DateTime<Local>>,
65 handle_start: Option<DateTime<Local>>,
66 handle_finish: Option<DateTime<Local>>,
67 write_start: Option<DateTime<Local>>,
68 write_finish: Option<DateTime<Local>>,
69}
70
71impl ServerStats {
72 stat_impl!(read_header_start);
73 stat_impl!(read_header_finish);
74 stat_impl!(read_body_start);
75 stat_impl!(read_body_finish);
76 stat_impl!(handle_start);
77 stat_impl!(handle_finish);
78 stat_impl!(write_start);
79 stat_impl!(write_finish);
80}
81
82#[derive(Clone, Debug, Default)]
86pub struct Config {
87 #[cfg(feature = "__tls")]
88 tls: bool,
89}
90
91impl Config {
92 #[cfg(feature = "__tls")]
94 pub fn is_tls(&self) -> bool {
95 self.tls
96 }
97
98 #[cfg(feature = "__tls")]
99 pub(crate) fn set_tls(&mut self, tls: bool) {
100 self.tls = tls;
101 }
102}
103
104impl Reusable for Config {
105 fn clear(&mut self) {
106 *self = Default::default();
107 }
108}