use std::net::SocketAddr;
pub type HttpRequest = http::Request<Body>;
pub use http::HeaderMap as Headers;
pub use hyper::Body;
pub use jsonrpsee_types::Params;
#[derive(Debug, Copy, Clone)]
pub enum MethodKind {
Subscription,
Unsubscription,
MethodCall,
Unknown,
}
impl std::fmt::Display for MethodKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let s = match self {
Self::Subscription => "subscription",
Self::MethodCall => "method call",
Self::Unknown => "unknown",
Self::Unsubscription => "unsubscription",
};
write!(f, "{}", s)
}
}
#[derive(Debug, Copy, Clone)]
pub enum TransportProtocol {
Http,
WebSocket,
}
impl std::fmt::Display for TransportProtocol {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let s = match self {
Self::Http => "http",
Self::WebSocket => "websocket",
};
write!(f, "{}", s)
}
}
pub trait Logger: Send + Sync + Clone + 'static {
type Instant: std::fmt::Debug + Send + Sync + Copy;
fn on_connect(&self, _remote_addr: SocketAddr, _request: &HttpRequest, _t: TransportProtocol);
fn on_request(&self, transport: TransportProtocol) -> Self::Instant;
fn on_call(&self, method_name: &str, params: Params, kind: MethodKind, transport: TransportProtocol);
fn on_result(&self, method_name: &str, success: bool, started_at: Self::Instant, transport: TransportProtocol);
fn on_response(&self, result: &str, started_at: Self::Instant, transport: TransportProtocol);
fn on_disconnect(&self, _remote_addr: SocketAddr, transport: TransportProtocol);
}
impl Logger for () {
type Instant = ();
fn on_connect(&self, _: SocketAddr, _: &HttpRequest, _p: TransportProtocol) -> Self::Instant {}
fn on_request(&self, _p: TransportProtocol) -> Self::Instant {}
fn on_call(&self, _: &str, _: Params, _: MethodKind, _p: TransportProtocol) {}
fn on_result(&self, _: &str, _: bool, _: Self::Instant, _p: TransportProtocol) {}
fn on_response(&self, _: &str, _: Self::Instant, _p: TransportProtocol) {}
fn on_disconnect(&self, _: SocketAddr, _p: TransportProtocol) {}
}
impl<A, B> Logger for (A, B)
where
A: Logger,
B: Logger,
{
type Instant = (A::Instant, B::Instant);
fn on_connect(&self, remote_addr: std::net::SocketAddr, request: &HttpRequest, transport: TransportProtocol) {
self.0.on_connect(remote_addr, request, transport);
self.1.on_connect(remote_addr, request, transport);
}
fn on_request(&self, transport: TransportProtocol) -> Self::Instant {
(self.0.on_request(transport), self.1.on_request(transport))
}
fn on_call(&self, method_name: &str, params: Params, kind: MethodKind, transport: TransportProtocol) {
self.0.on_call(method_name, params.clone(), kind, transport);
self.1.on_call(method_name, params, kind, transport);
}
fn on_result(&self, method_name: &str, success: bool, started_at: Self::Instant, transport: TransportProtocol) {
self.0.on_result(method_name, success, started_at.0, transport);
self.1.on_result(method_name, success, started_at.1, transport);
}
fn on_response(&self, result: &str, started_at: Self::Instant, transport: TransportProtocol) {
self.0.on_response(result, started_at.0, transport);
self.1.on_response(result, started_at.1, transport);
}
fn on_disconnect(&self, remote_addr: SocketAddr, transport: TransportProtocol) {
self.0.on_disconnect(remote_addr, transport);
self.1.on_disconnect(remote_addr, transport);
}
}