JavaScript is disabled, refresh for a better experience. ambee/giterated-api

ambee/giterated-api

Git repository hosting, collaboration, and discovery for the Fediverse.

begin new protocol refactor

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨2091700

⁨src/lib.rs⁩ - ⁨1477⁩ bytes
Raw
1 use std::{fmt::Debug, net::SocketAddr};
2
3 use deadpool::managed::{BuildError, Pool};
4 use giterated_models::model::instance::Instance;
5 use pool::GiteratedConnectionPool;
6 use tokio::net::TcpStream;
7 use tokio_tungstenite::{MaybeTlsStream, WebSocketStream};
8
9 pub mod daemon_backend;
10 mod handshake;
11 mod pool;
12
13 type Socket = WebSocketStream<MaybeTlsStream<TcpStream>>;
14
15 #[macro_use]
16 extern crate tracing;
17
18 #[derive(Clone)]
19 pub struct DaemonConnectionPool(Pool<GiteratedConnectionPool>);
20
21 impl Debug for DaemonConnectionPool {
22 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
23 f.debug_tuple("DaemonConnectionPool").finish()
24 }
25 }
26
27 impl DaemonConnectionPool {
28 pub fn connect(
29 instance: impl ToOwned<Owned = Instance>,
30 ) -> Result<Self, BuildError<anyhow::Error>> {
31 let instance = instance.to_owned();
32 Ok(Self(
33 Pool::builder(GiteratedConnectionPool {
34 socket_addr: None,
35 target_instance: instance.to_owned(),
36 })
37 .build()?,
38 ))
39 }
40
41 pub fn connect_other(
42 instance_identity: impl ToOwned<Owned = Instance>,
43 connection_addr: SocketAddr,
44 ) -> Result<Self, BuildError<anyhow::Error>> {
45 Ok(Self(
46 Pool::builder(GiteratedConnectionPool {
47 target_instance: instance_identity.to_owned(),
48 socket_addr: Some(connection_addr),
49 })
50 .build()?,
51 ))
52 }
53 }
54