feat: added codemp config, changed connect methods

This commit is contained in:
əlemi 2024-09-11 15:12:31 +02:00
parent ba566439d3
commit d57fb2c4b6
Signed by: alemi
GPG key ID: A4895B84D311642C
4 changed files with 59 additions and 21 deletions

44
src/api/config.rs Normal file
View file

@ -0,0 +1,44 @@
//! # Config
//! Data structure defining clients configuration
/// Configuration struct for `codemp` client
#[derive(Debug, Clone)]
pub struct Config {
/// user identifier used to register, possibly your email
pub username: String,
/// user password chosen upon registration
pub password: String,
/// address of server to connect to, default api.code.mp
pub host: Option<String>,
/// port to connect to, default 50053
pub port: Option<u16>,
/// enable or disable tls, default true
pub tls: Option<bool>,
}
impl Config {
#[inline]
pub(crate) fn host(&self) -> &str {
self.host.as_deref().unwrap_or("api.code.mp")
}
#[inline]
pub(crate) fn port(&self) -> u16 {
self.port.unwrap_or(50053)
}
#[inline]
pub(crate) fn tls(&self) -> bool {
self.tls.unwrap_or(true)
}
pub(crate) fn endpoint(&self) -> String {
format!(
"{}{}:{}",
if self.tls() { "https://" } else { "http" },
self.host(),
self.port()
)
}
}

View file

@ -7,6 +7,9 @@ pub mod controller;
/// a generic representation of a text change /// a generic representation of a text change
pub mod change; pub mod change;
/// client configuration
pub mod config;
/// representation for an user's cursor /// representation for an user's cursor
pub mod cursor; pub mod cursor;
@ -18,6 +21,7 @@ pub mod user;
pub use controller::Controller; pub use controller::Controller;
pub use change::TextChange; pub use change::TextChange;
pub use config::Config;
pub use cursor::Cursor; pub use cursor::Cursor;
pub use event::Event; pub use event::Event;
pub use user::User; pub use user::User;

View file

@ -28,7 +28,7 @@ pub struct Client(Arc<ClientInner>);
#[derive(Debug)] #[derive(Debug)]
struct ClientInner { struct ClientInner {
user: User, user: User,
host: String, config: crate::api::Config,
workspaces: DashMap<String, Workspace>, workspaces: DashMap<String, Workspace>,
auth: AuthClient<Channel>, auth: AuthClient<Channel>,
session: SessionClient<InterceptedService<Channel, network::SessionInterceptor>>, session: SessionClient<InterceptedService<Channel, network::SessionInterceptor>>,
@ -37,39 +37,29 @@ struct ClientInner {
impl Client { impl Client {
/// Connect to the server, authenticate and instantiate a new [`Client`]. /// Connect to the server, authenticate and instantiate a new [`Client`].
pub async fn connect( pub async fn connect(config: crate::api::Config) -> ConnectionResult<Self> {
host: impl AsRef<str>, // TODO move these two into network.rs
username: impl AsRef<str>, let channel = Endpoint::from_shared(config.endpoint())?.connect().await?;
password: impl AsRef<str>,
) -> ConnectionResult<Self> {
let host = if host.as_ref().starts_with("http") {
host.as_ref().to_string()
} else {
format!("https://{}", host.as_ref())
};
let channel = Endpoint::from_shared(host.clone())?.connect().await?;
let mut auth = AuthClient::new(channel.clone()); let mut auth = AuthClient::new(channel.clone());
let resp = auth.login(LoginRequest { let resp = auth.login(LoginRequest {
username: username.as_ref().to_string(), username: config.username.clone(),
password: password.as_ref().to_string(), password: config.password.clone(),
}) })
.await? .await?
.into_inner(); .into_inner();
let claims = InternallyMutable::new(resp.token); let claims = InternallyMutable::new(resp.token);
// TODO move this one into network.rs
let session = SessionClient::with_interceptor( let session = SessionClient::with_interceptor(
channel, network::SessionInterceptor(claims.channel()) channel, network::SessionInterceptor(claims.channel())
); );
Ok(Client(Arc::new(ClientInner { Ok(Client(Arc::new(ClientInner {
host,
user: resp.user.into(), user: resp.user.into(),
workspaces: DashMap::default(), workspaces: DashMap::default(),
claims, claims, auth, session, config
auth, session,
}))) })))
} }
@ -139,7 +129,7 @@ impl Client {
let ws = Workspace::try_new( let ws = Workspace::try_new(
workspace.as_ref().to_string(), workspace.as_ref().to_string(),
self.0.user.clone(), self.0.user.clone(),
&self.0.host, self.0.config.clone(),
token, token,
self.0.claims.channel(), self.0.claims.channel(),
) )

View file

@ -54,12 +54,12 @@ impl Workspace {
pub(crate) async fn try_new( pub(crate) async fn try_new(
name: String, name: String,
user: User, user: User,
dest: &str, config: crate::api::Config,
token: Token, token: Token,
claims: tokio::sync::watch::Receiver<codemp_proto::common::Token>, // TODO ughh receiving this claims: tokio::sync::watch::Receiver<codemp_proto::common::Token>, // TODO ughh receiving this
) -> ConnectionResult<Self> { ) -> ConnectionResult<Self> {
let workspace_claim = InternallyMutable::new(token); let workspace_claim = InternallyMutable::new(token);
let services = Services::try_new(dest, claims, workspace_claim.channel()).await?; let services = Services::try_new(&config.endpoint(), claims, workspace_claim.channel()).await?;
let ws_stream = services.ws().attach(Empty {}).await?.into_inner(); let ws_stream = services.ws().attach(Empty {}).await?.into_inner();
let (tx, rx) = mpsc::channel(128); let (tx, rx) = mpsc::channel(128);