mirror of
https://github.com/hexedtech/codemp.git
synced 2024-12-23 05:14:54 +01:00
feat: added codemp config, changed connect methods
This commit is contained in:
parent
ba566439d3
commit
d57fb2c4b6
4 changed files with 59 additions and 21 deletions
44
src/api/config.rs
Normal file
44
src/api/config.rs
Normal 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()
|
||||
)
|
||||
}
|
||||
}
|
|
@ -7,6 +7,9 @@ pub mod controller;
|
|||
/// a generic representation of a text change
|
||||
pub mod change;
|
||||
|
||||
/// client configuration
|
||||
pub mod config;
|
||||
|
||||
/// representation for an user's cursor
|
||||
pub mod cursor;
|
||||
|
||||
|
@ -18,6 +21,7 @@ pub mod user;
|
|||
|
||||
pub use controller::Controller;
|
||||
pub use change::TextChange;
|
||||
pub use config::Config;
|
||||
pub use cursor::Cursor;
|
||||
pub use event::Event;
|
||||
pub use user::User;
|
||||
|
|
|
@ -28,7 +28,7 @@ pub struct Client(Arc<ClientInner>);
|
|||
#[derive(Debug)]
|
||||
struct ClientInner {
|
||||
user: User,
|
||||
host: String,
|
||||
config: crate::api::Config,
|
||||
workspaces: DashMap<String, Workspace>,
|
||||
auth: AuthClient<Channel>,
|
||||
session: SessionClient<InterceptedService<Channel, network::SessionInterceptor>>,
|
||||
|
@ -37,39 +37,29 @@ struct ClientInner {
|
|||
|
||||
impl Client {
|
||||
/// Connect to the server, authenticate and instantiate a new [`Client`].
|
||||
pub async fn connect(
|
||||
host: impl AsRef<str>,
|
||||
username: impl AsRef<str>,
|
||||
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?;
|
||||
pub async fn connect(config: crate::api::Config) -> ConnectionResult<Self> {
|
||||
// TODO move these two into network.rs
|
||||
let channel = Endpoint::from_shared(config.endpoint())?.connect().await?;
|
||||
let mut auth = AuthClient::new(channel.clone());
|
||||
|
||||
let resp = auth.login(LoginRequest {
|
||||
username: username.as_ref().to_string(),
|
||||
password: password.as_ref().to_string(),
|
||||
username: config.username.clone(),
|
||||
password: config.password.clone(),
|
||||
})
|
||||
.await?
|
||||
.into_inner();
|
||||
|
||||
let claims = InternallyMutable::new(resp.token);
|
||||
|
||||
// TODO move this one into network.rs
|
||||
let session = SessionClient::with_interceptor(
|
||||
channel, network::SessionInterceptor(claims.channel())
|
||||
);
|
||||
|
||||
Ok(Client(Arc::new(ClientInner {
|
||||
host,
|
||||
user: resp.user.into(),
|
||||
workspaces: DashMap::default(),
|
||||
claims,
|
||||
auth, session,
|
||||
claims, auth, session, config
|
||||
})))
|
||||
}
|
||||
|
||||
|
@ -139,7 +129,7 @@ impl Client {
|
|||
let ws = Workspace::try_new(
|
||||
workspace.as_ref().to_string(),
|
||||
self.0.user.clone(),
|
||||
&self.0.host,
|
||||
self.0.config.clone(),
|
||||
token,
|
||||
self.0.claims.channel(),
|
||||
)
|
||||
|
|
|
@ -54,12 +54,12 @@ impl Workspace {
|
|||
pub(crate) async fn try_new(
|
||||
name: String,
|
||||
user: User,
|
||||
dest: &str,
|
||||
config: crate::api::Config,
|
||||
token: Token,
|
||||
claims: tokio::sync::watch::Receiver<codemp_proto::common::Token>, // TODO ughh receiving this
|
||||
) -> ConnectionResult<Self> {
|
||||
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 (tx, rx) = mpsc::channel(128);
|
||||
|
|
Loading…
Reference in a new issue