Skip to content

Commit

Permalink
feat: move backend structure to shared module
Browse files Browse the repository at this point in the history
Signed-off-by: iverly <[email protected]>
  • Loading branch information
iverly committed Jan 7, 2024
1 parent 818202f commit 48fb7be
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 78 deletions.
68 changes: 0 additions & 68 deletions proxy/src/backend.rs

This file was deleted.

21 changes: 11 additions & 10 deletions proxy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use tokio::{

use crate::stream::Stream;

pub mod backend;
pub mod stream;

/// The proxy is responsible for accepting connections from the client and
Expand Down Expand Up @@ -237,11 +236,13 @@ impl Proxy {
})?;
}
Event::PutBackend(backend, tx) => {
tx.send(storage.lock().await.add_backend(backend::Backend::new(
backend.hostname,
backend.redirect_ip,
backend.redirect_port,
)))
tx.send(storage.lock().await.add_backend(
shared::models::backend::Backend::new(
backend.hostname,
backend.redirect_ip,
backend.redirect_port,
),
))
.map_err(|_| {
log::error!("failed to send put backend response");
anyhow!("failed to send put backend response")
Expand All @@ -252,11 +253,11 @@ impl Proxy {
.lock()
.await
.get_backends()
.into_iter()
.iter()
.map(|backend| shared::models::backend::Backend {
hostname: backend.hostname().to_string(),
redirect_ip: backend.redirect_ip().to_string(),
redirect_port: backend.redirect_port(),
hostname: backend.1.hostname().to_string(),
redirect_ip: backend.1.redirect_ip().to_string(),
redirect_port: backend.1.redirect_port(),
})
.collect::<Vec<_>>()))
.map_err(|_| {
Expand Down
62 changes: 62 additions & 0 deletions shared/src/models/backend.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,68 @@
/// A backend is a Minecraft server that the proxy can connect to.
///
/// Properties:
///
/// * `host`: The hostname of the backend server.
/// * `port`: The port that the backend server is listening on.
#[derive(Debug, Clone)]
pub struct Backend {
pub hostname: String,
pub redirect_ip: String,
pub redirect_port: u16,
}

impl Backend {
/// Creates a new instance of the `Backend` struct
///
/// Arguments:
///
/// * `host` - The host of the backend
/// * `port` - The port of the backend
///
/// Returns:
///
/// A new instance of the struct.
pub fn new(hostname: String, redirect_ip: String, redirect_port: u16) -> Self {
Self {
hostname,
redirect_ip,
redirect_port,
}
}

/// It returns the host of the backend
///
/// Returns:
///
/// The host of the backend
pub fn hostname(&self) -> &str {
self.hostname.as_str()
}

/// It returns the ip of the backend
///
/// Returns:
///
/// The ip of the backend
pub fn redirect_ip(&self) -> &str {
self.redirect_ip.as_str()
}

/// It returns the port of the backend
///
/// Returns:
///
/// The port of the backend
pub fn redirect_port(&self) -> u16 {
self.redirect_port
}

/// It returns the address of the backend
///
/// Returns:
///
/// The address of the backend
pub fn addr(&self) -> String {
self.redirect_ip.clone() + ":" + &self.redirect_port.to_string()
}
}

0 comments on commit 48fb7be

Please sign in to comment.